PSR-14 throughout
EventDispatcher and ListenerProvider implement PSR-14. The dispatcher accepts any PSR-14 provider, and application code depends on EventDispatcherInterface alone.
A PSR-14 event dispatcher with priorities, subscribers and listeners matched by class, parent class or interface.
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);
$event = $dispatcher->dispatch(new OrderPlaced(42)); // the same objectAn event is any object, and a listener is any callable that accepts it. Read the getting-started guide and the API reference for subscribers, stoppable events, failures and container wiring.
PHP 8.3+ is required. Composer installs psr/event-dispatcher ^1.0 and psr/log ^3.0 with the package.
composer require solophp/event-dispatcherContinue with the getting-started guide.