PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php

https://gitlab.com/jjpa2018/dashboard
PHP | 386 lines | 347 code | 9 blank | 30 comment | 2 complexity | af13ab832cb96f4b05cc83d674ded414 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Broadcasting;
  3. use Ably\AblyRest;
  4. use Closure;
  5. use Illuminate\Broadcasting\Broadcasters\AblyBroadcaster;
  6. use Illuminate\Broadcasting\Broadcasters\LogBroadcaster;
  7. use Illuminate\Broadcasting\Broadcasters\NullBroadcaster;
  8. use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
  9. use Illuminate\Broadcasting\Broadcasters\RedisBroadcaster;
  10. use Illuminate\Contracts\Broadcasting\Factory as FactoryContract;
  11. use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
  12. use Illuminate\Contracts\Bus\Dispatcher as BusDispatcherContract;
  13. use Illuminate\Contracts\Foundation\CachesRoutes;
  14. use InvalidArgumentException;
  15. use Psr\Log\LoggerInterface;
  16. use Pusher\Pusher;
  17. /**
  18. * @mixin \Illuminate\Contracts\Broadcasting\Broadcaster
  19. */
  20. class BroadcastManager implements FactoryContract
  21. {
  22. /**
  23. * The application instance.
  24. *
  25. * @var \Illuminate\Contracts\Container\Container
  26. */
  27. protected $app;
  28. /**
  29. * The array of resolved broadcast drivers.
  30. *
  31. * @var array
  32. */
  33. protected $drivers = [];
  34. /**
  35. * The registered custom driver creators.
  36. *
  37. * @var array
  38. */
  39. protected $customCreators = [];
  40. /**
  41. * Create a new manager instance.
  42. *
  43. * @param \Illuminate\Contracts\Container\Container $app
  44. * @return void
  45. */
  46. public function __construct($app)
  47. {
  48. $this->app = $app;
  49. }
  50. /**
  51. * Register the routes for handling broadcast authentication and sockets.
  52. *
  53. * @param array|null $attributes
  54. * @return void
  55. */
  56. public function routes(array $attributes = null)
  57. {
  58. if ($this->app instanceof CachesRoutes && $this->app->routesAreCached()) {
  59. return;
  60. }
  61. $attributes = $attributes ?: ['middleware' => ['web']];
  62. $this->app['router']->group($attributes, function ($router) {
  63. $router->match(
  64. ['get', 'post'], '/broadcasting/auth',
  65. '\\'.BroadcastController::class.'@authenticate'
  66. )->withoutMiddleware([\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class]);
  67. });
  68. }
  69. /**
  70. * Get the socket ID for the given request.
  71. *
  72. * @param \Illuminate\Http\Request|null $request
  73. * @return string|null
  74. */
  75. public function socket($request = null)
  76. {
  77. if (! $request && ! $this->app->bound('request')) {
  78. return;
  79. }
  80. $request = $request ?: $this->app['request'];
  81. return $request->header('X-Socket-ID');
  82. }
  83. /**
  84. * Begin broadcasting an event.
  85. *
  86. * @param mixed|null $event
  87. * @return \Illuminate\Broadcasting\PendingBroadcast
  88. */
  89. public function event($event = null)
  90. {
  91. return new PendingBroadcast($this->app->make('events'), $event);
  92. }
  93. /**
  94. * Queue the given event for broadcast.
  95. *
  96. * @param mixed $event
  97. * @return void
  98. */
  99. public function queue($event)
  100. {
  101. if ($event instanceof ShouldBroadcastNow ||
  102. (is_object($event) &&
  103. method_exists($event, 'shouldBroadcastNow') &&
  104. $event->shouldBroadcastNow())) {
  105. return $this->app->make(BusDispatcherContract::class)->dispatchNow(new BroadcastEvent(clone $event));
  106. }
  107. $queue = null;
  108. if (method_exists($event, 'broadcastQueue')) {
  109. $queue = $event->broadcastQueue();
  110. } elseif (isset($event->broadcastQueue)) {
  111. $queue = $event->broadcastQueue;
  112. } elseif (isset($event->queue)) {
  113. $queue = $event->queue;
  114. }
  115. $this->app->make('queue')->connection($event->connection ?? null)->pushOn(
  116. $queue, new BroadcastEvent(clone $event)
  117. );
  118. }
  119. /**
  120. * Get a driver instance.
  121. *
  122. * @param string|null $driver
  123. * @return mixed
  124. */
  125. public function connection($driver = null)
  126. {
  127. return $this->driver($driver);
  128. }
  129. /**
  130. * Get a driver instance.
  131. *
  132. * @param string|null $name
  133. * @return mixed
  134. */
  135. public function driver($name = null)
  136. {
  137. $name = $name ?: $this->getDefaultDriver();
  138. return $this->drivers[$name] = $this->get($name);
  139. }
  140. /**
  141. * Attempt to get the connection from the local cache.
  142. *
  143. * @param string $name
  144. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  145. */
  146. protected function get($name)
  147. {
  148. return $this->drivers[$name] ?? $this->resolve($name);
  149. }
  150. /**
  151. * Resolve the given broadcaster.
  152. *
  153. * @param string $name
  154. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  155. *
  156. * @throws \InvalidArgumentException
  157. */
  158. protected function resolve($name)
  159. {
  160. $config = $this->getConfig($name);
  161. if (isset($this->customCreators[$config['driver']])) {
  162. return $this->callCustomCreator($config);
  163. }
  164. $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
  165. if (! method_exists($this, $driverMethod)) {
  166. throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
  167. }
  168. return $this->{$driverMethod}($config);
  169. }
  170. /**
  171. * Call a custom driver creator.
  172. *
  173. * @param array $config
  174. * @return mixed
  175. */
  176. protected function callCustomCreator(array $config)
  177. {
  178. return $this->customCreators[$config['driver']]($this->app, $config);
  179. }
  180. /**
  181. * Create an instance of the driver.
  182. *
  183. * @param array $config
  184. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  185. */
  186. protected function createPusherDriver(array $config)
  187. {
  188. $pusher = new Pusher(
  189. $config['key'], $config['secret'],
  190. $config['app_id'], $config['options'] ?? []
  191. );
  192. if ($config['log'] ?? false) {
  193. $pusher->setLogger($this->app->make(LoggerInterface::class));
  194. }
  195. return new PusherBroadcaster($pusher);
  196. }
  197. /**
  198. * Create an instance of the driver.
  199. *
  200. * @param array $config
  201. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  202. */
  203. protected function createAblyDriver(array $config)
  204. {
  205. return new AblyBroadcaster(new AblyRest($config));
  206. }
  207. /**
  208. * Create an instance of the driver.
  209. *
  210. * @param array $config
  211. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  212. */
  213. protected function createRedisDriver(array $config)
  214. {
  215. return new RedisBroadcaster(
  216. $this->app->make('redis'), $config['connection'] ?? null,
  217. $this->app['config']->get('database.redis.options.prefix', '')
  218. );
  219. }
  220. /**
  221. * Create an instance of the driver.
  222. *
  223. * @param array $config
  224. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  225. */
  226. protected function createLogDriver(array $config)
  227. {
  228. return new LogBroadcaster(
  229. $this->app->make(LoggerInterface::class)
  230. );
  231. }
  232. /**
  233. * Create an instance of the driver.
  234. *
  235. * @param array $config
  236. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  237. */
  238. protected function createNullDriver(array $config)
  239. {
  240. return new NullBroadcaster;
  241. }
  242. /**
  243. * Get the connection configuration.
  244. *
  245. * @param string $name
  246. * @return array
  247. */
  248. protected function getConfig($name)
  249. {
  250. if (! is_null($name) && $name !== 'null') {
  251. return $this->app['config']["broadcasting.connections.{$name}"];
  252. }
  253. return ['driver' => 'null'];
  254. }
  255. /**
  256. * Get the default driver name.
  257. *
  258. * @return string
  259. */
  260. public function getDefaultDriver()
  261. {
  262. return $this->app['config']['broadcasting.default'];
  263. }
  264. /**
  265. * Set the default driver name.
  266. *
  267. * @param string $name
  268. * @return void
  269. */
  270. public function setDefaultDriver($name)
  271. {
  272. $this->app['config']['broadcasting.default'] = $name;
  273. }
  274. /**
  275. * Disconnect the given disk and remove from local cache.
  276. *
  277. * @param string|null $name
  278. * @return void
  279. */
  280. public function purge($name = null)
  281. {
  282. $name = $name ?? $this->getDefaultDriver();
  283. unset($this->drivers[$name]);
  284. }
  285. /**
  286. * Register a custom driver creator Closure.
  287. *
  288. * @param string $driver
  289. * @param \Closure $callback
  290. * @return $this
  291. */
  292. public function extend($driver, Closure $callback)
  293. {
  294. $this->customCreators[$driver] = $callback;
  295. return $this;
  296. }
  297. /**
  298. * Get the application instance used by the manager.
  299. *
  300. * @return \Illuminate\Contracts\Foundation\Application
  301. */
  302. public function getApplication()
  303. {
  304. return $this->app;
  305. }
  306. /**
  307. * Set the application instance used by the manager.
  308. *
  309. * @param \Illuminate\Contracts\Foundation\Application $app
  310. * @return $this
  311. */
  312. public function setApplication($app)
  313. {
  314. $this->app = $app;
  315. return $this;
  316. }
  317. /**
  318. * Forget all of the resolved driver instances.
  319. *
  320. * @return $this
  321. */
  322. public function forgetDrivers()
  323. {
  324. $this->drivers = [];
  325. return $this;
  326. }
  327. /**
  328. * Dynamically call the default driver instance.
  329. *
  330. * @param string $method
  331. * @param array $parameters
  332. * @return mixed
  333. */
  334. public function __call($method, $parameters)
  335. {
  336. return $this->driver()->$method(...$parameters);
  337. }
  338. }