yproximite / command-processor
此包的最新版本(v1.0.1)没有提供许可信息。
命令处理器
v1.0.1
2017-10-12 11:46 UTC
This package is not auto-updated.
Last update: 2024-09-24 06:46:44 UTC
README
此库提供了一种框架,用于启用多阶段过程的顺序和有序执行。
基本用法
一个示例过程:
<?php
namespace My\Namespace;
use Ylly\CommandProcessor\Process;
use Ylly\CommandProcessor\ProcessCommand;
class MyProcess extends Process
{
public function configure()
{
$this->setTitle('My Hello World Process');
$this->addCommand(new ProcessCommand('doHello', 'Says hello'));
$this->addCommand(new ProcessCommand('doGoodbye', 'Says goodbye'));
}
public function doHello(CommandInterface $command)
{
echo "Hello world.";
}
public function doGoodbye(CommandInterface $command)
{
echo "Goodbye cruel world.";
throw new \Exception('Process will fail gracefully when exception is encountered.');
}
}
过程可以按照以下方式执行:
$p = new MyProcess;
$p->execute();
日志记录
您可以通过传递一个实现 write
方法的类来向过程中添加日志。
$logger = new MyLogger; // (must have a method `function write($string)`
$p = new MyProcess;
$p->setOutput($logger);
$p->execute();
自定义命令
包装的 ProcessCommand
执行过程内定义的方法(作为命令)。然而,如果您愿意,当然可以为每个命令实现一个类:
<?php
namespace My\Namespace;
use Ylly\CommandProcessor\Process;
use Ylly\CommandProcessor\CommandInterface;
class MyCommand implements CommandInterface
{
// store result (optional)
protected $result;
// store the process (optional)
protected $process;
// store execution status
protected $hasExecuted;
/**
* Execute the command
*
* @access public
* @return void
*/
public function execute()
{
$result = // perform some crazy task;
// store the result for future access via. the `getResult` method.
$this->result = $result;
// mark the command as executed, quieried via. the `hasExecuted` method.
$this->hasExecuted = true;
}
/**
* Set the executing process ...
*
* @param Process $process
* @access public
* @return void
*/
public function setProcess(Process $process)
{
// store the process thats executing this command, if you like.
$this->process = $process;
}
/**
* To return a short description of the command
*
* @access public
* @return string
*/
public function getDescription()
{
return 'My command does this';
}
/**
* Return the title of this command
*
* @access public
* @return string
*/
public function getTitle()
{
return 'My Command';
}
/**
* If the command has been executed or not
*
* @access public
* @return boolean
*/
public function hasExecuted()
{
return $this->hasExecuted;
}
/**
* If the command has been executed, this
* will return the result of the command if applicable
*
* @access public
* @return void
*/
public function getResult()
{
if (!$this->hasExecuted()) {
throw new \Exception('Command has not been executed yet!');
}
return $this->result;
}
}
或者您也可以覆盖 ProcessCommand
类。
运行时添加命令
您可以使用 Process::addCommand
方法动态地向过程中添加命令:
$p = new MyProcess;
$p->addCommand(new AnotherCommand);
$p->execute();
向过程传递数据和依赖项
抽象的 Process
类没有构造函数方法,因此您可以创建自己的构造函数,例如:
class MyProcess extends Process
{
public function __construct(Connection $dbconnection, SomeAPI $api)
{
// ..
}
}
应该通过 setData
方法将用户数据传递到过程中:
$p = new MyProcess;
$p->setData(array('Foo', 'Bar'));
$p->execute();