123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
-
- namespace Illuminate\Broadcasting;
-
- use Closure;
- use Pusher\Pusher;
- use Psr\Log\LoggerInterface;
- use InvalidArgumentException;
- use Illuminate\Broadcasting\Broadcasters\LogBroadcaster;
- use Illuminate\Broadcasting\Broadcasters\NullBroadcaster;
- use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
- use Illuminate\Broadcasting\Broadcasters\RedisBroadcaster;
- use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
- use Illuminate\Contracts\Broadcasting\Factory as FactoryContract;
-
- /**
- * @mixin \Illuminate\Contracts\Broadcasting\Broadcaster
- */
- class BroadcastManager implements FactoryContract
- {
- /**
- * The application instance.
- *
- * @var \Illuminate\Foundation\Application
- */
- protected $app;
-
- /**
- * The array of resolved broadcast drivers.
- *
- * @var array
- */
- protected $drivers = [];
-
- /**
- * The registered custom driver creators.
- *
- * @var array
- */
- protected $customCreators = [];
-
- /**
- * Create a new manager instance.
- *
- * @param \Illuminate\Foundation\Application $app
- * @return void
- */
- public function __construct($app)
- {
- $this->app = $app;
- }
-
- /**
- * Register the routes for handling broadcast authentication and sockets.
- *
- * @param array|null $attributes
- * @return void
- */
- public function routes(array $attributes = null)
- {
- if ($this->app->routesAreCached()) {
- return;
- }
-
- $attributes = $attributes ?: ['middleware' => ['web']];
-
- $this->app['router']->group($attributes, function ($router) {
- $router->match(
- ['get', 'post'], '/broadcasting/auth',
- '\\'.BroadcastController::class.'@authenticate'
- );
- });
- }
-
- /**
- * Get the socket ID for the given request.
- *
- * @param \Illuminate\Http\Request|null $request
- * @return string|null
- */
- public function socket($request = null)
- {
- if (! $request && ! $this->app->bound('request')) {
- return;
- }
-
- $request = $request ?: $this->app['request'];
-
- return $request->header('X-Socket-ID');
- }
-
- /**
- * Begin broadcasting an event.
- *
- * @param mixed|null $event
- * @return \Illuminate\Broadcasting\PendingBroadcast|void
- */
- public function event($event = null)
- {
- return new PendingBroadcast($this->app->make('events'), $event);
- }
-
- /**
- * Queue the given event for broadcast.
- *
- * @param mixed $event
- * @return void
- */
- public function queue($event)
- {
- $connection = $event instanceof ShouldBroadcastNow ? 'sync' : null;
-
- if (is_null($connection) && isset($event->connection)) {
- $connection = $event->connection;
- }
-
- $queue = null;
-
- if (method_exists($event, 'broadcastQueue')) {
- $queue = $event->broadcastQueue();
- } elseif (isset($event->broadcastQueue)) {
- $queue = $event->broadcastQueue;
- } elseif (isset($event->queue)) {
- $queue = $event->queue;
- }
-
- $this->app->make('queue')->connection($connection)->pushOn(
- $queue, new BroadcastEvent(clone $event)
- );
- }
-
- /**
- * Get a driver instance.
- *
- * @param string $driver
- * @return mixed
- */
- public function connection($driver = null)
- {
- return $this->driver($driver);
- }
-
- /**
- * Get a driver instance.
- *
- * @param string|null $name
- * @return mixed
- */
- public function driver($name = null)
- {
- $name = $name ?: $this->getDefaultDriver();
-
- return $this->drivers[$name] = $this->get($name);
- }
-
- /**
- * Attempt to get the connection from the local cache.
- *
- * @param string $name
- * @return \Illuminate\Contracts\Broadcasting\Broadcaster
- */
- protected function get($name)
- {
- return $this->drivers[$name] ?? $this->resolve($name);
- }
-
- /**
- * Resolve the given store.
- *
- * @param string $name
- * @return \Illuminate\Contracts\Broadcasting\Broadcaster
- *
- * @throws \InvalidArgumentException
- */
- protected function resolve($name)
- {
- $config = $this->getConfig($name);
-
- if (is_null($config)) {
- throw new InvalidArgumentException("Broadcaster [{$name}] is not defined.");
- }
-
- if (isset($this->customCreators[$config['driver']])) {
- return $this->callCustomCreator($config);
- }
-
- $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
-
- if (! method_exists($this, $driverMethod)) {
- throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
- }
-
- return $this->{$driverMethod}($config);
- }
-
- /**
- * Call a custom driver creator.
- *
- * @param array $config
- * @return mixed
- */
- protected function callCustomCreator(array $config)
- {
- return $this->customCreators[$config['driver']]($this->app, $config);
- }
-
- /**
- * Create an instance of the driver.
- *
- * @param array $config
- * @return \Illuminate\Contracts\Broadcasting\Broadcaster
- */
- protected function createPusherDriver(array $config)
- {
- return new PusherBroadcaster(
- new Pusher($config['key'], $config['secret'],
- $config['app_id'], $config['options'] ?? [])
- );
- }
-
- /**
- * Create an instance of the driver.
- *
- * @param array $config
- * @return \Illuminate\Contracts\Broadcasting\Broadcaster
- */
- protected function createRedisDriver(array $config)
- {
- return new RedisBroadcaster(
- $this->app->make('redis'), $config['connection'] ?? null
- );
- }
-
- /**
- * Create an instance of the driver.
- *
- * @param array $config
- * @return \Illuminate\Contracts\Broadcasting\Broadcaster
- */
- protected function createLogDriver(array $config)
- {
- return new LogBroadcaster(
- $this->app->make(LoggerInterface::class)
- );
- }
-
- /**
- * Create an instance of the driver.
- *
- * @param array $config
- * @return \Illuminate\Contracts\Broadcasting\Broadcaster
- */
- protected function createNullDriver(array $config)
- {
- return new NullBroadcaster;
- }
-
- /**
- * Get the connection configuration.
- *
- * @param string $name
- * @return array
- */
- protected function getConfig($name)
- {
- return $this->app['config']["broadcasting.connections.{$name}"];
- }
-
- /**
- * Get the default driver name.
- *
- * @return string
- */
- public function getDefaultDriver()
- {
- return $this->app['config']['broadcasting.default'];
- }
-
- /**
- * Set the default driver name.
- *
- * @param string $name
- * @return void
- */
- public function setDefaultDriver($name)
- {
- $this->app['config']['broadcasting.default'] = $name;
- }
-
- /**
- * Register a custom driver creator Closure.
- *
- * @param string $driver
- * @param \Closure $callback
- * @return $this
- */
- public function extend($driver, Closure $callback)
- {
- $this->customCreators[$driver] = $callback;
-
- return $this;
- }
-
- /**
- * Dynamically call the default driver instance.
- *
- * @param string $method
- * @param array $parameters
- * @return mixed
- */
- public function __call($method, $parameters)
- {
- return $this->driver()->$method(...$parameters);
- }
- }
|