amonger/command
Composer 安装命令:
composer require amonger/command
包简介
The command pattern for PHP 5.3
README 文档
README
This is a simple command bus for use with PHP 5.3
Use
<?php namespace Command\Command; class MyHandlerCommand { public $someParam; public function __construct($someParam) { $this->someParam = $someParam; } }
<?php namespace Command\Handler; class MyHandlerHandler implements HandlerInterface, ApplicationInterface { protected $app; public function handle($object) { echo $app['db']->persist($object->someParam); } public function setApplication($app) { $this->app = $app; } }
$command = new Command\Command(new Command\Resolver); $command->setApplication($app); $command->dispatch(new MyHandlerCommand($someParam));
This will resolve MyHandlerHandler which implements HandlerInterface, and provides access to your application container.
Self handling
Alternatively you might not want to separate your DTOs from your commands. In this case, you can just implement the self handling interface which will call the handle method.
<?php namespace Command\Command; class MyHandlerCommand implements SelfHandling { protected $someParam; public function __construct($someParam) { $this->someParam = $someParam; } public function handle() { return $this->someParam * 2; } }
Finally, you can get access to the application by implementing ApplicationInterface
<?php class TestCommand implements SelfHandling, ApplicationInterface { private $name; private $application; public function __construct($name) { $this->name = $name; } public function handle() { $app = $this->application; var_dump($app($this->name)); } /** * @param $application * @return void */ public function setApplication($application) { $this->application = $application; } }
$resolver = new \Command\Resolver\SelfHandlingResolver(); $resolver->setApplication(function($a){ return $a*2;}); $command = new Command\Command($resolver); $command->dispatch(new TestCommand(2));
amonger/command 适用场景与选型建议
amonger/command 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 397 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 06 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 amonger/command 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 amonger/command 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 397
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-29