You are here

public function ContainerAwareEventDispatcher::dispatch in Drupal 10

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

File

core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php, line 88

Class

ContainerAwareEventDispatcher
A performance optimized container aware event dispatcher.

Namespace

Drupal\Component\EventDispatcher

Code

public function dispatch(object $event, ?string $eventName = NULL) : object {
  $event_name = $eventName ?? get_class($event);
  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 &$definitions) {
      foreach ($definitions as &$definition) {
        if (!isset($definition['callable'])) {
          $definition['callable'] = [
            $this->container
              ->get($definition['service'][0]),
            $definition['service'][1],
          ];
        }
        if (is_array($definition['callable']) && isset($definition['callable'][0]) && $definition['callable'][0] instanceof \Closure) {
          $definition['callable'][0] = $definition['callable'][0]();
        }
        call_user_func($definition['callable'], $event, $event_name, $this);
        if ($event
          ->isPropagationStopped()) {
          return $event;
        }
      }
    }
  }
  return $event;
}