class ContainerAwareEventDispatcher implements EventDispatcherInterface (View source)

A performance optimized container aware event dispatcher.

This version of the event dispatcher contains the following optimizations in comparison to the Symfony event dispatcher component:

Faster instantiation of the event dispatcher service
Instead of calling ```addSubscriberService``` once for each subscriber, a precompiled array of listener definitions is passed directly to the constructor. This is faster by roughly an order of magnitude. The listeners are collected and prepared using a compiler pass.
Lazy instantiation of listeners
Services are only retrieved from the container just before invocation. Especially when dispatching the KernelEvents::REQUEST event, this leads to a more timely invocation of the first listener. Overall dispatch runtime is not affected by this change though.

Properties

protected ContainerInterface $container

The service container.

protected array $listeners

Listener definitions.

protected true[] $unsorted

Whether listeners need to be sorted prior to dispatch, keyed by event name.

Methods

__construct(ContainerInterface $container, array $listeners = [])

Constructs a container aware event dispatcher.

dispatch($event)

{@inheritdoc}

array
getListeners($event_name = NULL)

{@inheritdoc}

int|null
getListenerPriority($event_name, $listener)

{@inheritdoc}

bool
hasListeners($event_name = NULL)

{@inheritdoc}

addListener($event_name, $listener, $priority = 0)

{@inheritdoc}

removeListener($event_name, $listener)

{@inheritdoc}

addSubscriber(EventSubscriberInterface $subscriber)

{@inheritdoc}

removeSubscriber(EventSubscriberInterface $subscriber)

{@inheritdoc}

Details

__construct(ContainerInterface $container, array $listeners = [])

Constructs a container aware event dispatcher.

Parameters

ContainerInterface $container

The service container.

array $listeners

A nested array of listener definitions keyed by event name and priority. The array is expected to be ordered by priority. A listener definition is an associative array with one of the following key value pairs:

  • callable: A callable listener
  • service: An array of the form [service id, method] A service entry will be resolved to a callable only just before its invocation.

dispatch($event)

{@inheritdoc}

Parameters

$event

array getListeners($event_name = NULL)

{@inheritdoc}

Parameters

$event_name

Return Value

array

int|null getListenerPriority($event_name, $listener)

{@inheritdoc}

Parameters

$event_name
$listener

Return Value

int|null

bool hasListeners($event_name = NULL)

{@inheritdoc}

Parameters

$event_name

Return Value

bool

addListener($event_name, $listener, $priority = 0)

{@inheritdoc}

Parameters

$event_name
$listener
$priority

removeListener($event_name, $listener)

{@inheritdoc}

Parameters

$event_name
$listener

addSubscriber(EventSubscriberInterface $subscriber)

{@inheritdoc}

Parameters

EventSubscriberInterface $subscriber

removeSubscriber(EventSubscriberInterface $subscriber)

{@inheritdoc}

Parameters

EventSubscriberInterface $subscriber