robotusers/commander

PHP 的命令总线抽象。

0.3.0 2021-03-23 11:08 UTC

This package is auto-updated.

Last update: 2024-09-23 18:31:43 UTC


README

Software License Build Status codecov

PHP 的命令总线抽象。

安装

composer require robotusers/commander

命令总线抽象

此库提供了一个 CommandBusInterface,命令总线应该实现此接口。

使用命令总线

use Robotusers\Commander\CommandBusAwareInterface;
use Robotusers\Commander\CommandBusAwareTrait;

class OrdersController implements CommandBusAwareInterface
{
   use CommandBusAwareTrait;

   public function makeOrder()
   {
        ...
        $command = new MakeOrderCommand($data);
        $this->handleCommand($command);
        ...
   }

}

适配器

此库为最常见的命令总线实现提供了适配器。

Tactician

composer require league/tactician
use League\Tactician\CommandBus;
use Robotusers\Commander\Adapter\TacticianAdapter;

$commandBus = new CommandBus($middleware);
$adapter = new TacticianAdapter($commandBus);

$controller->setCommandBus($adapter);

SimpleBus/MessageBus

composer require simple-bus/message-bus
use Robotusers\Commander\Adapter\SimpleBusAdapter;
use SimpleBus\Message\Bus\Middleware\MessageBusSupportingMiddleware;

$commandBus = new MessageBusSupportingMiddleware();
$adapter = new SimpleBusAdapter($commandBus);

$controller->setCommandBus($adapter);

PSB - ProophServiceBus

composer require prooph/service-bus
use Prooph\ServiceBus\CommandBus;
use Robotusers\Commander\Adapter\ServiceBusAdapter;

$commandBus = new CommandBus();
$adapter = new ServiceBusAdapter($commandBus);

$controller->setCommandBus($adapter);

编写自定义适配器

您可以编写自己的自定义适配器。适配器必须实现 Robotusers\Commander\CommandBusInterface

class MyAdapter implements CommandBusInterface
{
    public function handle($command)
    {
        //handle a command
    }
}