damasco / event-dispatcher
PSR-14 EventDispatcher
dev-master
2020-04-11 08:53 UTC
Requires
- php: ^7.2
- psr/container: 1.0.0
- psr/event-dispatcher: 1.0.0
Requires (Dev)
- php-di/php-di: ^6.1
- phpunit/phpunit: ^9.0
Suggests
Provides
This package is auto-updated.
Last update: 2024-12-11 20:24:20 UTC
README
Installation
$ composer require damasco/event-dispatcher
Providers
1. Simple provider
Example:
$provider = new Damasco\EventDispatcher\Provider\ListenerProvider(); $provider->addListener(ExampleEvent::class, function (ExampleEvent $event) { // code... }); $provider->addListener(ExampleEvent::class, new class { public function __invoke(ExampleEvent $event) { // code.. } }); $eventDispatcher = new Damasco\EventDispatcher\EventDispatcher($provider); $eventDispatcher->dispatch(new ExampleEvent(...));
2. Container aware provider
Example:
/** @var \Psr\Container\ContainerInterface $container */ $provider = new Damasco\EventDispatcher\Provider\ContainerAwareListenerProvider($container); $provider->addListener(ExampleEvent::class, function (ExampleEvent $event) { // code... }); $provider->addListener(ExampleEvent::class, new class { public function __invoke(ExampleEvent $event) { // code.. } }); // `ExampleEventListener` must be invokable class $provider->addListener(ExampleEvent::class, ExampleEventListener::class); $eventDispatcher = new Damasco\EventDispatcher\EventDispatcher($provider); $eventDispatcher->dispatch(new ExampleEvent(...));
Stoppable event
class StoppableEvent implements Psr\EventDispatcher\StoppableEventInterface { // code... public function isPropagationStopped(): bool { return true; } }