Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / DependencyInjection / Compiler / RegisterEventSubscribersPass.php
1 <?php
2
3 namespace Drupal\Core\DependencyInjection\Compiler;
4
5 use Symfony\Component\DependencyInjection\ContainerBuilder;
6 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
8 /**
9  * Registers all event subscribers to the event dispatcher.
10  */
11 class RegisterEventSubscribersPass implements CompilerPassInterface {
12
13   /**
14    * {@inheritdoc}
15    */
16   public function process(ContainerBuilder $container) {
17     if (!$container->hasDefinition('event_dispatcher')) {
18       return;
19     }
20
21     $definition = $container->getDefinition('event_dispatcher');
22
23     $event_subscriber_info = [];
24     foreach ($container->findTaggedServiceIds('event_subscriber') as $id => $attributes) {
25
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();
29
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));
34       }
35
36       // Get all subscribed events.
37       foreach ($class::getSubscribedEvents() as $event_name => $params) {
38         if (is_string($params)) {
39           $priority = 0;
40           $event_subscriber_info[$event_name][$priority][] = ['service' => [$id, $params]];
41         }
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]]];
45         }
46         else {
47           foreach ($params as $listener) {
48             $priority = isset($listener[1]) ? $listener[1] : 0;
49             $event_subscriber_info[$event_name][$priority][] = ['service' => [$id, $listener[0]]];
50           }
51         }
52       }
53     }
54
55     foreach (array_keys($event_subscriber_info) as $event_name) {
56       krsort($event_subscriber_info[$event_name]);
57     }
58
59     $definition->addArgument($event_subscriber_info);
60   }
61
62 }