piko / 事件分发器
基于优先队列的 PSR-14 实现的基本版本
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
此包提供了一个简单的使用事件优先队列的事件分发器,遵循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!