pccomponentes/common-bus

通用总线库

v0.0.2 2018-09-17 10:40 UTC

This package is auto-updated.

Last update: 2024-09-23 03:05:20 UTC


README

总线

您可以创建自己的总线,实现 CommandBus 或 QueryBus 接口,或者您可以使用预先配置的 Bus CommandBusSync 用于命令或 QueryBusSync 用于查询

使用总线

$bus = new CommandBusSync();
$bus->register(YourCommandClass::class, new Your\Invokable\Handler());
$bus->dispatch(new YourCommandClass());

如果您使用像 Symfony 依赖注入这样的服务容器,您可以通过注册您的命令来配置您的总线

unilae.bus.sync-command:
    class: Unilae\HexagonalUtils\Infrastructure\Bus\Command\CommandBusSync
    calls:
      - method: register
        arguments:
         - 'Your\Command\Class\Name'
         - '@Your\Handler\Service\Id'

这样,您可以将总线注入到您的控制器中

final class MyController
{
    private $bus;
    
    public function __construct(CommandBusSync $bus)
    {
        $this->bus = $bus;    
    }
    
    public function __invoke(Request $request)
    {
        $this->bus->dispatch(
            new Your\Command\Class(
                new Uuid($request->get('request_id'))
            )
        );
    }
}

如果您将多个命令注册到同一总线上,我建议您安装并使用 ocramius/proxy-manager 来处理代理

使用中间件

预先配置的命令总线支持中间件链,要使用它,您可以将您的中间件类传递给 Bus 构造函数