maxkaemmerer/commands

提供命令、命令处理程序、命令总线通信的接口和实现

v1.0.0 2018-07-21 11:42 UTC

This package is auto-updated.

Last update: 2024-09-08 07:21:32 UTC


README

Travis branch Coveralls github PHP from Packagist Packagist Packagist

描述

此库为简单的命令、命令处理程序、命令总线结构提供接口和实现。

这当然不是原创想法,但这是我偏好的相对简单的实现。

代码已完全测试,但我对在生产环境中使用不承担责任。

自行承担风险。

安装

composer require maxkaemmerer/commands

使用方法

通常,您不需要手动注册每个 CommandHandler。您可能希望使用容器或服务管理器的依赖注入。

(对于Symfony框架的一个例子是使用 CompilerPass)

您还希望通过依赖注入在需要的地方注入 CommandBus 本身。

如果您需要更高级的功能,请自由创建自己的 CommandBusPayload 实现。

注册命令处理程序

CommandHandler 注册到 CommandBusCommandHandlerhandles() 方法返回它处理的 Command 的名称。

最佳实践是使用命令的完全限定类名。例如 MyCommand::class

CommandHandler::handle($command) 方法是实际领域逻辑发生的地方。

请随意将服务、容器或其他您需要的任何内容注入到您的 CommandHandler 中。

$commandBus = new SimpleCommandBus();

$commandBus->registerHandler(new class implements CommandHandler
{

    /**
     * @param Command $command
     */
    public function handle(Command $command): void
    {
        echo $command->payload()->get('message');
    }

    /**
     * @return string
     * The name of the command this handler handles. Command::name()
     */
    public function handles(): string
    {
        return 'commandName';
    }

});

分派命令

分派 Command 将导致 CommandBus 寻找一个 CommandHandler,其 CommandHandler:handles() 方法与通过 Command:name() 指定的 Command 的名称匹配,并调用其 CommandHandler::handle($command) 方法。

重要提示:CommandHandlerCommandBus 从不返回任何内容。

...

// The CommandHandler was registered

$commandBus->dispatch(new class implements Command
{

    /**
     * @return CommandPayload
     */
    public function payload(): CommandPayload
    {
        // You would of course set the Payload in the constructor of your Command implementation
        return Payload::fromArray(['message' => 'Hello World!']);
    }

    /**
     * @return string
     * The name of this command, it is recommended to use the fully qualified class name.
     */
    public function name(): string
    {
        return 'commandName';
    }

});

完整示例

<?php

use MaxKaemmerer\Commands\Command;
use MaxKaemmerer\Commands\CommandHandler;
use MaxKaemmerer\Commands\CommandPayload;
use MaxKaemmerer\Commands\Exception\CommandException;
use MaxKaemmerer\Commands\Implementations\Payload;
use MaxKaemmerer\Commands\Implementations\SimpleCommandBus;

require_once __DIR__ . '/vendor/autoload.php';

try {
    $commandBus = new SimpleCommandBus();

    $commandBus->registerHandler(new class implements CommandHandler
    {

        /**
         * @param Command $command
         */
        public function handle(Command $command): void
        {
            echo $command->payload()->get('message');
        }

        /**
         * @return string
         * The name of the command this handler handles. Command::name()
         */
        public function handles(): string
        {
            return 'commandName';
        }

    });

    $commandBus->dispatch(new class implements Command
    {

        /**
         * @return CommandPayload
         */
        public function payload(): CommandPayload
        {
            // You would of course set the Payload in the constructor of your Command implementation
            return Payload::fromArray(['message' => 'Hello World!']);
        }

        /**
         * @return string
         * The name of this command, it is recommended to use the fully qualified class name.
         */
        public function name(): string
        {
            return 'commandName';
        }

    });

} catch (CommandException $exception) {
    error_log($exception->getMessage());
}

结果:Hello World!