jardisadapter/eventdispatcher
Composer 安装命令:
composer require jardisadapter/eventdispatcher
包简介
PSR-14 event dispatcher for PHP with priority-ordered listeners, type-hierarchy matching, stoppable events, and deferred dispatch via EventCollector; a building block of the open-source foundation that Jardis-generated DDD code runs on
关键字:
README 文档
README
Part of Jardis — the Domain-Driven Design platform for PHP. You model your domain; Jardis generates the production-ready hexagonal code (DTOs, Command/Query handlers, repositories, persistence). This package is part of the open-source foundation that generated code runs on.
Domain events for PHP as first-class citizens. A lightweight PSR-14 event dispatcher — built for DDD applications where events drive the communication between layers and contexts. No framework, no overhead, no magic. Just what you need.
Why this Dispatcher?
- Four classes, zero magic —
EventDispatcher,ListenerProvider,Event,EventCollector - Priority ordering — listeners with higher priority are called first
- Type-hierarchy matching — a listener on an interface catches all implementing events
- Stoppable events — break the listener chain when an event is considered handled
- EventCollector — collect events in the domain layer, dispatch them all at once in the application layer
- PSR-14 compliant — works with any PSR-14 compatible code
- 100% test coverage — no mocks, real execution only
Installation
composer require jardisadapter/eventdispatcher
Quick Start
Define an Event
use JardisAdapter\EventDispatcher\Event; final class OrderCreated extends Event { public function __construct( public readonly string $orderId, ) { } }
Register Listeners and Dispatch
use JardisAdapter\EventDispatcher\EventDispatcher; use JardisAdapter\EventDispatcher\ListenerProvider; $provider = new ListenerProvider(); $provider->listen(OrderCreated::class, function (OrderCreated $event): void { echo "Order {$event->orderId} created!"; }); $dispatcher = new EventDispatcher($provider); $dispatcher->dispatch(new OrderCreated('ORD-42'));
Listener Registration
Priority-based Registration
$provider->listen(OrderCreated::class, $sendConfirmation, priority: 10); // first $provider->listen(OrderCreated::class, $updateInventory, priority: 5); // second $provider->listen(OrderCreated::class, $logEvent); // last (0)
Higher number = higher priority = called first.
Remove a Listener
$provider->remove(OrderCreated::class, $sendConfirmation);
Type-Hierarchy Matching (Wildcard)
A listener on an interface or parent class catches all events that implement or extend it:
interface PaymentEventInterface {} final class PaymentReceived extends Event implements PaymentEventInterface {} final class PaymentFailed extends Event implements PaymentEventInterface {} // Catches both PaymentReceived AND PaymentFailed $provider->listen(PaymentEventInterface::class, $paymentAuditor); // Catches EVERY event that extends Event $provider->listen(Event::class, $globalLogger);
Direct and wildcard listeners are sorted together by priority.
Stoppable Events
A listener can stop further processing:
$provider->listen(OrderCreated::class, function (OrderCreated $event): void { if ($event->orderId === 'BLOCKED') { $event->stopPropagation(); // no further listeners will be called } }, priority: 100); $provider->listen(OrderCreated::class, function (OrderCreated $event): void { // Only called if stopPropagation() was NOT invoked });
Any event extending Event or implementing StoppableEventInterface supports this automatically.
EventCollector — Deferred Dispatch
Collect events in the domain layer, dispatch them later in the application layer:
use JardisAdapter\EventDispatcher\EventCollector; $collector = new EventCollector(); // In the domain layer — record events $collector->record(new OrderCreated($orderId)); $collector->record(new InventoryReserved($itemId)); // In the application layer — after the use case completes $collector->dispatchAll($dispatcher); // dispatches all, clears the list
The collector separates the occurrence of an event (domain) from its distribution (application). Ideal for use cases that produce multiple events.
$collector->count(); // number of collected events $collector->events(); // read events without dispatching $collector->clear(); // clear the list without dispatching
Error Handling
| Situation | Behavior |
|---|---|
| Listener throws an exception | Propagates unchanged to the caller |
| No listener registered | Event is silently ignored |
| Event already stopped | No listener is called |
No custom exception classes. Errors come from the listeners, not from the dispatcher.
Architecture
EventDispatcher (implements EventDispatcherInterface)
│
│ dispatch(object $event): object
│ └── iterates listeners, respects StoppableEventInterface
│
└── ListenerProvider (implements ListenerProviderInterface, EventListenerRegistryInterface)
│
├── listen() register listener with priority
├── remove() remove a listener
└── getListenersForEvent()
└── type-hierarchy matching + priority sorting
Event (abstract, implements StoppableEventInterface)
└── stopPropagation() / isPropagationStopped()
EventCollector
└── record() → dispatchAll() / events() / clear() / count()
The dispatcher is the postman — it receives the event and delivers it to all recipients. The listener provider is the address book. The event collector is the mailbox in the domain layer.
DDD Layer Rules
| Layer | Responsibility |
|---|---|
| Domain | Defines event classes. Does not dispatch — returns events instead |
| Application | Receives EventDispatcherInterface via injection. Dispatches after use case execution |
| Infrastructure | Registers listeners in the ListenerProvider |
Jardis Foundation Integration
In a Jardis DDD project, the dispatcher is wired into the DomainKernel via DomainApp::eventDispatcher():
// Inside a BoundedContext $dispatcher = $this->resource()->eventDispatcher(); if ($dispatcher !== null) { $dispatcher->dispatch(new OrderCreated($orderId)); }
Three-State Semantics
| Return value | Meaning |
|---|---|
EventDispatcher |
Dispatcher active, shared via ServiceRegistry |
null |
Package not installed — falls back to SharedRegistry |
false |
Event dispatching explicitly disabled |
Development
cp .env.example .env # Once make install # Install dependencies make phpunit # Run tests make phpstan # Static analysis (level 8) make phpcs # Coding standards (PSR-12)
Documentation
Full documentation, guides, and API reference:
docs.jardis.io/en/adapter/eventdispatcher
License
MIT License — free for any use, including commercial.
AI-Assisted Development
This package ships with a skill for Claude Code, Cursor, Continue, and Aider. Install it in your consuming project:
composer require --dev jardis/dev-skills
More details: https://docs.jardis.io/en/skills
jardisadapter/eventdispatcher 适用场景与选型建议
jardisadapter/eventdispatcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 133 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「event-dispatcher」 「php」 「event」 「dispatcher」 「listener」 「psr-14」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jardisadapter/eventdispatcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jardisadapter/eventdispatcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jardisadapter/eventdispatcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Interfaces, Traits and Nominal Classes used by Domain Entities Implementing Domain Events
Domain Event Implementation for Symfony and Doctrine
Implementation of the transactional outbox pattern on top of rekalogika/domain-event
PHP 5.2 port of Événement
Symfony bundle for broadway/broadway.
This is an Event Emitter equivalent to Node.js' Event Emitter.
统计信息
- 总下载量: 133
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 39
- 依赖项目数: 1
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-02