Documentation

Dispatcher extends EventDispatcher
in package

Event Dispatching class.

This class provides a bridge to the Symfony2 EventDispatcher. At current this is provided by inheritance but future iterations should solve this by making it an adapter pattern.

The class is implemented as (mockable) Singleton as this was the best solution to make the functionality available in every class of the project.

Table of Contents

$instances  : array<string|int, Dispatcher>
$listeners  : mixed
$optimized  : mixed
$sorted  : mixed
__construct()  : mixed
addListener()  : mixed
Adds an event listener that listens on the specified events.
addSubscriber()  : mixed
Adds an event subscriber.
dispatch()  : object
{@inheritdoc}
getInstance()  : self
Returns a named instance of the Event Dispatcher.
getListenerPriority()  : int|null
Gets the listener priority for a specific event.
getListeners()  : array<string|int, callable[]|callable>
Gets the listeners of a specific event or all listeners sorted by descending priority.
hasListeners()  : bool
Checks whether an event has any registered listeners.
removeListener()  : mixed
Removes an event listener from the specified events.
removeSubscriber()  : mixed
setInstance()  : void
Sets a names instance of the Event Dispatcher.
callListeners()  : mixed
Triggers the listeners of an event.
optimizeListeners()  : array<string|int, mixed>
Optimizes the internal list of listeners for the given event by priority.
sortListeners()  : mixed
Sorts the internal list of listeners for the given event by priority.

Properties

$instances

protected static array<string|int, Dispatcher> $instances = []

Keep track of an array of instances.

Methods

addListener()

Adds an event listener that listens on the specified events.

public addListener(string $eventName, mixed $listener, int $priority) : mixed
Parameters
$eventName : string
$listener : mixed
$priority : int

The higher this value, the earlier an event listener will be triggered in the chain (defaults to 0)

Return values
mixed

dispatch()

{@inheritdoc}

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

getInstance()

Returns a named instance of the Event Dispatcher.

public static getInstance([string $name = 'default' ]) : self
Parameters
$name : string = 'default'
Return values
self

getListenerPriority()

Gets the listener priority for a specific event.

public getListenerPriority(string $eventName, mixed $listener) : int|null
Parameters
$eventName : string
$listener : mixed
Return values
int|null

getListeners()

Gets the listeners of a specific event or all listeners sorted by descending priority.

public getListeners([string|null $eventName = null ]) : array<string|int, callable[]|callable>
Parameters
$eventName : string|null = null
Return values
array<string|int, callable[]|callable>

hasListeners()

Checks whether an event has any registered listeners.

public hasListeners([string|null $eventName = null ]) : bool
Parameters
$eventName : string|null = null
Return values
bool

removeListener()

Removes an event listener from the specified events.

public removeListener(string $eventName, mixed $listener) : mixed
Parameters
$eventName : string
$listener : mixed
Return values
mixed

setInstance()

Sets a names instance of the Event Dispatcher.

public static setInstance(string $name, self $instance) : void
Parameters
$name : string
$instance : self
Return values
void

callListeners()

Triggers the listeners of an event.

protected callListeners(array<string|int, callable> $listeners, string $eventName, object $event) : mixed

This method can be overridden to add functionality that is executed for each listener.

Parameters
$listeners : array<string|int, callable>

The event listeners

$eventName : string

The name of the event to dispatch

$event : object

The event object to pass to the event handlers/listeners

Return values
mixed

optimizeListeners()

Optimizes the internal list of listeners for the given event by priority.

private optimizeListeners(string $eventName) : array<string|int, mixed>
Parameters
$eventName : string
Return values
array<string|int, mixed>

sortListeners()

Sorts the internal list of listeners for the given event by priority.

private sortListeners(string $eventName) : mixed
Parameters
$eventName : string
Return values
mixed

Search results