koncnyjakub/event-dispatcher

一个简单的PSR-14事件调度器

1.1.0 2024-09-15 18:34 UTC

This package is auto-updated.

Last update: 2024-09-15 16:36:03 UTC


README

Total Downloads Latest Stable Version build status coverage report License

这是一个简单的PSR-14事件调度器,它允许注册回调作为事件监听器。它还支持来自psr的停止事件,并提供监听器提供程序,允许设置监听器的优先级或同时使用多个监听器提供程序。

安装

安装事件调度器的最佳方式是通过Composer。只需将konecnyjakub/event-dispatcher添加到您的依赖中。

快速入门

<?php
declare(strict_types=1);

use Konecnyjakub\EventDispatcher\EventDispatcher;
use Konecnyjakub\EventDispatcher\ListenerProvider;

class MyEvent {

}

$listenerProvider = new ListenerProvider();
$listenerProvider->registerListener(MyEvent::class, function (MyEvent $event) {
    echo "Event triggered\n";
});
$eventDispatcher = new EventDispatcher($listenerProvider);
$eventDispatcher->dispatch(new MyEvent());

高级用法

一次性注册多个监听器

在监听器提供程序中,您可以同时注册多个监听器,只需将数组/可迭代数组传递给registerListeners方法。

declare(strict_types=1);

use Konecnyjakub\EventDispatcher\EventDispatcher;
use Konecnyjakub\EventDispatcher\ListenerProvider;

class MyEvent {

}

$listenerProvider = new ListenerProvider();
$listenerProvider->registerListeners(MyEvent::class, ["time", "pi", ]);
$eventDispatcher = new EventDispatcher($listenerProvider);
$eventDispatcher->dispatch(new MyEvent());

监听器优先级

此库提供支持设置监听器优先级的监听器提供程序,优先级高的监听器将在优先级低的监听器之前触发。示例

<?php
declare(strict_types=1);

use Konecnyjakub\EventDispatcher\EventDispatcher;
use Konecnyjakub\EventDispatcher\PriorityListenerProvider;

class MyEvent {

}

$listenerProvider = new PriorityListenerProvider();
$listenerProvider->registerListener(MyEvent::class, "time", 0);
$listenerProvider->registerListener(MyEvent::class, "pi", 1);
$eventDispatcher = new EventDispatcher($listenerProvider);
$eventDispatcher->dispatch(new MyEvent());

在示例中,函数pi在函数time之前被调用。

多个监听器提供程序

如果您需要同时使用多个监听器提供程序,只需使用ChainListenerProvider。

<?php
declare(strict_types=1);

use Konecnyjakub\EventDispatcher\ChainListenerProvider;
use Konecnyjakub\EventDispatcher\EventDispatcher;
use Konecnyjakub\EventDispatcher\ListenerProvider;

class MyEvent {

}

$listenerProvider = new ChainListenerProvider();
$provider1 = new ListenerProvider();
$provider1->registerListener(MyEvent::class, "time");
$provider2 = new ListenerProvider();
$provider2->registerListener(MyEvent::class, "pi");
$listenerProvider->registerProvider($provider1);
$listenerProvider->registerProvider($provider2);
$eventDispatcher = new EventDispatcher($listenerProvider);
$eventDispatcher->dispatch(new MyEvent());

可停止事件

提供的事件调度器支持可停止的事件(如psr中定义的)。我们还提供了TStoppableEvent特质,您可以在事件类中使用它。

<?php
declare(strict_types=1);

use Konecnyjakub\EventDispatcher\EventDispatcher;
use Konecnyjakub\EventDispatcher\ListenerProvider;
use Konecnyjakub\EventDispatcher\TStoppableEvent;

class MyEvent {
    use TStoppableEvent;
}

$listenerProvider = new ListenerProvider();
$listenerProvider->registerListener(MyEvent::class, function (MyEvent $event) {
    echo "Event triggered\n";
    $event->stopPropagation();
});
$listenerProvider->registerListener(MyEvent::class, "time");
$eventDispatcher = new EventDispatcher($listenerProvider);
$eventDispatcher->dispatch(new MyEvent());