ContainerAwareEventDispatcher
extends EventDispatcher
in package
Lazily loads listeners and subscribers from the dependency injection container.
Tags
Table of Contents
Properties
- $container : ContainerInterface
- The container from where services are loaded.
- $listenerIds : array<string|int, mixed>
- The service IDs of the event listeners and subscribers.
- $listeners : array<string|int, mixed>
- The services registered as listeners.
- $sorted : mixed
Methods
- __construct() : mixed
- Constructor.
- addListener() : mixed
- Adds an event listener that listens on the specified events.
- addListenerService() : mixed
- Adds a service as event listener.
- addSubscriber() : mixed
- Adds an event subscriber.
- addSubscriberService() : mixed
- Adds a service as event subscriber.
- dispatch() : Event
- Dispatches an event to all registered listeners.
- getContainer() : mixed
- getListenerPriority() : int|null
- Gets the listener priority for a specific event.
- getListeners() : array<string|int, mixed>
- 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
- Removes an event subscriber.
- doDispatch() : mixed
- Triggers the listeners of an event.
- lazyLoad() : mixed
- Lazily loads listeners for this event from the dependency injection container.
- sortListeners() : mixed
- Sorts the internal list of listeners for the given event by priority.
Properties
$container
The container from where services are loaded.
private
ContainerInterface
$container
$listenerIds
The service IDs of the event listeners and subscribers.
private
array<string|int, mixed>
$listenerIds
= array()
$listeners
The services registered as listeners.
private
array<string|int, mixed>
$listeners
= array()
$sorted
private
mixed
$sorted
= array()
Methods
__construct()
Constructor.
public
__construct(ContainerInterface $container) : mixed
Parameters
- $container : ContainerInterface
-
A ContainerInterface instance
addListener()
Adds an event listener that listens on the specified events.
public
addListener(mixed $eventName, mixed $listener[, mixed $priority = 0 ]) : mixed
Parameters
- $eventName : mixed
-
The event to listen on
- $listener : mixed
-
The listener
- $priority : mixed = 0
-
The higher this value, the earlier an event listener will be triggered in the chain (defaults to 0)
addListenerService()
Adds a service as event listener.
public
addListenerService(string $eventName, array<string|int, mixed> $callback[, int $priority = 0 ]) : mixed
Parameters
- $eventName : string
-
Event for which the listener is added
- $callback : array<string|int, mixed>
-
The service ID of the listener service & the method name that has to be called
- $priority : int = 0
-
The higher this value, the earlier an event listener will be triggered in the chain. Defaults to 0.
Tags
addSubscriber()
Adds an event subscriber.
public
addSubscriber(EventSubscriberInterface $subscriber) : mixed
Parameters
- $subscriber : EventSubscriberInterface
-
The subscriber.
addSubscriberService()
Adds a service as event subscriber.
public
addSubscriberService(string $serviceId, string $class) : mixed
Parameters
- $serviceId : string
-
The service ID of the subscriber service
- $class : string
-
The service's class name (which must implement EventSubscriberInterface)
dispatch()
Dispatches an event to all registered listeners.
public
dispatch(mixed $eventName[, Event $event = null ]) : Event
Parameters
- $eventName : mixed
-
The name of the event to dispatch. The name of the event is the name of the method that is invoked on listeners.
- $event : Event = null
-
The event to pass to the event handlers/listeners. If not supplied, an empty Event instance is created.
Return values
EventgetContainer()
public
getContainer() : mixed
getListenerPriority()
Gets the listener priority for a specific event.
public
getListenerPriority(mixed $eventName, mixed $listener) : int|null
Parameters
- $eventName : mixed
-
The name of the event
- $listener : mixed
-
The listener
Return values
int|null —The event listener priority
getListeners()
Gets the listeners of a specific event or all listeners sorted by descending priority.
public
getListeners([mixed $eventName = null ]) : array<string|int, mixed>
Parameters
- $eventName : mixed = null
-
The name of the event
Return values
array<string|int, mixed> —The event listeners for the specified event, or all event listeners by event name
hasListeners()
Checks whether an event has any registered listeners.
public
hasListeners([mixed $eventName = null ]) : bool
Parameters
- $eventName : mixed = null
-
The name of the event
Return values
bool —true if the specified event has any listeners, false otherwise
removeListener()
Removes an event listener from the specified events.
public
removeListener(mixed $eventName, mixed $listener) : mixed
Parameters
- $eventName : mixed
-
The event to remove a listener from
- $listener : mixed
-
The listener to remove
removeSubscriber()
Removes an event subscriber.
public
removeSubscriber(EventSubscriberInterface $subscriber) : mixed
Parameters
- $subscriber : EventSubscriberInterface
-
The subscriber
doDispatch()
Triggers the listeners of an event.
protected
doDispatch(array<string|int, callable> $listeners, string $eventName, Event $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 : Event
-
The event object to pass to the event handlers/listeners.
lazyLoad()
Lazily loads listeners for this event from the dependency injection container.
protected
lazyLoad(string $eventName) : mixed
Parameters
- $eventName : string
-
The name of the event to dispatch. The name of the event is the name of the method that is invoked on listeners.
sortListeners()
Sorts the internal list of listeners for the given event by priority.
private
sortListeners(string $eventName) : mixed
Parameters
- $eventName : string
-
The name of the event.