function ContainerAwareEventDispatcher::dispatch

Same name in other branches
  1. 8.9.x core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher::dispatch()
  2. 10 core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher::dispatch()
  3. 11.x core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher::dispatch()

File

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

Class

ContainerAwareEventDispatcher
A performance optimized container aware event dispatcher.

Namespace

Drupal\Component\EventDispatcher

Code

public function dispatch($event) {
    $event_name = 1 < \func_num_args() ? func_get_arg(1) : NULL;
    if (\is_object($event)) {
        $class_name = get_class($event);
        $event_name = $event_name ?? $class_name;
        $deprecation_message = 'Symfony\\Component\\EventDispatcher\\Event is deprecated in drupal:9.1.0 and will be replaced by Symfony\\Contracts\\EventDispatcher\\Event in drupal:10.0.0. A new Drupal\\Component\\EventDispatcher\\Event class is available to bridge the two versions of the class. See https://www.drupal.org/node/3159012';
        // Trigger a deprecation error if the deprecated Event class is used
        // directly.
        if ($class_name === 'Symfony\\Component\\EventDispatcher\\Event') {
            @trigger_error($deprecation_message, E_USER_DEPRECATED);
        }
        elseif ($class_name !== 'Drupal\\Component\\EventDispatcher\\Event' && strpos($class_name, 'Drupal') !== FALSE) {
            if (get_parent_class($event) === 'Symfony\\Component\\EventDispatcher\\Event') {
                @trigger_error($deprecation_message, E_USER_DEPRECATED);
            }
        }
    }
    elseif (\is_string($event) && (NULL === $event_name || $event_name instanceof ContractsEvent || $event_name instanceof Event)) {
        @trigger_error('Calling the Symfony\\Component\\EventDispatcher\\EventDispatcherInterface::dispatch() method with a string event name as the first argument is deprecated in drupal:9.1.0, an Event object will be required instead in drupal:10.0.0. See https://www.drupal.org/node/3154407', E_USER_DEPRECATED);
        $swap = $event;
        $event = $event_name ?? new Event();
        $event_name = $swap;
    }
    else {
        throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an object, %s given.', ContractsEventDispatcherInterface::class, \gettype($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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.