Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Queue;
4
 
5
use Closure;
6
use Illuminate\Contracts\Queue\Factory as FactoryContract;
7
use Illuminate\Contracts\Queue\Monitor as MonitorContract;
8
use InvalidArgumentException;
9
 
10
/**
11
 * @mixin \Illuminate\Contracts\Queue\Queue
12
 */
13
class QueueManager implements FactoryContract, MonitorContract
14
{
15
    /**
16
     * The application instance.
17
     *
18
     * @var \Illuminate\Contracts\Foundation\Application
19
     */
20
    protected $app;
21
 
22
    /**
23
     * The array of resolved queue connections.
24
     *
25
     * @var array
26
     */
27
    protected $connections = [];
28
 
29
    /**
30
     * The array of resolved queue connectors.
31
     *
32
     * @var array
33
     */
34
    protected $connectors = [];
35
 
36
    /**
37
     * Create a new queue manager instance.
38
     *
39
     * @param  \Illuminate\Contracts\Foundation\Application  $app
40
     * @return void
41
     */
42
    public function __construct($app)
43
    {
44
        $this->app = $app;
45
    }
46
 
47
    /**
48
     * Register an event listener for the before job event.
49
     *
50
     * @param  mixed  $callback
51
     * @return void
52
     */
53
    public function before($callback)
54
    {
55
        $this->app['events']->listen(Events\JobProcessing::class, $callback);
56
    }
57
 
58
    /**
59
     * Register an event listener for the after job event.
60
     *
61
     * @param  mixed  $callback
62
     * @return void
63
     */
64
    public function after($callback)
65
    {
66
        $this->app['events']->listen(Events\JobProcessed::class, $callback);
67
    }
68
 
69
    /**
70
     * Register an event listener for the exception occurred job event.
71
     *
72
     * @param  mixed  $callback
73
     * @return void
74
     */
75
    public function exceptionOccurred($callback)
76
    {
77
        $this->app['events']->listen(Events\JobExceptionOccurred::class, $callback);
78
    }
79
 
80
    /**
81
     * Register an event listener for the daemon queue loop.
82
     *
83
     * @param  mixed  $callback
84
     * @return void
85
     */
86
    public function looping($callback)
87
    {
88
        $this->app['events']->listen(Events\Looping::class, $callback);
89
    }
90
 
91
    /**
92
     * Register an event listener for the failed job event.
93
     *
94
     * @param  mixed  $callback
95
     * @return void
96
     */
97
    public function failing($callback)
98
    {
99
        $this->app['events']->listen(Events\JobFailed::class, $callback);
100
    }
101
 
102
    /**
103
     * Register an event listener for the daemon queue stopping.
104
     *
105
     * @param  mixed  $callback
106
     * @return void
107
     */
108
    public function stopping($callback)
109
    {
110
        $this->app['events']->listen(Events\WorkerStopping::class, $callback);
111
    }
112
 
113
    /**
114
     * Determine if the driver is connected.
115
     *
116
     * @param  string|null  $name
117
     * @return bool
118
     */
119
    public function connected($name = null)
120
    {
121
        return isset($this->connections[$name ?: $this->getDefaultDriver()]);
122
    }
123
 
124
    /**
125
     * Resolve a queue connection instance.
126
     *
127
     * @param  string|null  $name
128
     * @return \Illuminate\Contracts\Queue\Queue
129
     */
130
    public function connection($name = null)
131
    {
132
        $name = $name ?: $this->getDefaultDriver();
133
 
134
        // If the connection has not been resolved yet we will resolve it now as all
135
        // of the connections are resolved when they are actually needed so we do
136
        // not make any unnecessary connection to the various queue end-points.
137
        if (! isset($this->connections[$name])) {
138
            $this->connections[$name] = $this->resolve($name);
139
 
140
            $this->connections[$name]->setContainer($this->app);
141
        }
142
 
143
        return $this->connections[$name];
144
    }
145
 
146
    /**
147
     * Resolve a queue connection.
148
     *
149
     * @param  string  $name
150
     * @return \Illuminate\Contracts\Queue\Queue
151
     *
152
     * @throws \InvalidArgumentException
153
     */
154
    protected function resolve($name)
155
    {
156
        $config = $this->getConfig($name);
157
 
158
        if (is_null($config)) {
159
            throw new InvalidArgumentException("The [{$name}] queue connection has not been configured.");
160
        }
161
 
162
        return $this->getConnector($config['driver'])
163
                        ->connect($config)
164
                        ->setConnectionName($name);
165
    }
166
 
167
    /**
168
     * Get the connector for a given driver.
169
     *
170
     * @param  string  $driver
171
     * @return \Illuminate\Queue\Connectors\ConnectorInterface
172
     *
173
     * @throws \InvalidArgumentException
174
     */
175
    protected function getConnector($driver)
176
    {
177
        if (! isset($this->connectors[$driver])) {
178
            throw new InvalidArgumentException("No connector for [$driver].");
179
        }
180
 
181
        return call_user_func($this->connectors[$driver]);
182
    }
183
 
184
    /**
185
     * Add a queue connection resolver.
186
     *
187
     * @param  string  $driver
188
     * @param  \Closure  $resolver
189
     * @return void
190
     */
191
    public function extend($driver, Closure $resolver)
192
    {
193
        return $this->addConnector($driver, $resolver);
194
    }
195
 
196
    /**
197
     * Add a queue connection resolver.
198
     *
199
     * @param  string  $driver
200
     * @param  \Closure  $resolver
201
     * @return void
202
     */
203
    public function addConnector($driver, Closure $resolver)
204
    {
205
        $this->connectors[$driver] = $resolver;
206
    }
207
 
208
    /**
209
     * Get the queue connection configuration.
210
     *
211
     * @param  string  $name
212
     * @return array|null
213
     */
214
    protected function getConfig($name)
215
    {
216
        if (! is_null($name) && $name !== 'null') {
217
            return $this->app['config']["queue.connections.{$name}"];
218
        }
219
 
220
        return ['driver' => 'null'];
221
    }
222
 
223
    /**
224
     * Get the name of the default queue connection.
225
     *
226
     * @return string
227
     */
228
    public function getDefaultDriver()
229
    {
230
        return $this->app['config']['queue.default'];
231
    }
232
 
233
    /**
234
     * Set the name of the default queue connection.
235
     *
236
     * @param  string  $name
237
     * @return void
238
     */
239
    public function setDefaultDriver($name)
240
    {
241
        $this->app['config']['queue.default'] = $name;
242
    }
243
 
244
    /**
245
     * Get the full name for the given connection.
246
     *
247
     * @param  string|null  $connection
248
     * @return string
249
     */
250
    public function getName($connection = null)
251
    {
252
        return $connection ?: $this->getDefaultDriver();
253
    }
254
 
255
    /**
256
     * Get the application instance used by the manager.
257
     *
258
     * @return \Illuminate\Contracts\Foundation\Application
259
     */
260
    public function getApplication()
261
    {
262
        return $this->app;
263
    }
264
 
265
    /**
266
     * Set the application instance used by the manager.
267
     *
268
     * @param  \Illuminate\Contracts\Foundation\Application  $app
269
     * @return $this
270
     */
271
    public function setApplication($app)
272
    {
273
        $this->app = $app;
274
 
275
        foreach ($this->connections as $connection) {
276
            $connection->setContainer($app);
277
        }
278
 
279
        return $this;
280
    }
281
 
282
    /**
283
     * Dynamically pass calls to the default connection.
284
     *
285
     * @param  string  $method
286
     * @param  array  $parameters
287
     * @return mixed
288
     */
289
    public function __call($method, $parameters)
290
    {
291
        return $this->connection()->$method(...$parameters);
292
    }
293
}