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