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;
18 * Compiler pass to register tagged services for an event dispatcher.
20 class RegisterListenersPass implements CompilerPassInterface
25 protected $dispatcherService;
30 protected $listenerTag;
35 protected $subscriberTag;
40 * @param string $dispatcherService Service name of the event dispatcher in processed container
41 * @param string $listenerTag Tag name used for listener
42 * @param string $subscriberTag Tag name used for subscribers
44 public function __construct($dispatcherService = 'event_dispatcher', $listenerTag = 'kernel.event_listener', $subscriberTag = 'kernel.event_subscriber')
46 $this->dispatcherService = $dispatcherService;
47 $this->listenerTag = $listenerTag;
48 $this->subscriberTag = $subscriberTag;
51 public function process(ContainerBuilder $container)
53 if (!$container->hasDefinition($this->dispatcherService) && !$container->hasAlias($this->dispatcherService)) {
57 $definition = $container->findDefinition($this->dispatcherService);
59 foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $events) {
60 $def = $container->getDefinition($id);
61 if (!$def->isPublic()) {
62 throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event listeners are lazy-loaded.', $id));
65 if ($def->isAbstract()) {
66 throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event listeners are lazy-loaded.', $id));
69 foreach ($events as $event) {
70 $priority = isset($event['priority']) ? $event['priority'] : 0;
72 if (!isset($event['event'])) {
73 throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
76 if (!isset($event['method'])) {
77 $event['method'] = 'on'.preg_replace_callback(array(
80 ), function ($matches) { return strtoupper($matches[0]); }, $event['event']);
81 $event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
84 $definition->addMethodCall('addListenerService', array($event['event'], array($id, $event['method']), $priority));
88 foreach ($container->findTaggedServiceIds($this->subscriberTag) as $id => $attributes) {
89 $def = $container->getDefinition($id);
90 if (!$def->isPublic()) {
91 throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
94 if ($def->isAbstract()) {
95 throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
98 // We must assume that the class value has been correctly filled, even if the service is created by a factory
99 $class = $container->getParameterBag()->resolveValue($def->getClass());
100 $interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
102 if (!is_subclass_of($class, $interface)) {
103 if (!class_exists($class, false)) {
104 throw new \InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
107 throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
110 $definition->addMethodCall('addSubscriberService', array($id, $class));