php-pico/event
Composer 安装命令:
composer require php-pico/event
包简介
A PSR-14 Event Dispather package.
README 文档
README
A PSR-14 Event Dispatcher package.
Installation
composer require php-pico/event
Requires PHP ^8.5.
Usage
Registering listeners
use PhpPico\Event\ListenerProvider; $listenerProvider = new ListenerProvider(); $listenerProvider->listen(UserRegistered::class, function (UserRegistered $event): void { // handle the event });
Listeners also receive events for classes implementing the registered interface, or extending the registered class:
$listenerProvider->listen(Throwable::class, function (Throwable $event): void { // invoked for any Throwable, including subclasses });
Dispatching events
use PhpPico\Event\EventDispatcher; $dispatcher = new EventDispatcher($listenerProvider); $dispatcher->dispatch(new UserRegistered($user));
Listeners are invoked in the order they were registered.
Stoppable events
Events implementing Psr\EventDispatcher\StoppableEventInterface can halt propagation to remaining listeners:
use Psr\EventDispatcher\StoppableEventInterface; final class UserRegistered implements StoppableEventInterface { private bool $stopped = false; public function stop(): void { $this->stopped = true; } public function isPropagationStopped(): bool { return $this->stopped; } }
Once stop() is called from within a listener, no further listeners are invoked for that dispatch.
Extend PhpPico\Event\AbstractStoppableEvent to get this behavior for free:
use PhpPico\Event\AbstractStoppableEvent; final class UserRegistered extends AbstractStoppableEvent { public function __construct( public readonly User $user, ) {} }
AbstractStoppableEvent already implements stop() and isPropagationStopped(), so the event class only needs to declare its own data.
Testing
composer test
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-07-06