wyrihaximus / tactician-job-command-mapper
Tactician 的作业到命令映射器
3.0.0
2019-01-14 17:55 UTC
Requires
- php: ^7.2
- doctrine/annotations: ^1.6
- wyrihaximus/doctrine-annotation-autoloader: ^1.0
- wyrihaximus/list-classes-in-directory: ^1.3
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-04 19:30:13 UTC
README
安装
通过 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
特此授予任何人获得本软件及其相关文档文件(“软件”)副本的许可,免费使用该软件,不受限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许向软件提供者提供软件的人这样做,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、适用于特定目的和无侵权性的保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任(无论是基于合同、侵权或其他方式)承担责任,无论是因软件或软件的使用或其他方式而产生的。