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 12 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
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,42 @@ 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.
terabytesoftw marked this conversation as resolved.
Show resolved Hide resolved

## Usage with Symfony Console
Copy link
Member

Choose a reason for hiding this comment

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

For use with Symfony Console we should add commands to Symfony Console and do not use files from bin.

Copy link
Member Author

Choose a reason for hiding this comment

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

https://symfony.com/doc/current/components/console.html#creating-a-console-application Well in the documents it clearly says that you must register a script and add the commands, it is what is done in this PR.

Copy link
Member

Choose a reason for hiding this comment

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

We should add instruction for use commands with symfony console that alreasy used in user app.

Copy link
Member Author

@terabytesoftw terabytesoftw Nov 23, 2023

Choose a reason for hiding this comment

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

I have to investigate it, i suggest merging this PR, and do it in another.

Copy link
Member Author

Choose a reason for hiding this comment

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

https://symfony.com/doc/current/console.html#registering-the-command According to this they are already registered, and the Symfony console should recognize them.


1. Copy configuration file `./vendor/yiisoft/yii-queue/bin/definitions.php` to `root` folder of your project.

```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
],
),
],
],
];
```

## 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
74 changes: 74 additions & 0 deletions bin/definitions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

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;

return [
Application::class => [
'__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,
];
29 changes: 29 additions & 0 deletions bin/queue
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env php
<?php

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';
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
$autoload = $GLOBALS['_composer_autoload_path'] ?? './vendor/autoload.php';
$autoload = $_composer_autoload_path ?? dirname(__DIR__, 3) . '/vendor/autoload.php';

https://getcomposer.org/doc/articles/vendor-binaries.md#finding-the-composer-autoloader-from-a-binary


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';
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
$definitions = require_once 'definitions.php';
$definitions = require_once __DIR__ . 'definitions.php';

Copy link
Member Author

@terabytesoftw terabytesoftw Nov 23, 2023

Choose a reason for hiding this comment

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

If you use the absolute path, when you copy the definitions to the ROOT Directory of the app, not work.

Copy link
Member

Choose a reason for hiding this comment

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

Not need copy definitions.

Copy link
Member Author

@terabytesoftw terabytesoftw Nov 23, 2023

Choose a reason for hiding this comment

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

If necessary, suppose that i have a different configuration than the predetermined, for example, i used monolog, when updating new version will lose my changes, that is fine, only when your configuration is equal to the predetermine


$containerConfig = ContainerConfig::create()->withDefinitions($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
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"php": "^8.0",
"psr/log": "^2.0|^3.0",
"psr/container": "^1.0|^2.0",
"yiisoft/yii-console": "^2.0",
"yiisoft/definitions": "^1.0|^2.0|^3.0",
"yiisoft/friendly-exception": "^1.0",
"yiisoft/injector": "^1.0",
Expand All @@ -41,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 @@ -58,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
3 changes: 1 addition & 2 deletions src/Command/ListenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -38,6 +37,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
->get($input->getArgument('channel'))
->listen();

return ExitCode::OK;
return 0;
}
}
3 changes: 1 addition & 2 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -38,6 +37,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
->get($input->getArgument('channel'))
->run();

return ExitCode::OK;
return 0;
}
}
3 changes: 1 addition & 2 deletions tests/Unit/Command/ListenCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
3 changes: 1 addition & 2 deletions tests/Unit/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
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