trntv/yii2-command-bus
Composer 安装命令:
composer require trntv/yii2-command-bus
包简介
Yii2 Command Bus extension
README 文档
README
Command Bus for Yii2
What is Command Bus?
The idea of a command bus is that you create command objects that represent what you want your application to do. Then, you toss it into the bus and the bus makes sure that the command object gets to where it needs to go. So, the command goes in -> the bus hands it off to a handler -> and then the handler actually does the job. The command essentially represents a method call to your service layer.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist trntv/yii2-command-bus
or add
"trntv/yii2-command-bus": "^1.0"
to your composer.json file
Setting Up
1. Command Bus Service
After the installation, first step is to set the command bus component.
return [ // ... 'components' => [ 'commandBus' => [ 'class' => 'trntv\bus\CommandBus' ] ], ];
2. Background commands support (optional)
Install required package:
php composer.phar require symfony/process:^3.0
For the background commands worker, add a controller and command bus middleware in your config
'controllerMap' => [ 'background-bus' => [ 'class' => 'trntv\bus\console\BackgroundBusController', ] ], 'components' => [ 'commandBus' =>[ ... 'middlewares' => [ [ 'class' => '\trntv\bus\middlewares\BackgroundCommandMiddleware', 'backgroundHandlerPath' => '@console/yii', 'backgroundHandlerRoute' => 'background-bus/handle', ] ] ... ] ],
Create background command
class ReportCommand extends Object implements BackgroundCommand, SelfHandlingCommand { use BackgroundCommandTrait; public $someImportantData; public function handle($command) { // do what you need } }
And run it asynchronously!
Yii::$app->commandBus->handle(new ReportCommand([ 'async' => true, 'someImportantData' => [ // data // ] ]))
3. Queued commands support (optional)
3.1 Install required package:
php composer.phar require yiisoft/yii2-queue
3.2 Configure extensions
If you need commands to be run in queue, you need to setup middleware and yii2-queue extensions.
'components' => [ 'queue' => [ // queue config ], 'commandBus' =>[ ... 'middlewares' => [ [ 'class' => '\trntv\bus\middlewares\QueuedCommandMiddleware', // 'delay' => 3, // You can set default delay for all commands here ] ] ... ] ]
More information about yii2-queue config can be found here
3.4 Run queue worker
yii queue/listen
More information here
3.5 Create and run command
class HeavyComputationsCommand extends Object implements QueuedCommand, SelfHandlingCommand { use QueuedCommandTrait; public function handle() { // do work here } } $command = new HeavyComputationsCommand(); Yii::$app->commandBus->handle($command)
4. Handlers
Handlers are objects that will handle command execution There are two possible ways to execute command:
4.1 External handler
return [ // ... 'components' => [ 'commandBus' => [ 'class' => 'trntv\bus\CommandBus', 'locator' => [ 'class' => 'trntv\bus\locators\ClassNameLocator', 'handlers' => [ 'app\commands\SomeCommand' => 'app\handlers\SomeHandler' ] ] ] ], ]; // or $handler = new SomeHandler; Yii::$app->commandBus->locator->addHandler($handler, 'app\commands\SomeCommand'); // or Yii::$app->commandBus->locator->addHandler('app\handlers\SomeHandler', 'app\commands\SomeCommand');
4.1 Self-handling command
class SomeCommand implements SelfHandlingCommand { public function handle($command) { // do what you need } } $command = Yii::$app->commandBus->handle($command);
trntv/yii2-command-bus 适用场景与选型建议
trntv/yii2-command-bus 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 655.64k 次下载、GitHub Stars 达 57, 最近一次更新时间为 2016 年 01 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「queue」 「extension」 「yii2」 「command bus」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 trntv/yii2-command-bus 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 trntv/yii2-command-bus 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 trntv/yii2-command-bus 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
PHP AMQP Binding Library
A Laravel package to monitor queue jobs.
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
统计信息
- 总下载量: 655.64k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 57
- 点击次数: 17
- 依赖项目数: 12
- 推荐数: 1
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2016-01-21