piko/事件分发器

基于优先队列的 PSR-14 实现的基本版本

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

This package is auto-updated.

Last update: 2024-09-13 23:43:24 UTC


README

Tests Coverage Status

此包提供了一个简单的使用事件优先队列的事件分发器,遵循PSR-14 事件分发器建议。

安装

通过 Composer

composer require piko/event-dispatcher

然后确保以下文件包含在您的 PHP 项目中

require 'vendor/autoload.php'; // The Composer autoloader

使用方法

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!