phphd/pipeline-bundle
Composer 安装命令:
composer require phphd/pipeline-bundle
包简介
Chain of Responsibility on top of Symfony Messenger
README 文档
README
🧰 Provides Symfony Messenger middleware for basic per-bus pipelining. It enables streamlined chaining of the messages created by message handlers. For instance, when handler (hdl1) processes message (msg1), it creates a subsequent message (msg2), triggering the invocation of the next handler (hdl2), which may, in turn, produce yet another new message, and this cycle continues.
Installation 📥
-
Install via composer
composer require phphd/pipeline-bundle
-
Enable the bundle in the
bundles.phpPhPhD\PipelineBundle\PhdPipelineBundle::class => ['all' => true],
Configuration ⚒️
To leverage chain of pipelined handlers for your command/query buses,
you should add phd_pipeline.forward_chain middleware to the list:
framework:
messenger:
buses:
command.bus:
middleware:
- doctrine_transaction
+ - phd_pipeline.forward_chain
- validation
query.bus:
middleware:
+ - phd_pipeline.forward_chain
- validation
Usage 🚀
Consider having this original message that is initially dispatched to the message bus:
final readonly class CreateVacationRequestCommandDto { public function __construct( public int $userId, public int $vacationTypeId, #[Assert\DateTime] public string $startDate, #[Assert\DateTime] public string $endDate, ) { } }
Thy upfront message handler returns a new message that will be used for subsequent redispatch:
#[AsMessageHandler(bus: 'command.bus')] final readonly class ConvertVacationRequestCommandHandler { public function __invoke(CreateVacationRequestCommandDto $dto): CreateVacationRequestCommand { $employee = $this->employeeRepository->find($dto->userId); $vacationType = $this->vacationTypeRepository->find($dto->vacationTypeId); $vacationPeriod = VacationPeriod::fromStringDates($dto->startDate, $dto->endDate); return new CreateVacationRequestCommand($employee, $vacationType, $vacationPeriod); } }
The new created message conveys basically the same business concept, but on the higher level of abstraction than
initially. Thereof, instead of scalar types, it has business objects (e.g. VacationType entity instead
of $vacationTypeId scalar). Basically, new class no longer merely represents the DTO. It now embodies the complete
domain object.
You should add #[NextForwarded] attribute to enable forwarding of this new message to the next handler:
use PhPhD\Pipeline\NextForwarded; #[NextForwarded] final readonly class CreateVacationRequestCommand { public function __construct( public Employee $employee, public VacationType $vacationType, public VacationPeriod $vacationPeriod, ) { } }
Messages lacking
#[NextForwarded]attribute will not be forwarded. This attribute must be put on each message expected of redispatching.
Finally, one ultimate handler must implement the core business logic. It may or may not return a result to the calling code.
#[AsMessageHandler(bus: 'command.bus')] final readonly class CreateVacationRequestHandler { public function __invoke(CreateVacationRequestCommand $command) { // The core business logic that deals with domain entities rather than primitives... } }
You may chain as many message handlers as needed, even in a recursive manner, by returning an instance of the same class as the original message, provided that it has the forwarding attribute enabled.
Extended forwarding
If you don't want to use the attribute on the message class, you don't have to. There could be some cases when you'd
like to apply some dynamic configurations for NextForwarded instance. In such cases, you can return an instance of
NextForwarded class right from the handler method:
#[AsMessageHandler(bus: 'command.bus')] final readonly class ConvertVacationRequestCommandHandler { /** @return NextForwarded<CreateVacationRequestCommand> */ public function __invoke(CreateVacationRequestCommandDto $dto): NextForwarded { return new NextForwarded($this->createCommandFromDto($dto)); } }
The code above is no different from the one shown earlier.
phphd/pipeline-bundle 适用场景与选型建议
phphd/pipeline-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.8k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 phphd/pipeline-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phphd/pipeline-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 11.8k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-12-30