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

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 => [
'__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' => []],
],
];
27 changes: 27 additions & 0 deletions bin/queue
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/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;

require_once './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.

Could there be a problem if run utility from different places?
May be better use absoulte path via __DIR__ (see yii2)?

Copy link
Member Author

@terabytesoftw terabytesoftw Nov 22, 2023

Choose a reason for hiding this comment

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

It is not necessary when using ./ it works well for commands, whether you install the package in an app or clone it.

Copy link
Member

Choose a reason for hiding this comment

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

I try it, and this do not work correclty.

Copy link
Member Author

Choose a reason for hiding this comment

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

As it fails, to reproduce it, since I use it in the app, and in the cloned directory, and it works correctly.

Copy link
Member

Choose a reason for hiding this comment

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

image

Copy link
Member Author

Choose a reason for hiding this comment

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

But you are inside the config directory, you must be root :)

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

Copy link
Member

Choose a reason for hiding this comment

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

Don't work. Add test.

Copy link
Member Author

Choose a reason for hiding this comment

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

image

image

It is a simple script, it works from the app's main directory, and from its own directory.

Copy link
Member

Choose a reason for hiding this comment

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

Utlitiy should work from any directory. Just use absolute path.


/** @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);

if ($container->has(LoggerInterface::class) === false) {
throw new RuntimeException(
vjik marked this conversation as resolved.
Show resolved Hide resolved
'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();
11 changes: 11 additions & 0 deletions bin/queue.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@echo off

@setlocal

set YII_PATH=%~dp0

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

"%PHP_COMMAND%" "%YII_PATH%queue" %*
vjik marked this conversation as resolved.
Show resolved Hide resolved

@endlocal
9 changes: 6 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,10 @@
"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-console": "^2.0",
vjik marked this conversation as resolved.
Show resolved Hide resolved
"yiisoft/yii-debug": "dev-master"
},
"suggest": {
"ext-pcntl": "Need for process signals"
Expand All @@ -58,6 +58,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;
}
}
Loading