nicwortel/command-pipeline

v0.4.0 2021-03-28 13:15 UTC

This package is auto-updated.

Last update: 2024-09-28 21:16:13 UTC


README

Build status License Required PHP version Current version

这是一个 PHP 的内存中命令管道实现。命令管道用于处理 CQRS 应用程序中的跨切面关注点,如命令验证、授权、日志记录等。

命令按线性过程处理。每个阶段的返回值传递给下一个阶段。一个阶段只能通过抛出异常来阻止下一个阶段的执行。

命令管道的概念是基于管道和过滤器模式的。

功能

  • 命令验证
  • 命令授权
  • 将命令处理包装在 Doctrine 数据库事务中
  • 将发出的领域事件发送到事件总线

安装

composer require nicwortel/command-pipeline

如果你使用的是 Symfony,请启用 CommandPipelineBundle

// config/bundles.php

return [
    // ...
    NicWortel\CommandPipeline\Bundle\CommandPipelineBundle::class => ['all' => true],
];

用法

从服务容器中获取 command_pipeline 服务或将 CommandPipeline 注入到你的应用程序代码中(推荐)

<?php

declare(strict_types=1);

use NicWortel\CommandPipeline\CommandPipeline;
// ...

class MyController
{
    private CommandPipeline $commandPipeline;

    public function __construct(CommandPipeline $commandPipeline)
    {
        $this->commandPipeline = $commandPipeline;
    }

    public function saveAction(): Response
    {
        $command = new MyCommand();
        $command->foo = 'bar';

        $this->commandPipeline->process($command);

        // ...
    }
}