Skip to main content

Dirthara Events

Dirthara Events is a PSR-14 event dispatcher. EventDispatcher implements EventDispatcherInterface and ListenerProvider implements ListenerProviderInterface, so either can be combined with any other PSR-14 implementation, and code written against the interfaces can swap them out.

Events are plain PHP objects​

An event is any object. Dirthara has no event interface, base class, trait, or attribute, and an event does not have to extend or implement anything:

final readonly class UserWasCreated
{
public function __construct(
public User $user,
) {}
}

An application can still give its events a common interface of its own, such as DomainEvent, and listen for that interface; see matching parent classes and interfaces.

A first event​

use Dirthara\Events\EventDispatcher;
use Dirthara\Events\ListenerProvider;

$provider = new ListenerProvider();

$provider->listen(
UserWasCreated::class,
static function (UserWasCreated $event): void {
// react to the event
},
);

$dispatcher = new EventDispatcher($provider);

$dispatcher->dispatch(
new UserWasCreated($user),
);

ListenerProvider holds the listeners and decides which of them apply to an event, and in which order. EventDispatcher asks its provider for those listeners and calls each of them in turn with the event.

Code that only announces an event, and does not rely on its listeners, can publish it through an EventPublisher instead of dispatching it.

PageCovers
Listening for eventsRegistering listeners, priorities, parent class and interface matching, and duplicate registrations.
Dispatching eventsThe dispatcher, stoppable events, and exceptions thrown by listeners.
Publishing eventsOne-way publishing, and when to publish rather than dispatch.
Deferred publishingBuffering published events and flushing them later.
InstallationRequirements and installation.