php-pico / event
A PSR-14 Event Dispatcher package.
Requires
- php: ^8.5
- psr/event-dispatcher: ^1.0
Requires (Dev)
- carthage-software/mago: ^1.30.0
- phpunit/phpunit: ^12.5.30
This package is auto-updated.
Last update: 2026-07-07 09:11:30 UTC
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