Projekt

Obecné

Profil

Stáhnout (5.13 KB) Statistiky
| Větev: | Revize:
1
<?php
2

    
3
namespace Illuminate\Broadcasting;
4

    
5
use Pusher;
6
use Closure;
7
use Illuminate\Support\Arr;
8
use InvalidArgumentException;
9
use Illuminate\Broadcasting\Broadcasters\LogBroadcaster;
10
use Illuminate\Broadcasting\Broadcasters\RedisBroadcaster;
11
use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
12
use Illuminate\Contracts\Broadcasting\Factory as FactoryContract;
13

    
14
class BroadcastManager implements FactoryContract
15
{
16
    /**
17
     * The application instance.
18
     *
19
     * @var \Illuminate\Foundation\Application
20
     */
21
    protected $app;
22

    
23
    /**
24
     * The array of resolved broadcast drivers.
25
     *
26
     * @var array
27
     */
28
    protected $drivers = [];
29

    
30
    /**
31
     * The registered custom driver creators.
32
     *
33
     * @var array
34
     */
35
    protected $customCreators = [];
36

    
37
    /**
38
     * Create a new manager instance.
39
     *
40
     * @param  \Illuminate\Foundation\Application  $app
41
     * @return void
42
     */
43
    public function __construct($app)
44
    {
45
        $this->app = $app;
46
    }
47

    
48
    /**
49
     * Get a driver instance.
50
     *
51
     * @param  string  $driver
52
     * @return mixed
53
     */
54
    public function connection($driver = null)
55
    {
56
        return $this->driver($driver);
57
    }
58

    
59
    /**
60
     * Get a driver instance.
61
     *
62
     * @param  string  $name
63
     * @return mixed
64
     */
65
    public function driver($name = null)
66
    {
67
        $name = $name ?: $this->getDefaultDriver();
68

    
69
        return $this->drivers[$name] = $this->get($name);
70
    }
71

    
72
    /**
73
     * Attempt to get the connection from the local cache.
74
     *
75
     * @param  string  $name
76
     * @return \Illuminate\Contracts\Broadcasting\Broadcaster
77
     */
78
    protected function get($name)
79
    {
80
        return isset($this->drivers[$name]) ? $this->drivers[$name] : $this->resolve($name);
81
    }
82

    
83
    /**
84
     * Resolve the given store.
85
     *
86
     * @param  string  $name
87
     * @return \Illuminate\Contracts\Broadcasting\Broadcaster
88
     *
89
     * @throws \InvalidArgumentException
90
     */
91
    protected function resolve($name)
92
    {
93
        $config = $this->getConfig($name);
94

    
95
        if (is_null($config)) {
96
            throw new InvalidArgumentException("Broadcaster [{$name}] is not defined.");
97
        }
98

    
99
        if (isset($this->customCreators[$config['driver']])) {
100
            return $this->callCustomCreator($config);
101
        } else {
102
            $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
103

    
104
            if (method_exists($this, $driverMethod)) {
105
                return $this->{$driverMethod}($config);
106
            } else {
107
                throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported.");
108
            }
109
        }
110
    }
111

    
112
    /**
113
     * Call a custom driver creator.
114
     *
115
     * @param  array  $config
116
     * @return mixed
117
     */
118
    protected function callCustomCreator(array $config)
119
    {
120
        return $this->customCreators[$config['driver']]($this->app, $config);
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 createPusherDriver(array $config)
130
    {
131
        return new PusherBroadcaster(
132
            new Pusher($config['key'], $config['secret'], $config['app_id'], Arr::get($config, 'options', []))
133
        );
134
    }
135

    
136
    /**
137
     * Create an instance of the driver.
138
     *
139
     * @param  array  $config
140
     * @return \Illuminate\Contracts\Broadcasting\Broadcaster
141
     */
142
    protected function createRedisDriver(array $config)
143
    {
144
        return new RedisBroadcaster(
145
            $this->app->make('redis'), Arr::get($config, 'connection')
146
        );
147
    }
148

    
149
    /**
150
     * Create an instance of the driver.
151
     *
152
     * @param  array  $config
153
     * @return \Illuminate\Contracts\Broadcasting\Broadcaster
154
     */
155
    protected function createLogDriver(array $config)
156
    {
157
        return new LogBroadcaster(
158
            $this->app->make('Psr\Log\LoggerInterface')
159
        );
160
    }
161

    
162
    /**
163
     * Get the connection configuration.
164
     *
165
     * @param  string  $name
166
     * @return array
167
     */
168
    protected function getConfig($name)
169
    {
170
        return $this->app['config']["broadcasting.connections.{$name}"];
171
    }
172

    
173
    /**
174
     * Get the default driver name.
175
     *
176
     * @return string
177
     */
178
    public function getDefaultDriver()
179
    {
180
        return $this->app['config']['broadcasting.default'];
181
    }
182

    
183
    /**
184
     * Set the default driver name.
185
     *
186
     * @param  string  $name
187
     * @return void
188
     */
189
    public function setDefaultDriver($name)
190
    {
191
        $this->app['config']['broadcasting.default'] = $name;
192
    }
193

    
194
    /**
195
     * Register a custom driver creator Closure.
196
     *
197
     * @param  string    $driver
198
     * @param  \Closure  $callback
199
     * @return $this
200
     */
201
    public function extend($driver, Closure $callback)
202
    {
203
        $this->customCreators[$driver] = $callback;
204

    
205
        return $this;
206
    }
207

    
208
    /**
209
     * Dynamically call the default driver instance.
210
     *
211
     * @param  string  $method
212
     * @param  array   $parameters
213
     * @return mixed
214
     */
215
    public function __call($method, $parameters)
216
    {
217
        return call_user_func_array([$this->driver(), $method], $parameters);
218
    }
219
}
(2-2/4)