php-pico/event

A PSR-14 Event Dispatcher package.

Maintainers

Package info

github.com/php-pico/event

pkg:composer/php-pico/event

Transparency log

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.1.2 2026-07-07 09:10 UTC

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