PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/dzakiafif/cokelatklasik
PHP | 210 lines | 87 code | 23 blank | 100 comment | 3 complexity | 748c4e57aa72675a1330152927f952f4 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Broadcasting;
  3. use Pusher;
  4. use Closure;
  5. use InvalidArgumentException;
  6. use Illuminate\Broadcasting\Broadcasters\LogBroadcaster;
  7. use Illuminate\Broadcasting\Broadcasters\RedisBroadcaster;
  8. use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
  9. use Illuminate\Contracts\Broadcasting\Factory as FactoryContract;
  10. class BroadcastManager implements FactoryContract
  11. {
  12. /**
  13. * The application instance.
  14. *
  15. * @var \Illuminate\Foundation\Application
  16. */
  17. protected $app;
  18. /**
  19. * The array of resolved broadcast drivers.
  20. *
  21. * @var array
  22. */
  23. protected $drivers = [];
  24. /**
  25. * The registered custom driver creators.
  26. *
  27. * @var array
  28. */
  29. protected $customCreators = [];
  30. /**
  31. * Create a new manager instance.
  32. *
  33. * @param \Illuminate\Foundation\Application $app
  34. * @return void
  35. */
  36. public function __construct($app)
  37. {
  38. $this->app = $app;
  39. }
  40. /**
  41. * Get a driver instance.
  42. *
  43. * @param string $driver
  44. * @return mixed
  45. */
  46. public function connection($driver = null)
  47. {
  48. return $this->driver($driver);
  49. }
  50. /**
  51. * Get a driver instance.
  52. *
  53. * @param string $name
  54. * @return mixed
  55. */
  56. public function driver($name = null)
  57. {
  58. $name = $name ?: $this->getDefaultDriver();
  59. return $this->drivers[$name] = $this->get($name);
  60. }
  61. /**
  62. * Attempt to get the connection from the local cache.
  63. *
  64. * @param string $name
  65. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  66. */
  67. protected function get($name)
  68. {
  69. return isset($this->drivers[$name]) ? $this->drivers[$name] : $this->resolve($name);
  70. }
  71. /**
  72. * Resolve the given store.
  73. *
  74. * @param string $name
  75. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  76. */
  77. protected function resolve($name)
  78. {
  79. $config = $this->getConfig($name);
  80. if (is_null($config)) {
  81. throw new InvalidArgumentException("Broadcaster [{$name}] is not defined.");
  82. }
  83. if (isset($this->customCreators[$config['driver']])) {
  84. return $this->callCustomCreator($config);
  85. } else {
  86. return $this->{'create'.ucfirst($config['driver']).'Driver'}($config);
  87. }
  88. }
  89. /**
  90. * Call a custom driver creator.
  91. *
  92. * @param array $config
  93. * @return mixed
  94. */
  95. protected function callCustomCreator(array $config)
  96. {
  97. return $this->customCreators[$config['driver']]($this->app, $config);
  98. }
  99. /**
  100. * Create an instance of the driver.
  101. *
  102. * @param array $config
  103. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  104. */
  105. protected function createPusherDriver(array $config)
  106. {
  107. return new PusherBroadcaster(
  108. new Pusher($config['key'], $config['secret'], $config['app_id'])
  109. );
  110. }
  111. /**
  112. * Create an instance of the driver.
  113. *
  114. * @param array $config
  115. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  116. */
  117. protected function createRedisDriver(array $config)
  118. {
  119. return new RedisBroadcaster(
  120. $this->app->make('redis'), array_get($config, 'connection')
  121. );
  122. }
  123. /**
  124. * Create an instance of the driver.
  125. *
  126. * @param array $config
  127. * @return \Illuminate\Contracts\Broadcasting\Broadcaster
  128. */
  129. protected function createLogDriver(array $config)
  130. {
  131. return new LogBroadcaster(
  132. $this->app->make('Psr\Log\LoggerInterface')
  133. );
  134. }
  135. /**
  136. * Get the connection configuration.
  137. *
  138. * @param string $name
  139. * @return array
  140. */
  141. protected function getConfig($name)
  142. {
  143. return $this->app['config']["broadcasting.connections.{$name}"];
  144. }
  145. /**
  146. * Get the default driver name.
  147. *
  148. * @return string
  149. */
  150. public function getDefaultDriver()
  151. {
  152. return $this->app['config']['broadcasting.default'];
  153. }
  154. /**
  155. * Set the default driver name.
  156. *
  157. * @param string $name
  158. * @return void
  159. */
  160. public function setDefaultDriver($name)
  161. {
  162. $this->app['config']['broadcasting.default'] = $name;
  163. }
  164. /**
  165. * Register a custom driver creator Closure.
  166. *
  167. * @param string $driver
  168. * @param \Closure $callback
  169. * @return $this
  170. */
  171. public function extend($driver, Closure $callback)
  172. {
  173. $this->customCreators[$driver] = $callback;
  174. return $this;
  175. }
  176. /**
  177. * Dynamically call the default driver instance.
  178. *
  179. * @param string $method
  180. * @param array $parameters
  181. * @return mixed
  182. */
  183. public function __call($method, $parameters)
  184. {
  185. return call_user_func_array([$this->driver(), $method], $parameters);
  186. }
  187. }