class RegisterEventSubscribersPass

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterEventSubscribersPass.php \Drupal\Core\DependencyInjection\Compiler\RegisterEventSubscribersPass
  2. 8.9.x core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterEventSubscribersPass.php \Drupal\Core\DependencyInjection\Compiler\RegisterEventSubscribersPass
  3. 11.x core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterEventSubscribersPass.php \Drupal\Core\DependencyInjection\Compiler\RegisterEventSubscribersPass

Registers all event subscribers to the event dispatcher.

Hierarchy

  • class \Drupal\Core\DependencyInjection\Compiler\RegisterEventSubscribersPass extends \Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface

Expanded class hierarchy of RegisterEventSubscribersPass

1 file declares its use of RegisterEventSubscribersPass
CoreServiceProvider.php in core/lib/Drupal/Core/CoreServiceProvider.php

File

core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterEventSubscribersPass.php, line 12

Namespace

Drupal\Core\DependencyInjection\Compiler
View source
class RegisterEventSubscribersPass implements CompilerPassInterface {
  
  /**
   * {@inheritdoc}
   */
  public function process(ContainerBuilder $container) {
    if (!$container->hasDefinition('event_dispatcher')) {
      return;
    }
    $definition = $container->getDefinition('event_dispatcher');
    $event_subscriber_info = [];
    foreach ($container->findTaggedServiceIds('event_subscriber') as $id => $attributes) {
      // We must assume that the class value has been correctly filled, even if
      // the service is created by a factory.
      $class = $container->getDefinition($id)
        ->getClass();
      $interface = EventSubscriberInterface::class;
      if (!is_subclass_of($class, $interface)) {
        throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
      }
      // Get all subscribed events.
      foreach ($class::getSubscribedEvents() as $event_name => $params) {
        if (is_string($params)) {
          $priority = 0;
          $event_subscriber_info[$event_name][$priority][] = [
            'service' => [
              $id,
              $params,
            ],
          ];
        }
        elseif (is_string($params[0])) {
          $priority = $params[1] ?? 0;
          $event_subscriber_info[$event_name][$priority][] = [
            'service' => [
              $id,
              $params[0],
            ],
          ];
        }
        else {
          foreach ($params as $listener) {
            $priority = $listener[1] ?? 0;
            $event_subscriber_info[$event_name][$priority][] = [
              'service' => [
                $id,
                $listener[0],
              ],
            ];
          }
        }
      }
    }
    foreach (array_keys($event_subscriber_info) as $event_name) {
      krsort($event_subscriber_info[$event_name]);
    }
    $definition->addArgument($event_subscriber_info);
  }

}

Members

Title Sort descending Modifiers Object type Summary
RegisterEventSubscribersPass::process public function

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