innmind / command-bus-bundle
此包已被放弃且不再维护。未建议替代包。
命令总线捆绑包
2.0.0
2017-02-19 16:29 UTC
Requires
- php: ~7.1
- innmind/command-bus: ~2.0
- symfony/config: ~3.0
- symfony/dependency-injection: ~3.0
- symfony/http-kernel: ~3.0
- symfony/yaml: ~3.0
Requires (Dev)
- phpunit/phpunit: ~6.0
This package is auto-updated.
Last update: 2022-02-01 13:00:09 UTC
README
master |
develop |
---|---|
这是 innmind/command-bus
的 Symfony 集成,它简化了命令总线的堆叠。
安装
composer require innmind/command-bus-bundle
在你的 AppKernel.php
中添加以下行
//app/AppKernel.php class AppKernel extends Kernel { public function registerBundles() { $bundles = array( // ... new Innmind\CommandBusBundle\InnmindCommandBusBundle, ); // ... } // ... }
使用方法
$container->get('innmind_command_bus');
为了处理你的命令,你需要定义服务作为具有 innmind_command_bus.handler
标签的处理程序,其中包含属性 handles
,它将包含命令的 FQCN。
高级配置
你可能希望向命令总线添加额外的功能,以便在每个命令执行前后做一些特定的事情(例如刷新 doctrine 管理器或记录每个命令)。为此,你需要创建一个实现 CommandBusInterface
的类。该类必须在其构造函数中至少有一个类型提示此接口的参数。
然后你将这个命令总线声明为一个具有 innmind_command_bus
标签和 alias
属性的服务。然后必须将别名放置在 innminc_command_bus.stack
配置数组中。
示例
use Innmind\CommandBus\CommandBusInterface; use Psr\Log\LoggerInterface; final class LoggingCommandBus implements CommandBusInterface { private $bus; private $logger; public function __construct(LoggerInterface $logger, CommandBusInterface $bus) { $this->logger = $logger; $this->bus = $bus; } public function handle($command) { $this->bus->handle($command); $this->logger->debug( 'A command has been executed', ['class' => get_class($command)] ); } }
#app/config/services.yml services: logging_command_bus: class: LoggingCommandBus arguments: - '@logger' - ~ #this is important to declare this argument as null tags: - { name: innmind_command_bus, alias: logging }
#app/config/config.yml innmind_command_bus: stack: - queue - logging - default
有了所有这些,每次处理命令时,它都会检查是否有正在处理的处理程序(在这种情况下,它将排队;如果你在处理程序内部处理命令,就会发生这种情况),然后如果可以执行,它将记录命令(这是你的类),最后它将调用与命令关联的处理程序。