https://github.com/piko-framework/event-dispatcher
A simple event dispatcher using a priority queue and compliant with the PSR-14
https://github.com/piko-framework/event-dispatcher
Last synced: over 1 year ago
JSON representation
A simple event dispatcher using a priority queue and compliant with the PSR-14
- Host: GitHub
- URL: https://github.com/piko-framework/event-dispatcher
- Owner: piko-framework
- License: mit
- Created: 2022-10-26T15:12:35.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-13T19:42:53.000Z (over 3 years ago)
- Last Synced: 2024-04-14T04:43:58.814Z (about 2 years ago)
- Language: PHP
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Piko event-dispatcher
[](https://github.com/piko-framework/event-dispatcher/actions/workflows/php.yml)
[](https://coveralls.io/github/piko-framework/event-dispatcher?branch=main)
This package offers a simple event dispatcher using an event priority queue, following the [PSR-14](https://www.php-fig.org/psr/psr-14/) Event Dispatcher recommendation.
## Installation
Via Composer
```bash
composer require piko/event-dispatcher
```
Then ensure that the following file is included in your PHP project:
```php
require 'vendor/autoload.php'; // The Composer autoloader
```
## usage
```php
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!
```