Search by

solophp / event-dispatcher

Vitaliy Olos

PSR-14 event dispatcher with listener priorities, subscribers, type-hierarchy matching and optional PSR-3 failure logging

Package info

github.com/SoloPHP/Event-Dispatcher

Documentation

pkg:composer/solophp/event-dispatcher

Statistics

Installs: 224

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v2.0.0 2026-11-01 20:19 UTC

This package is auto-updated.

Last update: 2026-11-01 20:20:48 UTC


README

Latest Version on Packagist PHP Version License

PSR-14 event dispatcher for PHP. Listeners run by priority, then in registration order; a listener for a class or an interface receives its subtypes; subscribers group the handlers of one object. A listener failure reaches the caller unchanged, and an optional PSR-3 logger records which listener failed.

Installation

composer require solophp/event-dispatcher

Requires PHP 8.3+.

Example

use Solo\EventDispatcher\EventDispatcher;
use Solo\EventDispatcher\ListenerProvider;

final readonly class OrderPlaced
{
    public function __construct(public int $orderId)
    {
    }
}

$listeners = new ListenerProvider();
$listeners->addListener(OrderPlaced::class, static function (OrderPlaced $event): void {
    echo "Reserving stock for order {$event->orderId}\n";
}, priority: 10);
$listeners->addListener(OrderPlaced::class, static function (OrderPlaced $event): void {
    echo "Sending the confirmation of order {$event->orderId}\n";
});

$dispatcher = new EventDispatcher($listeners);
$dispatcher->dispatch(new OrderPlaced(42)); // returns the same event

An event is any object, and a listener is any callable that accepts it. Inject Psr\EventDispatcher\EventDispatcherInterface where events are dispatched; a listener can resolve its service from the container when the event arrives, so that building the dispatcher constructs nothing.

Documentation

Full Documentation

License

MIT. See LICENSE.