icanboogie / bind-message-bus
将 icanboogie/message-bus 绑定到 ICanBoogie
Requires
- php: >=7.2
- icanboogie/icanboogie: ^5.0
- icanboogie/message-bus: ^1.0
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-09-22 21:14:09 UTC
README
icanboogie/bind-message-bus 包将 icanboogie/message-bus 绑定到 ICanBoogie。
<?php use ICanBoogie\MessageBus\SimpleDispatcher; /* @var \ICanBoogie\Application $app */ $bus = new SimpleDispatcher($app->message_handler_provider); # or $bus = $app->message_bus;
绑定
应用绑定
以下获取器被添加到 Application 类中
message_bus
: 使用$app->message_handler_provider
和$app->message_pusher
创建的 Dispatcher 实例。message_handler_provider
: 使用message-bus-handlers
配置的 HandlerProvider 实例。
<?php /* @var \ICanBoogie\Application $app */ $app->message_bus; $app->message_handler_provider;
控制器绑定
以下方法被添加到 Controller 类中
mixed dispatch_message($message)
: 使用$this->app->command_bus
分发消息。
使用与您的控制器一起使用的 ControllerBindings 特性来提示绑定。
<?php use ICanBoogie\Routing\Controller; class ProductController extends Controller { use Controller\ActionTrait; use \ICanBoogie\Binding\MessageBus\ControllerBindings; protected function action_create() { $message = new CreateProduct($this->request['payload']); $result = $this->dispatch_message($message); return $this->respond($result); } }
配置
使用 message-bus
配置来配置消息总线。消息处理程序通过具有键 MessageBusConfig::HANDLERS
的键/值对数组定义,其中 键 是消息类,而 值 是处理程序(一个可调用对象)。
以下示例演示了如何将消息与处理程序引用匹配。 icanboogie/service 包提供了用于定义引用的 ref()
方法。
<?php namespace App\Application\Message; use function ICanBoogie\Service\ref; use ICanBoogie\Binding\MessageBus\MessageBusConfig; return [ MessageBusConfig::HANDLERS => [ CreateArticle::class => ref('handler.article.create'), DeleteArticle::class => ref('handler.article.delete'), ] ];
要求
该软件包需要 PHP 7.2 或更高版本。
安装
composer require icanboogie/bind-message-bus
文档
该软件包作为 ICanBoogie 框架 文档 的一部分进行记录。您可以使用 make doc
命令生成软件包及其依赖项的文档。文档生成在 build/docs
目录中。需要 ApiGen。可以使用 make clean
命令清理该目录。
测试
运行 make test-container
以创建并登录到测试容器,然后运行 make test
以运行测试套件。或者,运行 make test-coverage
以带有测试覆盖率运行测试套件。打开 build/coverage/index.html
以查看代码覆盖率的细分。
许可证
icanboogie/bind-message-bus 根据 新BSD许可证 发布。