piko / event-dispatcher
A basic PSR-14 implementation using a priority queue
v1.1
2022-11-13 19:35 UTC
Requires
- php: >=7.1.0
- psr/event-dispatcher: ^1.0
Requires (Dev)
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
Provides
README
This package offers a simple event dispatcher using an event priority queue, following the PSR-14 Event Dispatcher recommendation.
Installation
Via Composer
composer require piko/event-dispatcher
Then ensure that the following file is included in your PHP project:
require 'vendor/autoload.php'; // The Composer autoloader
usage
use Piko\Event; use Piko\ListenerProvider; use Piko\EventDispatcher; class MyEvent extends \Piko\Event { public $value; } $provider = new ListenerProvider(); $dispatcher = new EventDispatcher($provider); $event = new MyEvent(); $provider->addListenerForEvent(MyEvent::class, function(MyEvent $event) { $event->value .= 'World !'; }); $provider->addListenerForEvent(MyEvent::class, function(MyEvent $event) { $event->value .= 'Hello '; }, 10); // Set the priority to 10 $dispatcher->dispatch($event); echo $event->value; // Hello World!