<?php
namespace Illuminate\Queue;
use IlluminateQueueClosure;
use Illuminate\Support\ServiceProvider;
use Illuminate\Queue\Console\WorkCommand;
use Illuminate\Queue\Console\ListenCommand;
use Illuminate\Queue\Console\RestartCommand;
use Illuminate\Queue\Connectors\SqsConnector;
use Illuminate\Queue\Connectors\NullConnector;
use Illuminate\Queue\Connectors\SyncConnector;
use Illuminate\Queue\Connectors\RedisConnector;
use Illuminate\Queue\Failed\NullFailedJobProvider;
use Illuminate\Queue\Connectors\DatabaseConnector;
use Illuminate\Queue\Connectors\BeanstalkdConnector;
use Illuminate\Queue\Failed\DatabaseFailedJobProvider;
class QueueServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
* 指示是否延迟加载提供程序。
* @var bool
*/
protected $defer = true;
/**
* Register the service provider.
* 注册服务提供商。
* @return void
*/
public function register()
{
$this->registerManager();
$this->registerWorker();
$this->registerListener();
$this->registerFailedJobServices();
$this->registerQueueClosure();
}
/**
* Register the queue manager.
* 注册队列管理器。
* @return void
*/
protected function registerManager()
{
$this->app->singleton('queue', function ($app) {
// 一旦我们有了队列管理器的实例,我们将为队列连接器注册各种解析器。 这些连接器负责创建接受队列配置和实例化队列的类。
$manager = new QueueManager($app);
$this->registerConnectors($manager);
return $manager;
});
$this->app->singleton('queue.connection', function ($app) {
return $app['queue']->connection();
});
}
/**
* Register the queue worker.
* 注册队列工作者。
* @return void
*/
protected function registerWorker()
{
$this->registerWorkCommand();
$this->registerRestartCommand();
$this->app->singleton('queue.worker', function ($app) {
return new Worker($app['queue'], $app['queue.failer'], $app['events']);
});
}
/**
* Register the queue worker console command.
* 注册队列工作器控制台命令。
* @return void
*/
protected function registerWorkCommand()
{
$this->app->singleton('command.queue.work', function ($app) {
return new WorkCommand($app['queue.worker']);
});
$this->commands('command.queue.work');
}
/**
* Register the queue listener.
* 注册队列侦听器。
* @return void
*/
protected function registerListener()
{
$this->registerListenCommand();
$this->app->singleton('queue.listener', function ($app) {
return new Listener($app->basePath());
});
}
/**
* Register the queue listener console command.
* 注册队列侦听器控制台命令。
* @return void
*/
protected function registerListenCommand()
{
$this->app->singleton('command.queue.listen', function ($app) {
return new ListenCommand($app['queue.listener']);
});
$this->commands('command.queue.listen');
}
/**
* Register the queue restart console command.
* 注册队列重启控制台命令。
* @return void
*/
public function registerRestartCommand()
{
$this->app->singleton('command.queue.restart', function () {
return new RestartCommand;
});
$this->commands('command.queue.restart');
}
/**
* Register the connectors on the queue manager.
* 在队列管理器上注册连接器。
* @param \Illuminate\Queue\QueueManager $manager
* @return void
*/
public function registerConnectors($manager)
{
foreach (['Null', 'Sync', 'Database', 'Beanstalkd', 'Redis', 'Sqs'] as $connector) {
$this->{"register{$connector}Connector"}($manager);
}
}
/**
* Register the Null queue connector.
* 注册空队列连接器。
* @param \Illuminate\Queue\QueueManager $manager
* @return void
*/
protected function registerNullConnector($manager)
{
$manager->addConnector('null', function () {
return new NullConnector;
});
}
/**
* Register the Sync queue connector.
* 注册同步队列连接器。
* @param \Illuminate\Queue\QueueManager $manager
* @return void
*/
protected function registerSyncConnector($manager)
{
$manager->addConnector('sync', function () {
return new SyncConnector;
});
}
/**
* Register the Beanstalkd queue connector.
* 注册 Beanstalkd 队列连接器。
* @param \Illuminate\Queue\QueueManager $manager
* @return void
*/
protected function registerBeanstalkdConnector($manager)
{
$manager->addConnector('beanstalkd', function () {
return new BeanstalkdConnector;
});
}
/**
* Register the database queue connector.
* 注册数据库队列连接器。
* @param \Illuminate\Queue\QueueManager $manager
* @return void
*/
protected function registerDatabaseConnector($manager)
{
$manager->addConnector('database', function () {
return new DatabaseConnector($this->app['db']);
});
}
/**
* Register the Redis queue connector.
* 注册 Redis 队列连接器。
* @param \Illuminate\Queue\QueueManager $manager
* @return void
*/
protected function registerRedisConnector($manager)
{
$app = $this->app;
$manager->addConnector('redis', function () use ($app) {
return new RedisConnector($app['redis']);
});
}
/**
* Register the Amazon SQS queue connector.
* 注册 Amazon SQS 队列连接器。
* @param \Illuminate\Queue\QueueManager $manager
* @return void
*/
protected function registerSqsConnector($manager)
{
$manager->addConnector('sqs', function () {
return new SqsConnector;
});
}
/**
* Register the failed job services.
* 注册失败的作业服务。
* @return void
*/
protected function registerFailedJobServices()
{
$this->app->singleton('queue.failer', function ($app) {
$config = $app['config']['queue.failed'];
if (isset($config['table'])) {
return new DatabaseFailedJobProvider($app['db'], $config['database'], $config['table']);
} else {
return new NullFailedJobProvider;
}
});
}
/**
* Register the Illuminate queued closure job.
* 注册 Illuminate 排队关闭作业。
* @return void
*/
protected function registerQueueClosure()
{
$this->app->singleton('IlluminateQueueClosure', function ($app) {
return new IlluminateQueueClosure($app['encrypter']);
});
}
/**
* Get the services provided by the provider.
* 获取提供者提供的服务。
* @return array
*/
public function provides()
{
return [
'queue', 'queue.worker', 'queue.listener', 'queue.failer',
'command.queue.work', 'command.queue.listen',
'command.queue.restart', 'queue.connection',
];
}
}
|