composer package updates
[openemr.git] / vendor / symfony / event-dispatcher / DependencyInjection / RegisterListenersPass.php
blob764517693c01ca991bb7b999e00a422568b322c7
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\EventDispatcher\DependencyInjection;
14 use Symfony\Component\DependencyInjection\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17 /**
18 * Compiler pass to register tagged services for an event dispatcher.
20 class RegisterListenersPass implements CompilerPassInterface
22 protected $dispatcherService;
23 protected $listenerTag;
24 protected $subscriberTag;
26 /**
27 * @param string $dispatcherService Service name of the event dispatcher in processed container
28 * @param string $listenerTag Tag name used for listener
29 * @param string $subscriberTag Tag name used for subscribers
31 public function __construct($dispatcherService = 'event_dispatcher', $listenerTag = 'kernel.event_listener', $subscriberTag = 'kernel.event_subscriber')
33 $this->dispatcherService = $dispatcherService;
34 $this->listenerTag = $listenerTag;
35 $this->subscriberTag = $subscriberTag;
38 public function process(ContainerBuilder $container)
40 if (!$container->hasDefinition($this->dispatcherService) && !$container->hasAlias($this->dispatcherService)) {
41 return;
44 $definition = $container->findDefinition($this->dispatcherService);
46 foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $events) {
47 $def = $container->getDefinition($id);
48 if (!$def->isPublic()) {
49 throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event listeners are lazy-loaded.', $id));
52 if ($def->isAbstract()) {
53 throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event listeners are lazy-loaded.', $id));
56 foreach ($events as $event) {
57 $priority = isset($event['priority']) ? $event['priority'] : 0;
59 if (!isset($event['event'])) {
60 throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
63 if (!isset($event['method'])) {
64 $event['method'] = 'on'.preg_replace_callback(array(
65 '/(?<=\b)[a-z]/i',
66 '/[^a-z0-9]/i',
67 ), function ($matches) { return strtoupper($matches[0]); }, $event['event']);
68 $event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
71 $definition->addMethodCall('addListenerService', array($event['event'], array($id, $event['method']), $priority));
75 foreach ($container->findTaggedServiceIds($this->subscriberTag) as $id => $attributes) {
76 $def = $container->getDefinition($id);
77 if (!$def->isPublic()) {
78 throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
81 if ($def->isAbstract()) {
82 throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
85 // We must assume that the class value has been correctly filled, even if the service is created by a factory
86 $class = $container->getParameterBag()->resolveValue($def->getClass());
87 $interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
89 if (!is_subclass_of($class, $interface)) {
90 if (!class_exists($class, false)) {
91 throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
94 throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
97 $definition->addMethodCall('addSubscriberService', array($id, $class));