wyrihaximus/tactician-job-command-mapper

Tactician 的作业到命令映射器

3.0.0 2019-01-14 17:55 UTC

README

Build Status Latest Stable Version Total Downloads Code Coverage License PHP 7 ready

安装

要通过 Composer 安装,请使用以下命令,它将自动检测最新版本并将其绑定到 ^

composer require wyrihaximus/tactician-job-command-mapper

设置

当创建一个 Command 时,添加 @Job 注解以将其映射到一个或多个作业。

<?php

namespace Test\App\Commands;

use WyriHaximus\Tactician\JobCommand\Annotations\Job;

/**
 * @Job("awesomesauce")
 * OR
 * @Job({"awesomesauce", "sauceawesome"})
 */
class AwesomesauceCommand
{
}

映射

映射器需要两个东西,一个它可以从中找到命令的路径。从那里它扫描所有找到的类,查找 @Job 注解,并将该映射内部存储。

Mapper::map

要添加一组命令,只需传递路径,映射器将为您选择正确的命名空间。

use League\Tactician\Setup\QuickStart;

$map = (new Mapper())->map('src' . DS . 'CommandBus');

Mapper::hasCommand

检查映射是否包含我们拥有的作业的命令。

$job = 'awesomesauce';
// True when it has a command or false when it doesn't
$map->hasCommand($job);

Mapper::getCommand

检查映射是否包含我们拥有的作业的命令。

$job = 'awesomesauce';
// Command class when it has a command, or throws an exception when it doesn't
$command = $map->getCommand($job);

// Call the command bus with the command we found to handle the given job
$commandBus->handle(new $command(...$data));

完整类示例

final class CommandBusWrapper
{
    /**
      * @var CommandBus
      */
    private $commandBus;
    
    /**
      * @var Mapper
      */
    private $map;
    
    /**
      * @param CommandBus
      */
    public function __construct(CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
        $this->map = (new Mapper())->map('src' . DS . 'CommandBus', 'App\CommandBus');
    }
    
    /**
      * @param string $job
      * @param array $data
      * @return bool
      */
    function handle(string $job, array $data = []): bool
    {
        if (!$this->map->hasCommand($job)) {
            return false;
        }

        $command = $map->getCommand($job);
        $this->commandBus->handle(new $command(...$data));
        return true;
    }
}

许可协议

MIT 许可协议 (MIT)

版权所有 (c) 2019 Cees-Jan Kiewiet

特此授予任何获得本软件及其相关文档副本(“软件”)的人,在以下条件下免费使用软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许获得软件的人行使上述权利,前提是

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性。在任何情况下,作者或版权持有人不对任何索赔、损害或其他责任负责,无论这些责任是基于合同、侵权或其他方式产生,无论这些责任是否与软件或其使用或其他方式有关。