此包最新版本(1.0.2)没有可用的许可证信息。

一个简单的命令总线。

1.0.2 2016-03-24 09:52 UTC

This package is auto-updated.

Last update: 2024-09-15 06:50:31 UTC


README

Build Status Latest Stable Version Total Downloads Monthly Downloads Daily Downloads License

用法

您需要一个命令和一个CommandHandler。

假设我们有一个存储在app/commanding/command/RegisterUserCommand.php的命令类。

namespace App\Commanding\Command;

class RegisterUserCommand extends \TomWright\Commander\Command\Command
{
	protected $username;
    protected $password;
    
    public function setUsername($username)
    {
    	$this->username = $username;
    }
    
    public function getUsername()
    {
    	return $this->username;
    }
    
    public function setPassword($password)
    {
        $this->password = $password;
    }
    
    public function getPassword()
    {
        return $this->password;
    }
}

假设我们还有一个存储在app/commanding/handler/RegisterUserHandler.php的命令处理类。

namespace App\Commanding\Handler;

class RegisterUserHandler implements \TomWright\Commander\Handler\HandlerInterface
{   
    public function handle(\TomWright\Commander\Command\CommandInterface $command)
    {
    	echo "Registering user \"{$command->getUsername()}\" with password \"{$command->getPassword()}\".";
    }
}

现在我们需要添加一个CommandHandler命名空间,以便CommandBus知道在哪里查找处理程序。

$bus = \TomWright\Commander\CommandBus::getInstance();
$bus->addHandlerNamespace('\\App\\Commanding\\Handler');

现在,每当我们要注册新用户时,我们只需执行以下操作

$bus = \TomWright\Commander\CommandBus::getInstance();
$command = new \App\Commanding\Command\RegisterUserCommand();
$command->setUsername('Some user');
$command->setPassword('Somepassword123');
$bus->handle($command);

记住 - 命令是一个动作,而不是一个通知。如果您正在寻找通知/事件处理程序,请参阅TomWright/Eventing