itmedia/command-bus-bundle

命令总线

安装次数: 2,562

依赖项: 0

建议者: 0

安全性: 0

星标: 3

关注者: 1

分支: 1

开放问题: 0

类型:symfony-bundle

v0.5.0 2021-09-13 15:28 UTC

This package is auto-updated.

Last update: 2024-09-13 22:06:26 UTC


README

Scrutinizer Code Quality Build Status

安装

composer require itmedia/command-bus-bundle 

CommandBus

注册服务的示例,也参见以下内置处理程序(Middleware)的添加

services:

    app.command_bus:
        class: Itmedia\CommandBusBundle\CommandBus\CommandBus
        arguments: ["@itmedia_command_bus.container_handler_mapper"]

中间件

中间件实现消息的额外处理,例如:验证、权限检查、日志记录。中间件必须实现接口 MiddlewareInterface。在 CommandBus 中,消息执行时,会通过连接的 Middleware 进行处理。如果未满足规则,应始终抛出异常。

配置示例

services:
    app.command_bus:
        class: Itmedia\CommandBusBundle\CommandBus\CommandBus
        arguments: ["@itmedia_command_bus.container_handler_mapper"]
        calls:
            - [addMiddleware, ["@itmedia_command_bus.middleware_validation"]]
            - [addMiddleware, ["@app.middleware_access_control"]] 

    # custom middleware
    app.middleware_access_control:
        class: AppBundle\Middleware\AccessControlMiddleware

命令

命令必须有接口 Command

use Itmedia\CommandBusBundle\Command\Command;

class TestCommand implements Command
{
    //...

    public function commandName(): string
    {
        return 'test_command';
    }

}

如果使用,处理器可以有任意结构,或者对于单个处理器 - CommandHandler

配置 CommandHandler 的示例

services:
    # по умолчанию будет вызван метод execute()
    AppBundle\Handler\MyHandler:
        public: true
        tags:
            - {name: command_handler, command: core_register_user } 


    # явное указание методов
    AppBundle\Handler\NyHandler2:
        public: true
        tags:
            - {name: command_handler, command: core_register_user1, method: methodName1 }
            - {name: command_handler, command: core_register_user2, method: methodName2 }
            - {name: command_handler, command: core_register_user3, method: methodName3 }

使用示例

    $command = new CommandTest();
    $this->get('app.command_bus')->handle($command);

命令验证

要使用 symfony/validator 对命令进行验证,需要为 CommandBus 连接 ValidationMiddleware

services:
    Itmedia\CommandBusBundle\CommandBus\CommandBus:
        arguments: ["@itmedia_command_bus.container_handler_mapper"]
        calls:
            - [addMiddleware, ["@itmedia_command_bus.middleware_validation"]]

命令验证规则的示例

use Itmedia\CommandBusBundle\Command\Command;
use Symfony\Component\Validator\Constraints as Assert;

class TestCommand implements Command
{

    /**
     * @NotBlank()
     */
    private string $username;

    /**
     * @NotBlank()
     * @Assert\Email()
     */
    private string $email;

    public function __construct(string $username, string $email)
    {
        $this->username = $username;
        $this->email = $email;
    }

    public function commandName(): string
    {
        return 'test_command';
    }

    public function getUsername(): string
    {
        return $this->username;
    }

    public function getEmail(): string
    {
        return $this->email;
    }

}

如果命令未通过验证,将抛出异常 ValidationException

从数组设置命令属性值

HandlePropertiesFormArrayTrait - 用于从数组中按键设置命令属性的辅助特性。键的名称应与属性的名称相匹配。

use Itmedia\CommandBusBundle\Command\Command;
use Itmedia\CommandBusBundle\Command\HandlePropertiesFormArrayTrait;

class TestCommand implements Command
{

    use HandlePropertiesFormArrayTrait;
    
    // ....
  
    public function __construct($id, array $data)
    {
        $this->handleProperties($data);
        $this->id = $id;
    }

}