3 namespace Drupal\Core\DependencyInjection\Compiler;
5 use Symfony\Component\DependencyInjection\ContainerBuilder;
6 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9 * Registers all event subscribers to the event dispatcher.
11 class RegisterEventSubscribersPass implements CompilerPassInterface {
16 public function process(ContainerBuilder $container) {
17 if (!$container->hasDefinition('event_dispatcher')) {
21 $definition = $container->getDefinition('event_dispatcher');
23 $event_subscriber_info = [];
24 foreach ($container->findTaggedServiceIds('event_subscriber') as $id => $attributes) {
26 // We must assume that the class value has been correctly filled, even if
27 // the service is created by a factory.
28 $class = $container->getDefinition($id)->getClass();
30 $refClass = new \ReflectionClass($class);
31 $interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
32 if (!$refClass->implementsInterface($interface)) {
33 throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
36 // Get all subscribed events.
37 foreach ($class::getSubscribedEvents() as $event_name => $params) {
38 if (is_string($params)) {
40 $event_subscriber_info[$event_name][$priority][] = ['service' => [$id, $params]];
42 elseif (is_string($params[0])) {
43 $priority = isset($params[1]) ? $params[1] : 0;
44 $event_subscriber_info[$event_name][$priority][] = ['service' => [$id, $params[0]]];
47 foreach ($params as $listener) {
48 $priority = isset($listener[1]) ? $listener[1] : 0;
49 $event_subscriber_info[$event_name][$priority][] = ['service' => [$id, $listener[0]]];
55 foreach (array_keys($event_subscriber_info) as $event_name) {
56 krsort($event_subscriber_info[$event_name]);
59 $definition->addArgument($event_subscriber_info);