From ed418eb190288b6c20f3e63018f7e088d90c9ba4 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Thu, 23 Nov 2023 15:41:33 -0300 Subject: [PATCH] Improve config. --- README.md | 6 +-- bin/QueueContainer.php | 83 ++++++++++++++++++++++++++++++++++++++++++ bin/definitions.php | 74 ------------------------------------- bin/queue | 6 +-- 4 files changed, 88 insertions(+), 81 deletions(-) create mode 100644 bin/QueueContainer.php delete mode 100644 bin/definitions.php diff --git a/README.md b/README.md index 27be8492..a6f9be65 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,13 @@ Install `yiisoft/yii-console` package and you are ready to go. ## Usage with Symfony Console -1. Copy configuration file `./vendor/yiisoft/yii-queue/bin/definitions.php` to `root` folder of your project. +1. Copy configuration file `./vendor/yiisoft/yii-queue/bin/QueueContainer.php` to `root` folder of your project. ```shell -cp ./vendor/yiisoft/yii-queue/bin/definitions.php ./ +cp ./vendor/yiisoft/yii-queue/bin/QueueContainer.php ./ ``` -2. Edit `./definitions.php` and add definitions for your logger and queue adapter. +2. Edit `./QueueContainer.php` and add definitions for your logger and queue adapter. Here's a sample configuration. diff --git a/bin/QueueContainer.php b/bin/QueueContainer.php new file mode 100644 index 00000000..108a5b2b --- /dev/null +++ b/bin/QueueContainer.php @@ -0,0 +1,83 @@ + [ + '__construct()' => [ + 'name' => 'Yii Queue Tool', + 'version' => '1.0.0', + ], + 'addCommands()' => [self::getCommands()], + ], + QueueWorker::class => [ + 'class' => QueueWorker::class, + '__construct()' => [[]], + ], + WorkerInterface::class => QueueWorker::class, + LoopInterface::class => static function (ContainerInterface $container): LoopInterface { + return extension_loaded('pcntl') + ? $container->get(SignalLoop::class) + : $container->get(SimpleLoop::class); + }, + QueueFactoryInterface::class => QueueFactory::class, + QueueFactory::class => [ + '__construct()' => [[]], + ], + QueueInterface::class => Queue::class, + MiddlewareFactoryPushInterface::class => MiddlewareFactoryPush::class, + MiddlewareFactoryConsumeInterface::class => MiddlewareFactoryConsume::class, + MiddlewareFactoryFailureInterface::class => MiddlewareFactoryFailure::class, + PushMiddlewareDispatcher::class => [ + '__construct()' => ['middlewareDefinitions' => []], + ], + ConsumeMiddlewareDispatcher::class => [ + '__construct()' => ['middlewareDefinitions' => []], + ], + FailureMiddlewareDispatcher::class => [ + '__construct()' => ['middlewareDefinitions' => []], + ], + LoggerInterface::class => NullLogger::class, + ]; + } + + public static function getCommands(): array + { + return ReferencesArray::from( + [ + RunCommand::class, + ListenCommand::class, + ] + ); + } +} diff --git a/bin/definitions.php b/bin/definitions.php deleted file mode 100644 index 75afebb1..00000000 --- a/bin/definitions.php +++ /dev/null @@ -1,74 +0,0 @@ - [ - '__construct()' => [ - 'name' => 'Yii Queue Tool', - 'version' => '1.0.0', - ], - 'addCommands()' => [ - ReferencesArray::from( - [ - RunCommand::class, - ListenCommand::class, - ], - ), - ], - ], - QueueWorker::class => [ - 'class' => QueueWorker::class, - '__construct()' => [[]], - ], - WorkerInterface::class => QueueWorker::class, - LoopInterface::class => static function (ContainerInterface $container): LoopInterface { - return extension_loaded('pcntl') - ? $container->get(SignalLoop::class) - : $container->get(SimpleLoop::class); - }, - QueueFactoryInterface::class => QueueFactory::class, - QueueFactory::class => [ - '__construct()' => [[]], - ], - QueueInterface::class => Queue::class, - MiddlewareFactoryPushInterface::class => MiddlewareFactoryPush::class, - MiddlewareFactoryConsumeInterface::class => MiddlewareFactoryConsume::class, - MiddlewareFactoryFailureInterface::class => MiddlewareFactoryFailure::class, - PushMiddlewareDispatcher::class => [ - '__construct()' => ['middlewareDefinitions' => []], - ], - ConsumeMiddlewareDispatcher::class => [ - '__construct()' => ['middlewareDefinitions' => []], - ], - FailureMiddlewareDispatcher::class => [ - '__construct()' => ['middlewareDefinitions' => []], - ], - LoggerInterface::class => NullLogger::class, -]; diff --git a/bin/queue b/bin/queue index 14d43367..60d787b0 100644 --- a/bin/queue +++ b/bin/queue @@ -16,11 +16,9 @@ if (!file_exists($autoload)) { } require_once $autoload; +require_once 'QueueContainer.php'; -/** @var array $definitions */ -$definitions = require_once 'definitions.php'; - -$containerConfig = ContainerConfig::create()->withDefinitions($definitions); +$containerConfig = ContainerConfig::create()->withDefinitions(QueueContainer::definitions()); $container = new Container($containerConfig); /** @var Application $application */