Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use yiisoft/yii-console with optional. #178

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest require the user to create yii-queue-params.php in root directory with queue params only and don't require copy full container configuration. It's more cleary...


```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
Expand Down
83 changes: 83 additions & 0 deletions bin/QueueContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emm, why do we need it?
Isn't it better to use all users' configs instead?


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;
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;

final class QueueContainer
{
public static function definitions(): array
{
return [
Application::class => [
'__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,
]
);
}
}
26 changes: 26 additions & 0 deletions bin/queue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

use Symfony\Component\Console\Application;
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$autoload = $_composer_autoload_path ?? './vendor/autoload.php';

if (!file_exists($autoload)) {
throw new RuntimeException(
vjik marked this conversation as resolved.
Show resolved Hide resolved
'Please run "composer install" in your project root to install the required dependencies.'
);
}

require_once $autoload;
require_once 'QueueContainer.php';

$containerConfig = ContainerConfig::create()->withDefinitions(QueueContainer::definitions());
$container = new Container($containerConfig);

/** @var Application $application */
$application = $container->get(Application::class);
$application->run();
9 changes: 9 additions & 0 deletions bin/queue.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@echo off

@setlocal

if "%PHP_COMMAND%" == "" set PHP_COMMAND=php.exe

"%PHP_COMMAND%" "%~dp0queue" %*

@endlocal
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -57,6 +57,9 @@
"Yiisoft\\Yii\\Queue\\Tests\\": "tests"
}
},
"bin": [
"bin/queue"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"bin/queue"
"bin/yii-queue"

It's global space, I suggest use prefix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea when removing the console is to rename the package to yiisoft/queue, so queue makes sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest rename utility only. Because it is global namespace

],
"extra": {
"branch-alias": {
"dev-master": "3.0.x-dev"
Expand Down
5 changes: 2 additions & 3 deletions src/Command/ListenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

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;
use Symfony\Component\Console\Output\OutputInterface;
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();
Expand Down
5 changes: 2 additions & 3 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@

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;
use Symfony\Component\Console\Output\OutputInterface;
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();
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Command/ListenCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
17 changes: 17 additions & 0 deletions tests/Unit/ConsoleQueueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Tests\Unit;

use PHPUnit\Framework\TestCase;

final class ConsoleQueueTest extends TestCase
{
public function testQueueRunsSuccessfully(): void
{
$output = shell_exec('php ./bin/queue');

$this->assertStringContainsString('Yii Queue Tool 1.0.0', $output);
}
}
Loading