Projekt

Obecné

Profil

Stáhnout (6.91 KB) Statistiky
| Větev: | Revize:
1 cb15593b Cajova-Houba
<?php
2
3
namespace Illuminate\Queue;
4
5
use IlluminateQueueClosure;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\Queue\Console\WorkCommand;
8
use Illuminate\Queue\Console\ListenCommand;
9
use Illuminate\Queue\Console\RestartCommand;
10
use Illuminate\Queue\Connectors\SqsConnector;
11
use Illuminate\Queue\Connectors\NullConnector;
12
use Illuminate\Queue\Connectors\SyncConnector;
13
use Illuminate\Queue\Connectors\RedisConnector;
14
use Illuminate\Queue\Failed\NullFailedJobProvider;
15
use Illuminate\Queue\Connectors\DatabaseConnector;
16
use Illuminate\Queue\Connectors\BeanstalkdConnector;
17
use Illuminate\Queue\Failed\DatabaseFailedJobProvider;
18
19
class QueueServiceProvider extends ServiceProvider
20
{
21
    /**
22
     * Indicates if loading of the provider is deferred.
23
     *
24
     * @var bool
25
     */
26
    protected $defer = true;
27
28
    /**
29
     * Register the service provider.
30
     *
31
     * @return void
32
     */
33
    public function register()
34
    {
35
        $this->registerManager();
36
37
        $this->registerWorker();
38
39
        $this->registerListener();
40
41
        $this->registerFailedJobServices();
42
43
        $this->registerQueueClosure();
44
    }
45
46
    /**
47
     * Register the queue manager.
48
     *
49
     * @return void
50
     */
51
    protected function registerManager()
52
    {
53
        $this->app->singleton('queue', function ($app) {
54
            // Once we have an instance of the queue manager, we will register the various
55
            // resolvers for the queue connectors. These connectors are responsible for
56
            // creating the classes that accept queue configs and instantiate queues.
57
            $manager = new QueueManager($app);
58
59
            $this->registerConnectors($manager);
60
61
            return $manager;
62
        });
63
64
        $this->app->singleton('queue.connection', function ($app) {
65
            return $app['queue']->connection();
66
        });
67
    }
68
69
    /**
70
     * Register the queue worker.
71
     *
72
     * @return void
73
     */
74
    protected function registerWorker()
75
    {
76
        $this->registerWorkCommand();
77
78
        $this->registerRestartCommand();
79
80
        $this->app->singleton('queue.worker', function ($app) {
81
            return new Worker($app['queue'], $app['queue.failer'], $app['events']);
82
        });
83
    }
84
85
    /**
86
     * Register the queue worker console command.
87
     *
88
     * @return void
89
     */
90
    protected function registerWorkCommand()
91
    {
92
        $this->app->singleton('command.queue.work', function ($app) {
93
            return new WorkCommand($app['queue.worker']);
94
        });
95
96
        $this->commands('command.queue.work');
97
    }
98
99
    /**
100
     * Register the queue listener.
101
     *
102
     * @return void
103
     */
104
    protected function registerListener()
105
    {
106
        $this->registerListenCommand();
107
108
        $this->app->singleton('queue.listener', function ($app) {
109
            return new Listener($app->basePath());
110
        });
111
    }
112
113
    /**
114
     * Register the queue listener console command.
115
     *
116
     * @return void
117
     */
118
    protected function registerListenCommand()
119
    {
120
        $this->app->singleton('command.queue.listen', function ($app) {
121
            return new ListenCommand($app['queue.listener']);
122
        });
123
124
        $this->commands('command.queue.listen');
125
    }
126
127
    /**
128
     * Register the queue restart console command.
129
     *
130
     * @return void
131
     */
132
    public function registerRestartCommand()
133
    {
134
        $this->app->singleton('command.queue.restart', function () {
135
            return new RestartCommand;
136
        });
137
138
        $this->commands('command.queue.restart');
139
    }
140
141
    /**
142
     * Register the connectors on the queue manager.
143
     *
144
     * @param  \Illuminate\Queue\QueueManager  $manager
145
     * @return void
146
     */
147
    public function registerConnectors($manager)
148
    {
149
        foreach (['Null', 'Sync', 'Database', 'Beanstalkd', 'Redis', 'Sqs'] as $connector) {
150
            $this->{"register{$connector}Connector"}($manager);
151
        }
152
    }
153
154
    /**
155
     * Register the Null queue connector.
156
     *
157
     * @param  \Illuminate\Queue\QueueManager  $manager
158
     * @return void
159
     */
160
    protected function registerNullConnector($manager)
161
    {
162
        $manager->addConnector('null', function () {
163
            return new NullConnector;
164
        });
165
    }
166
167
    /**
168
     * Register the Sync queue connector.
169
     *
170
     * @param  \Illuminate\Queue\QueueManager  $manager
171
     * @return void
172
     */
173
    protected function registerSyncConnector($manager)
174
    {
175
        $manager->addConnector('sync', function () {
176
            return new SyncConnector;
177
        });
178
    }
179
180
    /**
181
     * Register the Beanstalkd queue connector.
182
     *
183
     * @param  \Illuminate\Queue\QueueManager  $manager
184
     * @return void
185
     */
186
    protected function registerBeanstalkdConnector($manager)
187
    {
188
        $manager->addConnector('beanstalkd', function () {
189
            return new BeanstalkdConnector;
190
        });
191
    }
192
193
    /**
194
     * Register the database queue connector.
195
     *
196
     * @param  \Illuminate\Queue\QueueManager  $manager
197
     * @return void
198
     */
199
    protected function registerDatabaseConnector($manager)
200
    {
201
        $manager->addConnector('database', function () {
202
            return new DatabaseConnector($this->app['db']);
203
        });
204
    }
205
206
    /**
207
     * Register the Redis queue connector.
208
     *
209
     * @param  \Illuminate\Queue\QueueManager  $manager
210
     * @return void
211
     */
212
    protected function registerRedisConnector($manager)
213
    {
214
        $app = $this->app;
215
216
        $manager->addConnector('redis', function () use ($app) {
217
            return new RedisConnector($app['redis']);
218
        });
219
    }
220
221
    /**
222
     * Register the Amazon SQS queue connector.
223
     *
224
     * @param  \Illuminate\Queue\QueueManager  $manager
225
     * @return void
226
     */
227
    protected function registerSqsConnector($manager)
228
    {
229
        $manager->addConnector('sqs', function () {
230
            return new SqsConnector;
231
        });
232
    }
233
234
    /**
235
     * Register the failed job services.
236
     *
237
     * @return void
238
     */
239
    protected function registerFailedJobServices()
240
    {
241
        $this->app->singleton('queue.failer', function ($app) {
242
            $config = $app['config']['queue.failed'];
243
244
            if (isset($config['table'])) {
245
                return new DatabaseFailedJobProvider($app['db'], $config['database'], $config['table']);
246
            } else {
247
                return new NullFailedJobProvider;
248
            }
249
        });
250
    }
251
252
    /**
253
     * Register the Illuminate queued closure job.
254
     *
255
     * @return void
256
     */
257
    protected function registerQueueClosure()
258
    {
259
        $this->app->singleton('IlluminateQueueClosure', function ($app) {
260
            return new IlluminateQueueClosure($app['encrypter']);
261
        });
262
    }
263
264
    /**
265
     * Get the services provided by the provider.
266
     *
267
     * @return array
268
     */
269
    public function provides()
270
    {
271
        return [
272
            'queue', 'queue.worker', 'queue.listener', 'queue.failer',
273
            'command.queue.work', 'command.queue.listen',
274
            'command.queue.restart', 'queue.connection',
275
        ];
276
    }
277
}