Projekt

Obecné

Profil

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

    
3
namespace Illuminate\Queue;
4

    
5
use Closure;
6
use InvalidArgumentException;
7
use Illuminate\Contracts\Queue\Factory as FactoryContract;
8
use Illuminate\Contracts\Queue\Monitor as MonitorContract;
9

    
10
class QueueManager implements FactoryContract, MonitorContract
11
{
12
    /**
13
     * The application instance.
14
     *
15
     * @var \Illuminate\Foundation\Application
16
     */
17
    protected $app;
18

    
19
    /**
20
     * The array of resolved queue connections.
21
     *
22
     * @var array
23
     */
24
    protected $connections = [];
25

    
26
    /**
27
     * The array of resolved queue connectors.
28
     *
29
     * @var array
30
     */
31
    protected $connectors = [];
32

    
33
    /**
34
     * Create a new queue 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
     * Register an event listener for the before job event.
46
     *
47
     * @param  mixed  $callback
48
     * @return void
49
     */
50
    public function before($callback)
51
    {
52
        $this->app['events']->listen(Events\JobProcessing::class, $callback);
53
    }
54

    
55
    /**
56
     * Register an event listener for the after job event.
57
     *
58
     * @param  mixed  $callback
59
     * @return void
60
     */
61
    public function after($callback)
62
    {
63
        $this->app['events']->listen(Events\JobProcessed::class, $callback);
64
    }
65

    
66
    /**
67
     * Register an event listener for the exception occurred job event.
68
     *
69
     * @param  mixed  $callback
70
     * @return void
71
     */
72
    public function exceptionOccurred($callback)
73
    {
74
        $this->app['events']->listen(Events\JobExceptionOccurred::class, $callback);
75
    }
76

    
77
    /**
78
     * Register an event listener for the daemon queue loop.
79
     *
80
     * @param  mixed  $callback
81
     * @return void
82
     */
83
    public function looping($callback)
84
    {
85
        $this->app['events']->listen('illuminate.queue.looping', $callback);
86
    }
87

    
88
    /**
89
     * Register an event listener for the failed job event.
90
     *
91
     * @param  mixed  $callback
92
     * @return void
93
     */
94
    public function failing($callback)
95
    {
96
        $this->app['events']->listen(Events\JobFailed::class, $callback);
97
    }
98

    
99
    /**
100
     * Register an event listener for the daemon queue stopping.
101
     *
102
     * @param  mixed  $callback
103
     * @return void
104
     */
105
    public function stopping($callback)
106
    {
107
        $this->app['events']->listen(Events\WorkerStopping::class, $callback);
108
    }
109

    
110
    /**
111
     * Determine if the driver is connected.
112
     *
113
     * @param  string  $name
114
     * @return bool
115
     */
116
    public function connected($name = null)
117
    {
118
        return isset($this->connections[$name ?: $this->getDefaultDriver()]);
119
    }
120

    
121
    /**
122
     * Resolve a queue connection instance.
123
     *
124
     * @param  string  $name
125
     * @return \Illuminate\Contracts\Queue\Queue
126
     */
127
    public function connection($name = null)
128
    {
129
        $name = $name ?: $this->getDefaultDriver();
130

    
131
        // If the connection has not been resolved yet we will resolve it now as all
132
        // of the connections are resolved when they are actually needed so we do
133
        // not make any unnecessary connection to the various queue end-points.
134
        if (! isset($this->connections[$name])) {
135
            $this->connections[$name] = $this->resolve($name);
136

    
137
            $this->connections[$name]->setContainer($this->app);
138

    
139
            $this->connections[$name]->setEncrypter($this->app['encrypter']);
140
        }
141

    
142
        return $this->connections[$name];
143
    }
144

    
145
    /**
146
     * Resolve a queue connection.
147
     *
148
     * @param  string  $name
149
     * @return \Illuminate\Contracts\Queue\Queue
150
     */
151
    protected function resolve($name)
152
    {
153
        $config = $this->getConfig($name);
154

    
155
        return $this->getConnector($config['driver'])->connect($config);
156
    }
157

    
158
    /**
159
     * Get the connector for a given driver.
160
     *
161
     * @param  string  $driver
162
     * @return \Illuminate\Queue\Connectors\ConnectorInterface
163
     *
164
     * @throws \InvalidArgumentException
165
     */
166
    protected function getConnector($driver)
167
    {
168
        if (isset($this->connectors[$driver])) {
169
            return call_user_func($this->connectors[$driver]);
170
        }
171

    
172
        throw new InvalidArgumentException("No connector for [$driver]");
173
    }
174

    
175
    /**
176
     * Add a queue connection resolver.
177
     *
178
     * @param  string    $driver
179
     * @param  \Closure  $resolver
180
     * @return void
181
     */
182
    public function extend($driver, Closure $resolver)
183
    {
184
        return $this->addConnector($driver, $resolver);
185
    }
186

    
187
    /**
188
     * Add a queue connection resolver.
189
     *
190
     * @param  string    $driver
191
     * @param  \Closure  $resolver
192
     * @return void
193
     */
194
    public function addConnector($driver, Closure $resolver)
195
    {
196
        $this->connectors[$driver] = $resolver;
197
    }
198

    
199
    /**
200
     * Get the queue connection configuration.
201
     *
202
     * @param  string  $name
203
     * @return array
204
     */
205
    protected function getConfig($name)
206
    {
207
        if ($name === null || $name === 'null') {
208
            return ['driver' => 'null'];
209
        }
210

    
211
        return $this->app['config']["queue.connections.{$name}"];
212
    }
213

    
214
    /**
215
     * Get the name of the default queue connection.
216
     *
217
     * @return string
218
     */
219
    public function getDefaultDriver()
220
    {
221
        return $this->app['config']['queue.default'];
222
    }
223

    
224
    /**
225
     * Set the name of the default queue connection.
226
     *
227
     * @param  string  $name
228
     * @return void
229
     */
230
    public function setDefaultDriver($name)
231
    {
232
        $this->app['config']['queue.default'] = $name;
233
    }
234

    
235
    /**
236
     * Get the full name for the given connection.
237
     *
238
     * @param  string  $connection
239
     * @return string
240
     */
241
    public function getName($connection = null)
242
    {
243
        return $connection ?: $this->getDefaultDriver();
244
    }
245

    
246
    /**
247
     * Determine if the application is in maintenance mode.
248
     *
249
     * @return bool
250
     */
251
    public function isDownForMaintenance()
252
    {
253
        return $this->app->isDownForMaintenance();
254
    }
255

    
256
    /**
257
     * Dynamically pass calls to the default connection.
258
     *
259
     * @param  string  $method
260
     * @param  array   $parameters
261
     * @return mixed
262
     */
263
    public function __call($method, $parameters)
264
    {
265
        $callable = [$this->connection(), $method];
266

    
267
        return call_user_func_array($callable, $parameters);
268
    }
269
}
(10-10/18)