Minimalistic implementation of event dispatching according to PSR-14: EventDispatcher + ListenerProvider.
composer require nimp/observerfinal class MyListener implements EventListenerInterface
{
public function events(): iterable
{
yield StartedEvent::class => $this->onStarted(...);
yield MyEvent::class => 'onMyEvent';
yield MyStoppableEvent::class => function (MyStoppableEvent $e): void {
// handle and stop propagation if needed
$e->stop();
};
}
public function onStarted(StartedEvent $event): void
{
// handle StartedEvent
}
public function onMyEvent(object $event): void
{
// handle MyEvent
}
}
$provider = new ListenerProvider();
$provider->addListeners(new MyListener());
$dispatcher = new EventDispatcher($provider);composer install && composer test