jlorente/yii2-command-handler
Composer 安装命令:
composer require jlorente/yii2-command-handler
包简介
A Yii2 extension that provides the framework with the Command Design Pattern. Useful to create commands in a main process and execute them in another processes such as crons, console controllers, etc.
README 文档
README
A Yii2 extension that provides the framework with the Command Design Pattern. Useful to create commands in a main process and execute them in another processes such as crons, console controllers, etc.
Installation
To install, either run
$ php composer.phar require jlorente/yii2-command-handler "*"
or add
... "require": { // ... other configurations ... "jlorente/yii2-command-handler": "*" }
to the require section of your composer.json file and run the following
commands from your project directory.
$ composer update $ ./yii migrate --migrationPath=@app/vendor/jlorente/yii2-command-handler/src/migrations
The last command will create the table needed to handle the command list.
Basic Usage
First at all you must include the module in your console application in order to execute the command queue processor.
./console/config/main.php
// ... other configurations ... "modules" => [ // ... other modules ... "command" => [ "class" => "jlorente\command\Module" ] ]
Now you can begin to create your own commands.
Example
Imagine we want multiple emails to be sent to a group of users when the admin of this group clicks a button. The process could be tedious and would increase in time the more users are in the group so our admin will have to wait until the process is finished to continue navigating over the website.
Here, we can create a command and delegate the process to an another non locking process by writting only a few lines.
Receiver
First, the Group class MUST implement \jlorente\command\base\Receiver interface and the method that will send the email to all the group users.
namespace common\models; use yii\db\ActiveRecord; use jlorente\command\base\Receiver; class Group extends ActiveRecord implements Receiver { public function sendEmailToUsers() { //Implementation of the send email to group users method goes here. } }
Command
Now we have to create the command that will execute this method. Note that the command MUST implement the \jlorente\command\base\CommandInterface or extend one of its descendants provided along with the package in order to work (\jlorente\command\base\Command for classes that extend form \yii\base\Model and \jlorente\command\db\Command for classes that implement the \yii\db\ActiveRecordInterface).
//In our example we will extend the \jlorente\command\db\Command because Group extends from \yii\db\ActiveRecord namespace common\models\commands; use jlorente\command\db\Command; class GroupSendEmailCommand extends Command { /** * @inheritdoc */ public function run() { $this->getReciver()->sendEmailToUsers(); } }
CommandMapper
Now we will create the controller action that receives the user click, creates and puts the command in the command list.
namespace frontend\controllers; use yii\web\Controller; use yii\web\NotFoundHttpException; use common\models\commands\GroupSendEmailCommand; use jlorente\command\db\CommandMapper; class GroupController extends Controller { public function actionSendEmail($groupId) { $group = Group::findOne($groupId); if ($group === null) { throw new NotFoundHttpException(); } $command = new GroupSendEmailCommand(); $command->setReceiver($group); CommandMapper::map($command); //Enqueues the command } }
Console Controller
The console controller will process the command list and execute the commands inside it in a queue mode. You can run the console controller by using the following shell command.
$ ./yii <moduleId>/command-processor/run <int>
Where <moduleId> is the name you put in the module configuration of your config file and <int> is an optional argument to limit the number of mappers to be processed in the execution.
Maybe you want to put the execution of the action in a cronjob or something similar to be run every minute or in the interval that you want.
/etc/crontab
*/1 * * * * <PathToProject>/yii <moduleId>/command-processor/run
Advanced Usage
Behavior
Along with the package comes a behavior that can be attached to your models that creates and maps command. Continuing with the previous example, we are going to send the email to the group of users when the group changes its state property instead of having a controller action that creates and maps the command.
namespace common\models; use yii\db\ActiveRecord; use jlorente\command\base\Receiver; use jlorente\command\behaviors\CommandGeneratorBehavior; use common\models\commands\GroupSendEmailCommand; class Group extends ActiveRecord implements Receiver { public function sendEmailToUsers() { //Implementation of the send email to group users method goes here. } public function behaviors() { return array_merge(parent::behaviors(), [ // ... other behaviors ... [ 'class' => CommandGeneratorBehavior::className(), 'commands' => [ self::EVENT_BEFORE_SAVE => GroupSendEmailCommand::className(), ], 'condition' => function ($model) { return $model->isAttributeChanged('state'); } ] ]); } }
By doing this, the GroupSendEmailCommand will be enqueued every time the state property changes. For more configuration params see the documentation in the CommandGeneratorBehavior class.
CommandProcessor
Maybe you want to run the command queue processor by your own instead of using the provided console controller.
You can achieve this by instantiating and running the CommandProcessor class.
use jlorente\command\base\CommandProcessor; $processor = new CommandProcessor(); $processor->run();
You can set the the way that the command processor executes the commands by selecting between queue and stack mode. By default the command processor will handle the execution in queue mode.
$processor->setMode(CommandProcessor::MODE_QUEUE)
The run method also accept two arguments. The first one is an integer that will limit the number of commands to be processed and the second is a boolean that indicates if the erroneous commands have to be restored in the queue to be processed another time. By default the processor will try to process all the commands in the list and will restore the erroneous ones.
$processor->run(10, false); //This will limit the commands to 10 and won't restore the erroneous ones.
Further considerations
Previously I said that commands for ActiveRecord receivers have to inherit from \jlorente\command\db\Command. That's not true at all and depends of the final behavior that you want to achieve. Commands for ActiveRecord receivers could inherit from \jlorente\command\base\Command too, but the receiver of the command will store the state it had when the command was created and not the one that it has when the command will be executed. Those commands that inherit from \jlorente\command\db\Command will always fetch the ActiveRecord object from the database instead of using a copy of the object with the properties that it had when the command was created.
License
Copyright © 2015 José Lorente Martín jose.lorente.martin@gmail.com.
Licensed under the MIT license. See LICENSE.txt for details.
jlorente/yii2-command-handler 适用场景与选型建议
jlorente/yii2-command-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 103 次下载、GitHub Stars 达 5, 最近一次更新时间为 2015 年 05 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「process」 「pattern」 「cron」 「command」 「design」 「handler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jlorente/yii2-command-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jlorente/yii2-command-handler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jlorente/yii2-command-handler 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
Symfony bundle to monitor and execute commands
Shell command module for PHP.
Swoole server process management
Scheduling extension for Yii2 framework
A Laravel package for the Repository Design Pattern.
统计信息
- 总下载量: 103
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 28
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-06