From 9289565f075cea59d9e2cbba01229fe7c12b9945 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Tue, 21 Nov 2023 12:27:24 -0300 Subject: [PATCH 01/14] Use `yiisoft/yii-console` with optional. --- README.md | 32 +++++++++++++++++++++++++++++++ bin/definitions.php | 25 ++++++++++++++++++++++++ bin/queue | 36 +++++++++++++++++++++++++++++++++++ bin/queue.bat | 11 +++++++++++ composer.json | 10 +++++++--- src/Command/ListenCommand.php | 3 +-- src/Command/RunCommand.php | 3 +-- 7 files changed, 113 insertions(+), 7 deletions(-) create mode 100644 bin/definitions.php create mode 100644 bin/queue create mode 100644 bin/queue.bat diff --git a/README.md b/README.md index e30a3729..99443477 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,38 @@ or add to the `require` section of your `composer.json` file. +## Usage with Yii Console + +Just install `yiisoft/yii-console` package and you are ready to go. + +## Usage with Symfony Console + +To use this package without the Yii Framework, it is quite simple. You only need to add the configuration of your chosen logger, +example with [yiisoft/log-target-file](https://github.com/yiisoft/log-target-file). + +./bin/definitions.php + +```php + LoggerInterface::class => [ + 'class' => Logger::class, + '__construct()' => [ + 'targets' => ReferencesArray::from([ + FileTarget::class, + ]), + ], + ], +``` + +And in .config/params.php add config for aliases for the logger. + +```php + 'yiisoft/aliases' => [ + 'aliases' => [ + '@runtime' => your_runtime_path, + ], + ], +``` + ## Ready for yiisoft/config If you are using [yiisoft/config](https://github.com/yiisoft/config), you'll find out this package has some defaults diff --git a/bin/definitions.php b/bin/definitions.php new file mode 100644 index 00000000..d92a56ba --- /dev/null +++ b/bin/definitions.php @@ -0,0 +1,25 @@ + [ + '__construct()' => [ + 'name' => 'Yii Queue Tool', + 'version' => '1.0.0', + ], + 'addCommands()' => [ + ReferencesArray::from( + [ + RunCommand::class, + ListenCommand::class, + ], + ), + ], + ], +]; diff --git a/bin/queue b/bin/queue new file mode 100644 index 00000000..ca76a7b6 --- /dev/null +++ b/bin/queue @@ -0,0 +1,36 @@ +#!/usr/bin/env php +get('di')); +$containerConfig = ContainerConfig::create()->withDefinitions($definitions); +$container = new Container($containerConfig); + +if ($container->has(LoggerInterface::class) === false) { + throw new RuntimeException( + 'Logger is not configured, install your logger of choice and configure it properly in /bin/definitions.php.' + ); +} + +/** @var Application $application */ +$application = $container->get(Application::class); +$application->run(); diff --git a/bin/queue.bat b/bin/queue.bat new file mode 100644 index 00000000..ff8bbf16 --- /dev/null +++ b/bin/queue.bat @@ -0,0 +1,11 @@ +@echo off + +@setlocal + +set YII_PATH=%~dp0 + +if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exe + +"%PHP_COMMAND%" "%YII_PATH%queue" %* + +@endlocal diff --git a/composer.json b/composer.json index 01a13a5a..07718a59 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,7 @@ "php": "^8.0", "psr/log": "^2.0|^3.0", "psr/container": "^1.0|^2.0", - "yiisoft/yii-console": "^2.0", + "yiisoft/config": "^1.4", "yiisoft/definitions": "^1.0|^2.0|^3.0", "yiisoft/friendly-exception": "^1.0", "yiisoft/injector": "^1.0", @@ -40,9 +40,10 @@ "phpunit/phpunit": "^9.5", "roave/infection-static-analysis-plugin": "^1.16", "spatie/phpunit-watcher": "^1.23", - "yiisoft/yii-debug": "dev-master", "vimeo/psalm": "^4.30|^5.8", - "yiisoft/test-support": "^3.0" + "yiisoft/test-support": "^3.0", + "yiisoft/yii-console": "^2.0", + "yiisoft/yii-debug": "dev-master" }, "suggest": { "ext-pcntl": "Need for process signals" @@ -57,6 +58,9 @@ "Yiisoft\\Yii\\Queue\\Tests\\": "tests" } }, + "bin": [ + "bin/queue" + ], "extra": { "branch-alias": { "dev-master": "3.0.x-dev" diff --git a/src/Command/ListenCommand.php b/src/Command/ListenCommand.php index bc71544f..f3d172fe 100644 --- a/src/Command/ListenCommand.php +++ b/src/Command/ListenCommand.php @@ -8,7 +8,6 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Yiisoft\Yii\Console\ExitCode; use Yiisoft\Yii\Queue\QueueFactory; use Yiisoft\Yii\Queue\QueueFactoryInterface; @@ -41,6 +40,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int ->get($input->getArgument('channel')) ->listen(); - return ExitCode::OK; + return 0; } } diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index c1c4638f..54bd5111 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -8,7 +8,6 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Yiisoft\Yii\Console\ExitCode; use Yiisoft\Yii\Queue\QueueFactory; use Yiisoft\Yii\Queue\QueueFactoryInterface; @@ -41,6 +40,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int ->get($input->getArgument('channel')) ->run(); - return ExitCode::OK; + return 0; } } From e1f76cddafdaed363664dee78c905bea7ba22841 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 22 Nov 2023 07:35:43 -0300 Subject: [PATCH 02/14] Remove `yiisoft/config`. --- README.md | 36 +++++++++++++++++++---------------- bin/definitions.php | 46 +++++++++++++++++++++++++++++++++++++++++++++ bin/queue | 9 --------- composer.json | 1 - 4 files changed, 66 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 99443477..61b55228 100644 --- a/README.md +++ b/README.md @@ -43,30 +43,34 @@ Just install `yiisoft/yii-console` package and you are ready to go. ## Usage with Symfony Console -To use this package without the Yii Framework, it is quite simple. You only need to add the configuration of your chosen logger, -example with [yiisoft/log-target-file](https://github.com/yiisoft/log-target-file). +1. Copy configuration file `./vendor/yiisoft/yii-queue/bin/definitions.php` to `root` folder of your project. -./bin/definitions.php +```shell +cp ./vendor/yiisoft/yii-queue/bin/definitions.php ./ +``` + +2. Edit `./definitions.php` and add definitions for your logger and queue adapter. + +Here's a sample configuration. ```php +use Psr\Log\LoggerInterface; +use Yiisoft\Definitions\ReferencesArray; +use Yiisoft\Log\Logger; +use Yiisoft\Log\Target\File\FileTarget; + +return [ LoggerInterface::class => [ 'class' => Logger::class, '__construct()' => [ - 'targets' => ReferencesArray::from([ - FileTarget::class, - ]), - ], - ], -``` - -And in .config/params.php add config for aliases for the logger. - -```php - 'yiisoft/aliases' => [ - 'aliases' => [ - '@runtime' => your_runtime_path, + 'targets' => ReferencesArray::from( + [ + FileTarget::class + ], + ), ], ], +]; ``` ## Ready for yiisoft/config diff --git a/bin/definitions.php b/bin/definitions.php index d92a56ba..ac569fb2 100644 --- a/bin/definitions.php +++ b/bin/definitions.php @@ -2,10 +2,29 @@ declare(strict_types=1); +use Psr\Container\ContainerInterface; use Symfony\Component\Console\Application; use Yiisoft\Definitions\ReferencesArray; +use Yiisoft\Yii\Queue\Cli\LoopInterface; +use Yiisoft\Yii\Queue\Cli\SignalLoop; +use Yiisoft\Yii\Queue\Cli\SimpleLoop; use Yiisoft\Yii\Queue\Command\ListenCommand; use Yiisoft\Yii\Queue\Command\RunCommand; +use Yiisoft\Yii\Queue\Middleware\Consume\ConsumeMiddlewareDispatcher; +use Yiisoft\Yii\Queue\Middleware\Consume\MiddlewareFactoryConsume; +use Yiisoft\Yii\Queue\Middleware\Consume\MiddlewareFactoryConsumeInterface; +use Yiisoft\Yii\Queue\Middleware\FailureHandling\FailureMiddlewareDispatcher; +use Yiisoft\Yii\Queue\Middleware\FailureHandling\MiddlewareFactoryFailure; +use Yiisoft\Yii\Queue\Middleware\FailureHandling\MiddlewareFactoryFailureInterface; +use Yiisoft\Yii\Queue\Middleware\Push\MiddlewareFactoryPush; +use Yiisoft\Yii\Queue\Middleware\Push\MiddlewareFactoryPushInterface; +use Yiisoft\Yii\Queue\Middleware\Push\PushMiddlewareDispatcher; +use Yiisoft\Yii\Queue\Queue; +use Yiisoft\Yii\Queue\QueueFactory; +use Yiisoft\Yii\Queue\QueueFactoryInterface; +use Yiisoft\Yii\Queue\QueueInterface; +use Yiisoft\Yii\Queue\Worker\Worker as QueueWorker; +use Yiisoft\Yii\Queue\Worker\WorkerInterface; return [ Application::class => [ @@ -22,4 +41,31 @@ ), ], ], + 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' => []], + ], ]; diff --git a/bin/queue b/bin/queue index ca76a7b6..792017f3 100644 --- a/bin/queue +++ b/bin/queue @@ -5,9 +5,6 @@ declare(strict_types=1); use Psr\Log\LoggerInterface; use Symfony\Component\Console\Application; -use Yiisoft\Config\Config; -use Yiisoft\Config\ConfigPaths; -use Yiisoft\Config\Modifier\RecursiveMerge; use Yiisoft\Di\Container; use Yiisoft\Di\ContainerConfig; @@ -16,12 +13,6 @@ require_once './vendor/autoload.php'; /** @var array $definitions */ $definitions = require_once 'definitions.php'; -$config = new Config( - new ConfigPaths('.', 'config', 'vendor'), - modifiers: [RecursiveMerge::groups('di', 'params')], - paramsGroup: 'params', -); -$definitions = array_merge($definitions, $config->get('di')); $containerConfig = ContainerConfig::create()->withDefinitions($definitions); $container = new Container($containerConfig); diff --git a/composer.json b/composer.json index 07718a59..cc3d5911 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,6 @@ "php": "^8.0", "psr/log": "^2.0|^3.0", "psr/container": "^1.0|^2.0", - "yiisoft/config": "^1.4", "yiisoft/definitions": "^1.0|^2.0|^3.0", "yiisoft/friendly-exception": "^1.0", "yiisoft/injector": "^1.0", From 239da6c1be0514db062bf43917b7303436a54dc6 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 22 Nov 2023 12:51:36 -0300 Subject: [PATCH 03/14] Remove `yiisoft/yii-console` in tests. --- composer.json | 1 - tests/Unit/Command/ListenCommandTest.php | 3 +-- tests/Unit/Command/RunCommandTest.php | 3 +-- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index da350d6b..fe5d146b 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,6 @@ "spatie/phpunit-watcher": "^1.23", "vimeo/psalm": "^4.30|^5.8", "yiisoft/test-support": "^3.0", - "yiisoft/yii-console": "^2.0", "yiisoft/yii-debug": "dev-master" }, "suggest": { diff --git a/tests/Unit/Command/ListenCommandTest.php b/tests/Unit/Command/ListenCommandTest.php index 47393688..4819dde5 100644 --- a/tests/Unit/Command/ListenCommandTest.php +++ b/tests/Unit/Command/ListenCommandTest.php @@ -7,7 +7,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\OutputInterface; -use Yiisoft\Yii\Console\ExitCode; use Yiisoft\Yii\Queue\Command\ListenCommand; use Yiisoft\Yii\Queue\QueueFactoryInterface; use Yiisoft\Yii\Queue\QueueInterface; @@ -32,6 +31,6 @@ public function testExecute(): void $command = new ListenCommand($queueFactory); $exitCode = $command->run($input, $this->createMock(OutputInterface::class)); - $this->assertEquals(ExitCode::OK, $exitCode); + $this->assertSame(0, $exitCode); } } diff --git a/tests/Unit/Command/RunCommandTest.php b/tests/Unit/Command/RunCommandTest.php index a6db12e3..2159fe93 100644 --- a/tests/Unit/Command/RunCommandTest.php +++ b/tests/Unit/Command/RunCommandTest.php @@ -7,7 +7,6 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\OutputInterface; -use Yiisoft\Yii\Console\ExitCode; use Yiisoft\Yii\Queue\Command\RunCommand; use Yiisoft\Yii\Queue\QueueFactoryInterface; use Yiisoft\Yii\Queue\QueueInterface; @@ -32,6 +31,6 @@ public function testExecute(): void $command = new RunCommand($queueFactory); $exitCode = $command->run($input, $this->createMock(OutputInterface::class)); - $this->assertEquals(ExitCode::OK, $exitCode); + $this->assertSame(0, $exitCode); } } From ffb4cabfc1d6706d9c2dcf70c09ebbbf8bb254b8 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 22 Nov 2023 12:57:27 -0300 Subject: [PATCH 04/14] Remove `YII_PATH`. --- bin/queue.bat | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bin/queue.bat b/bin/queue.bat index ff8bbf16..18a1e0b4 100644 --- a/bin/queue.bat +++ b/bin/queue.bat @@ -2,10 +2,8 @@ @setlocal -set YII_PATH=%~dp0 - if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exe -"%PHP_COMMAND%" "%YII_PATH%queue" %* +"%PHP_COMMAND%" "%~dp0queue" %* @endlocal From 3ee2b20109ddcfddb5357e481f30765d87b832fd Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 22 Nov 2023 13:32:44 -0300 Subject: [PATCH 05/14] Use `NullLogger:.class` for default. --- bin/definitions.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bin/definitions.php b/bin/definitions.php index ac569fb2..75afebb1 100644 --- a/bin/definitions.php +++ b/bin/definitions.php @@ -3,6 +3,8 @@ declare(strict_types=1); use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; use Symfony\Component\Console\Application; use Yiisoft\Definitions\ReferencesArray; use Yiisoft\Yii\Queue\Cli\LoopInterface; @@ -68,4 +70,5 @@ FailureMiddlewareDispatcher::class => [ '__construct()' => ['middlewareDefinitions' => []], ], + LoggerInterface::class => NullLogger::class, ]; From 81fc0d20e8558b6f058e0914b337dc18cf5cbd9d Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 22 Nov 2023 13:42:34 -0300 Subject: [PATCH 06/14] Allow running the command from any directory. --- bin/queue | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/bin/queue b/bin/queue index 792017f3..49dba824 100644 --- a/bin/queue +++ b/bin/queue @@ -8,7 +8,27 @@ use Symfony\Component\Console\Application; use Yiisoft\Di\Container; use Yiisoft\Di\ContainerConfig; -require_once './vendor/autoload.php'; +$vendorPath = null; + +$composerAutoload = [ + __DIR__ . '/../vendor/autoload.php', // in yii2-dev repo + __DIR__ . '/../../autoload.php', // installed as a composer binary +]; + +foreach ($composerAutoload as $autoload) { + if (file_exists($autoload)) { + require_once $autoload; + + $vendorPath = dirname($autoload); + + break; + } +} + +if ($vendorPath === null) { + fwrite(STDERR, 'Can not find vendor/autoload.php' . PHP_EOL); + exit(1); +} /** @var array $definitions */ $definitions = require_once 'definitions.php'; From d6563e778c3c15faf57912e2a4b8badf09c0ad50 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 22 Nov 2023 14:26:25 -0300 Subject: [PATCH 07/14] Add simple tests. --- tests/Unit/ConsoleQueueTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/Unit/ConsoleQueueTest.php diff --git a/tests/Unit/ConsoleQueueTest.php b/tests/Unit/ConsoleQueueTest.php new file mode 100644 index 00000000..db92e73c --- /dev/null +++ b/tests/Unit/ConsoleQueueTest.php @@ -0,0 +1,17 @@ +assertStringContainsString('Yii Queue Tool 1.0.0', $output); + } +} From 3f7b78c091eef6dc2aa516ac3882d721552d81a2 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 22 Nov 2023 14:35:58 -0300 Subject: [PATCH 08/14] Fix script again. --- bin/queue | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/bin/queue b/bin/queue index 49dba824..792017f3 100644 --- a/bin/queue +++ b/bin/queue @@ -8,27 +8,7 @@ use Symfony\Component\Console\Application; use Yiisoft\Di\Container; use Yiisoft\Di\ContainerConfig; -$vendorPath = null; - -$composerAutoload = [ - __DIR__ . '/../vendor/autoload.php', // in yii2-dev repo - __DIR__ . '/../../autoload.php', // installed as a composer binary -]; - -foreach ($composerAutoload as $autoload) { - if (file_exists($autoload)) { - require_once $autoload; - - $vendorPath = dirname($autoload); - - break; - } -} - -if ($vendorPath === null) { - fwrite(STDERR, 'Can not find vendor/autoload.php' . PHP_EOL); - exit(1); -} +require_once './vendor/autoload.php'; /** @var array $definitions */ $definitions = require_once 'definitions.php'; From 5216463275845a527e6eba66612a819b86de452c Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 22 Nov 2023 14:58:08 -0300 Subject: [PATCH 09/14] Fix run any directory, --- bin/queue | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bin/queue b/bin/queue index 792017f3..cd3b9cfa 100644 --- a/bin/queue +++ b/bin/queue @@ -8,7 +8,15 @@ use Symfony\Component\Console\Application; use Yiisoft\Di\Container; use Yiisoft\Di\ContainerConfig; -require_once './vendor/autoload.php'; +$autoload = $GLOBALS['_composer_autoload_path'] ?? './vendor/autoload.php'; + +if (!file_exists($autoload)) { + throw new RuntimeException( + 'Please run "composer install" in your project root to install the required dependencies.' + ); +} + +require_once $autoload; /** @var array $definitions */ $definitions = require_once 'definitions.php'; From 2fe344d1e3f025d36a8cdf0b3f96bff3e76d0b64 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 22 Nov 2023 15:09:44 -0300 Subject: [PATCH 10/14] Remove check logger. --- bin/queue | 6 ------ 1 file changed, 6 deletions(-) diff --git a/bin/queue b/bin/queue index cd3b9cfa..ed707523 100644 --- a/bin/queue +++ b/bin/queue @@ -24,12 +24,6 @@ $definitions = require_once 'definitions.php'; $containerConfig = ContainerConfig::create()->withDefinitions($definitions); $container = new Container($containerConfig); -if ($container->has(LoggerInterface::class) === false) { - throw new RuntimeException( - 'Logger is not configured, install your logger of choice and configure it properly in /bin/definitions.php.' - ); -} - /** @var Application $application */ $application = $container->get(Application::class); $application->run(); From c854a147d84517f97b9f51c4b9a05d58e7e100b6 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Thu, 23 Nov 2023 13:00:58 -0300 Subject: [PATCH 11/14] Apply suggest review. --- README.md | 2 +- bin/queue | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 61b55228..27be8492 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ to the `require` section of your `composer.json` file. ## Usage with Yii Console -Just install `yiisoft/yii-console` package and you are ready to go. +Install `yiisoft/yii-console` package and you are ready to go. ## Usage with Symfony Console diff --git a/bin/queue b/bin/queue index ed707523..14d43367 100644 --- a/bin/queue +++ b/bin/queue @@ -3,12 +3,11 @@ declare(strict_types=1); -use Psr\Log\LoggerInterface; use Symfony\Component\Console\Application; use Yiisoft\Di\Container; use Yiisoft\Di\ContainerConfig; -$autoload = $GLOBALS['_composer_autoload_path'] ?? './vendor/autoload.php'; +$autoload = $_composer_autoload_path ?? './vendor/autoload.php'; if (!file_exists($autoload)) { throw new RuntimeException( From 73df35d7309edf3162602f1938a99b26863ee99a Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Thu, 23 Nov 2023 15:19:55 -0300 Subject: [PATCH 12/14] Uase attribute for register commands. --- src/Command/ListenCommand.php | 5 ++--- src/Command/RunCommand.php | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Command/ListenCommand.php b/src/Command/ListenCommand.php index 6851a9f8..17f965d1 100644 --- a/src/Command/ListenCommand.php +++ b/src/Command/ListenCommand.php @@ -4,6 +4,7 @@ namespace Yiisoft\Yii\Queue\Command; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -11,11 +12,9 @@ use Yiisoft\Yii\Queue\QueueFactory; use Yiisoft\Yii\Queue\QueueFactoryInterface; +#[AsCommand('queue:listen', 'Listens the queue and executes messages as they come. Needs to be stopped manually.')] final class ListenCommand extends Command { - protected static $defaultName = 'queue:listen'; - protected static $defaultDescription = 'Listens the queue and executes messages as they come. Needs to be stopped manually.'; - public function __construct(private QueueFactoryInterface $queueFactory) { parent::__construct(); diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index bdc848ff..643fdbac 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -4,6 +4,7 @@ namespace Yiisoft\Yii\Queue\Command; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -11,11 +12,9 @@ use Yiisoft\Yii\Queue\QueueFactory; use Yiisoft\Yii\Queue\QueueFactoryInterface; +#[AsCommand('queue:run', 'Runs all the existing messages in the queue. Exits once messages are over.')] final class RunCommand extends Command { - protected static $defaultName = 'queue:run'; - protected static $defaultDescription = 'Runs all the existing messages in the queue. Exits once messages are over.'; - public function __construct(private QueueFactoryInterface $queueFactory) { parent::__construct(); From ed418eb190288b6c20f3e63018f7e088d90c9ba4 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Thu, 23 Nov 2023 15:41:33 -0300 Subject: [PATCH 13/14] 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 */ From 5d77c411d000938fb908a3bc1ed7c707c8d42bb1 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Thu, 23 Nov 2023 16:24:02 -0300 Subject: [PATCH 14/14] Update `README.md`. --- README.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a6f9be65..7c925ada 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,7 @@ or add to the `require` section of your `composer.json` file. -## Usage with Yii Console - -Install `yiisoft/yii-console` package and you are ready to go. - -## Usage with Symfony Console +## Usage with Queue utility 1. Copy configuration file `./vendor/yiisoft/yii-queue/bin/QueueContainer.php` to `root` folder of your project. @@ -73,6 +69,16 @@ return [ ]; ``` +## Usage with Yii Console + +Install `yiisoft/yii-console` package and you are ready to go. + +## Usage with Symfony Console + +Install `yiisoft/yii-queue` and the commands are automatically registered by the symfony console. + +> https://symfony.com/doc/current/console.html#registering-the-command + ## Ready for yiisoft/config If you are using [yiisoft/config](https://github.com/yiisoft/config), you'll find out this package has some defaults