stiggg / command-dispatcher
命令分发库
该软件包的规范仓库似乎已丢失,因此软件包已被冻结。
v1.0
2014-07-13 11:38 UTC
Requires
- php: >=5.3.0
- doctrine/collections: ~1.0
- psr/log: ~1.0
Requires (Dev)
- monolog/monolog: ~1.7
- phpunit/phpunit: ~3.7.0
This package is not auto-updated.
Last update: 2020-04-27 08:52:49 UTC
README
支持预命令和后命令处理程序的命令分发/执行库
- 可撤销命令
- 可链式命令
- 命令排队
- 命令历史
- 分发器类
分发器(基本分发器)
- 分发器(基本分发器)
- 队列分发器
- 链式分发器
动机
命令分发库的基本思想是将程序逻辑抽象成独立的类,逻辑通过从领域语言派生的类名表达。
优点包括
- 正确使用领域语言描述了代码应该做什么,因此它更好地传达了其意图,对所有感兴趣的相关方来说都有更好的沟通。
- 可以轻松添加和将每个命令的日志记录等常见支持任务抽象到命令类中,这样它们就不会 clutter 主程序代码。
- 易于对命令逻辑进行单元测试
使用方法
基本分发器
class SomeCommand extends AbstractCommand implements CommandInterface
{
public function __construct($param1, $param2)
{
// add dependencies, construct command "environment"
}
public function execute()
{
// command logic added here
}
}
$dispatcher = new Dispatcher();
$dispatcher->handle(
SomeCommand::create(array($param1, $param2))
);
使用不同的构造参数两次分发和记录相同的命令
class SomeCommand extends AbstractCommand implements CommandInterface
{
public function __construct($parameter)
{
// ...
}
public function execute()
{
//...
}
public function log($logger)
{
// use logger
}
}
$dispatcher = new Dispatcher();
$dispatcher->addPostCommandHandler(function($command) use ($logger) {
$command->log($logger);
});
$dispatcher->handleCommands(array(
SomeCommand::create(array($oneValue)),
SomeCommand::create(array($anotherValue)),
));
请参阅 基本分发器示例。
链式分发器
使用链式分发器,每个命令将前一个命令的输出作为执行()方法的输入
class FirstCommand extends AbstractCommand implements ChainableCommandInterface
{
public function execute(array $input)
{
//...
}
}
class SecondCommand extends AbstractCommand implements ChainableCommandInterface
{
public function execute(array $input)
{
//...
}
}
$dispatcher = new ChainDispatcher();
$firstCommandInput = 'wow';
// $firstCommandInput is FirstCommand's input, $output is SecondCommand's return value
$output = $dispatcher->handleCommands(array(
FirstCommand::create(),
SecondCommand::create()
), $firstCommandInput);
请参阅 链式分发器示例。
安装
要将命令分发器库添加到Composer,请参阅Packagist以获取版本信息: https://packagist.org.cn/packages/stiggg/command-dispatcher
要求
- PHP >=5.3
- Composer
许可证
版本历史
支持semver的版本控制: http://semver.org/
请参阅 版本历史