定制 jardisadapter/eventdispatcher 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jardisadapter/eventdispatcher

最新稳定版本:v1.0.0

Composer 安装命令:

composer require jardisadapter/eventdispatcher

包简介

PSR-14 event dispatcher with listener registry, priority ordering, and stoppable event support

README 文档

README

Build Status License: MIT PHP Version PHPStan Level PSR-12 PSR-14

Part of the Jardis Business Platform — Enterprise-grade PHP components for Domain-Driven Design

Domain Events 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 magicEventDispatcher, 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.

KI-gestützte Entwicklung

Dieses Package liefert einen Skill für Claude Code, Cursor, Continue und Aider mit. Installation im Konsumentenprojekt:

composer require --dev jardis/dev-skills

Mehr Details: https://docs.jardis.io/skills

统计信息

  • 总下载量: 49
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固