EventDispatcher

Class
implements EventDispatcherInterface
Read only: Yes Final: Yes

Dispatch events through PSR-14 listener providers and optional named wrappers.

Description

This dispatcher resolves listeners for the original event object first. When the event is not already a NamedEvent, it dispatches a named wrapper afterward so listeners registered by string identifier can observe the same event instance.

Tags
see
EventDispatcherInterface

Table of Contents

Interfaces

Properties

 : ListenerProviderInterface

Methods

__construct()

Create a dispatcher backed by the provided listener provider.

 : mixed
dispatch()

Dispatch an event to all matching listeners.

 : object
callListeners()

Invoke the provided listeners until propagation stops or a listener fails.

 : void
handleThrowable()

Dispatch an error event for a listener failure and rethrow the original throwable.

 : void
Properties

$listenerProvider

Private
private ListenerProviderInterface $listenerProvider
Methods

__construct()

Public

Create a dispatcher backed by the provided listener provider.

public __construct(ListenerProviderInterface  $listenerProvider) : mixed
Parameters
$listenerProvider : ListenerProviderInterface

Description

listener provider used to resolve event listeners

dispatch()

Public

Dispatch an event to all matching listeners.

public dispatch( object  $event[, string|null  $eventName = null]) : object

Description

Stoppable events are returned immediately when propagation has already been halted. When a listener throws during a non-error dispatch, the dispatcher emits an ErrorEvent before rethrowing the original throwable.

Parameters
$event : object

Description

event object to dispatch

$eventName : string|null = null

Description

explicit name for the generated NamedEvent wrapper

Tags
throws
Throwable

Description

thrown when a listener failure is not absorbed by an error-event listener

Return values
object

Description

the dispatched event instance

callListeners()

Private

Invoke the provided listeners until propagation stops or a listener fails.

private callListeners( iterable<string|int, callable>  $listeners, object  $event) : void
Parameters
$listeners : iterable<string|int, callable>

Description

listeners resolved for the current event

$event : object

Description

event instance passed to each listener

Tags
throws
Throwable

Description

thrown when listener failure handling rethrows an error

handleThrowable()

Private

Dispatch an error event for a listener failure and rethrow the original throwable.

private handleThrowable(Throwable  $throwable, object  $event, callable  $listener) : void

Description

Error events are not wrapped again, which prevents recursive error dispatch when an error listener itself fails.

Parameters
$throwable : Throwable

Description

throwable raised by the listener

$event : object

Description

event being dispatched when the failure occurred

$listener : callable

Description

listener that raised the throwable

Tags
throws
Throwable

Description

always rethrows after attempting error dispatch