Skip to content

nimp-dev/observer

Repository files navigation

Nimp Observer

Tests PHPStan Code Coverage PHP Version License

Minimalistic implementation of event dispatching according to PSR-14: EventDispatcher + ListenerProvider.

Installation

composer require nimp/observer

Quick start

final 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);

Tests

composer install && composer test

About

Minimalistic implementation of event dispatching according to PSR-14: EventDispatcher + ListenerProvider.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages