solophp / event-dispatcher
PSR-14 event dispatcher with listener priorities, subscribers, type-hierarchy matching and optional PSR-3 failure logging
Requires
- php: ^8.3
- psr/event-dispatcher: ^1.0
- psr/log: ^3.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^12.3
- squizlabs/php_codesniffer: ^3.13
Suggests
None
Provides
Conflicts
None
Replaces
None
README
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
- Getting Started
- Priorities and Order
- Event Hierarchies
- Subscribers
- Stopping Propagation
- Failures and Logging
- Container Wiring
- API Reference
- Behavior in Detail
- Upgrading from 1.x
- Changelog
License
MIT. See LICENSE.