piko/event-dispatcher

A basic PSR-14 implementation using a priority queue

v1.1 2022-11-13 19:35 UTC

This package is auto-updated.

Last update: 2024-11-14 00:04:20 UTC


README

Tests Coverage Status

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!