Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / event-dispatcher / DependencyInjection / RegisterListenersPass.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\EventDispatcher\DependencyInjection;
13
14 use Symfony\Component\DependencyInjection\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17
18 /**
19  * Compiler pass to register tagged services for an event dispatcher.
20  */
21 class RegisterListenersPass implements CompilerPassInterface
22 {
23     /**
24      * @var string
25      */
26     protected $dispatcherService;
27
28     /**
29      * @var string
30      */
31     protected $listenerTag;
32
33     /**
34      * @var string
35      */
36     protected $subscriberTag;
37
38     /**
39      * Constructor.
40      *
41      * @param string $dispatcherService Service name of the event dispatcher in processed container
42      * @param string $listenerTag       Tag name used for listener
43      * @param string $subscriberTag     Tag name used for subscribers
44      */
45     public function __construct($dispatcherService = 'event_dispatcher', $listenerTag = 'kernel.event_listener', $subscriberTag = 'kernel.event_subscriber')
46     {
47         $this->dispatcherService = $dispatcherService;
48         $this->listenerTag = $listenerTag;
49         $this->subscriberTag = $subscriberTag;
50     }
51
52     public function process(ContainerBuilder $container)
53     {
54         if (!$container->hasDefinition($this->dispatcherService) && !$container->hasAlias($this->dispatcherService)) {
55             return;
56         }
57
58         $definition = $container->findDefinition($this->dispatcherService);
59
60         foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $events) {
61             $def = $container->getDefinition($id);
62             if (!$def->isPublic()) {
63                 throw new InvalidArgumentException(sprintf('The service "%s" must be public as event listeners are lazy-loaded.', $id));
64             }
65
66             if ($def->isAbstract()) {
67                 throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as event listeners are lazy-loaded.', $id));
68             }
69
70             foreach ($events as $event) {
71                 $priority = isset($event['priority']) ? $event['priority'] : 0;
72
73                 if (!isset($event['event'])) {
74                     throw new InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
75                 }
76
77                 if (!isset($event['method'])) {
78                     $event['method'] = 'on'.preg_replace_callback(array(
79                         '/(?<=\b)[a-z]/i',
80                         '/[^a-z0-9]/i',
81                     ), function ($matches) { return strtoupper($matches[0]); }, $event['event']);
82                     $event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
83                 }
84
85                 $definition->addMethodCall('addListenerService', array($event['event'], array($id, $event['method']), $priority));
86             }
87         }
88
89         foreach ($container->findTaggedServiceIds($this->subscriberTag) as $id => $attributes) {
90             $def = $container->getDefinition($id);
91             if (!$def->isPublic()) {
92                 throw new InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
93             }
94
95             if ($def->isAbstract()) {
96                 throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
97             }
98
99             // We must assume that the class value has been correctly filled, even if the service is created by a factory
100             $class = $container->getParameterBag()->resolveValue($def->getClass());
101             $interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
102
103             if (!is_subclass_of($class, $interface)) {
104                 if (!class_exists($class, false)) {
105                     throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
106                 }
107
108                 throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
109             }
110
111             $definition->addMethodCall('addSubscriberService', array($id, $class));
112         }
113     }
114 }