diff --git a/README.md b/README.md index e30a3729..7c925ada 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,48 @@ or add to the `require` section of your `composer.json` file. +## Usage with Queue utility + +1. Copy configuration file `./vendor/yiisoft/yii-queue/bin/QueueContainer.php` to `root` folder of your project. + +```shell +cp ./vendor/yiisoft/yii-queue/bin/QueueContainer.php ./ +``` + +2. Edit `./QueueContainer.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 + ], + ), + ], + ], +]; +``` + +## 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 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/queue b/bin/queue new file mode 100644 index 00000000..60d787b0 --- /dev/null +++ b/bin/queue @@ -0,0 +1,26 @@ +#!/usr/bin/env php +withDefinitions(QueueContainer::definitions()); +$container = new Container($containerConfig); + +/** @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..18a1e0b4 --- /dev/null +++ b/bin/queue.bat @@ -0,0 +1,9 @@ +@echo off + +@setlocal + +if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exe + +"%PHP_COMMAND%" "%~dp0queue" %* + +@endlocal diff --git a/composer.json b/composer.json index 73c88702..fe5d146b 100644 --- a/composer.json +++ b/composer.json @@ -40,9 +40,9 @@ "rector/rector": "^0.18.10", "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-debug": "dev-master" }, "suggest": { "ext-pcntl": "Need for process signals" @@ -57,6 +57,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 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(); diff --git a/tests/Unit/Command/ListenCommandTest.php b/tests/Unit/Command/ListenCommandTest.php index 35d4ae96..4819dde5 100644 --- a/tests/Unit/Command/ListenCommandTest.php +++ b/tests/Unit/Command/ListenCommandTest.php @@ -31,6 +31,6 @@ public function testExecute(): void $command = new ListenCommand($queueFactory); $exitCode = $command->run($input, $this->createMock(OutputInterface::class)); - $this->assertEquals(0, $exitCode); + $this->assertSame(0, $exitCode); } } diff --git a/tests/Unit/Command/RunCommandTest.php b/tests/Unit/Command/RunCommandTest.php index d4494bc2..2159fe93 100644 --- a/tests/Unit/Command/RunCommandTest.php +++ b/tests/Unit/Command/RunCommandTest.php @@ -31,6 +31,6 @@ public function testExecute(): void $command = new RunCommand($queueFactory); $exitCode = $command->run($input, $this->createMock(OutputInterface::class)); - $this->assertEquals(0, $exitCode); + $this->assertSame(0, $exitCode); } } 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); + } +}