You are here

public function ContainerAwareEventDispatcher::dispatch in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher::dispatch()

Dispatches an event to all registered listeners.

Parameters

string $eventName 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 The event to pass to the event handlers/listeners.: If not supplied, an empty Event instance is created.

Return value

Event

Overrides EventDispatcherInterface::dispatch

File

core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php, line 94
Contains \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher.

Class

ContainerAwareEventDispatcher
A performance optimized container aware event dispatcher.

Namespace

Drupal\Component\EventDispatcher

Code

public function dispatch($event_name, Event $event = NULL) {
  if ($event === NULL) {
    $event = new Event();
  }
  $event
    ->setDispatcher($this);
  $event
    ->setName($event_name);
  if (isset($this->listeners[$event_name])) {

    // Sort listeners if necessary.
    if (isset($this->unsorted[$event_name])) {
      krsort($this->listeners[$event_name]);
      unset($this->unsorted[$event_name]);
    }

    // Invoke listeners and resolve callables if necessary.
    foreach ($this->listeners[$event_name] as $priority => &$definitions) {
      foreach ($definitions as $key => &$definition) {
        if (!isset($definition['callable'])) {
          $definition['callable'] = [
            $this->container
              ->get($definition['service'][0]),
            $definition['service'][1],
          ];
        }
        $definition['callable']($event, $event_name, $this);
        if ($event
          ->isPropagationStopped()) {
          return $event;
        }
      }
    }
  }
  return $event;
}