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;
16 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
19 * Compiler pass to register tagged services for an event dispatcher.
21 class RegisterListenersPass implements CompilerPassInterface
26 protected $dispatcherService;
31 protected $listenerTag;
36 protected $subscriberTag;
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
45 public function __construct($dispatcherService = 'event_dispatcher', $listenerTag = 'kernel.event_listener', $subscriberTag = 'kernel.event_subscriber')
47 $this->dispatcherService = $dispatcherService;
48 $this->listenerTag = $listenerTag;
49 $this->subscriberTag = $subscriberTag;
52 public function process(ContainerBuilder $container)
54 if (!$container->hasDefinition($this->dispatcherService) && !$container->hasAlias($this->dispatcherService)) {
58 $definition = $container->findDefinition($this->dispatcherService);
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));
66 if ($def->isAbstract()) {
67 throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as event listeners are lazy-loaded.', $id));
70 foreach ($events as $event) {
71 $priority = isset($event['priority']) ? $event['priority'] : 0;
73 if (!isset($event['event'])) {
74 throw new InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
77 if (!isset($event['method'])) {
78 $event['method'] = 'on'.preg_replace_callback(array(
81 ), function ($matches) { return strtoupper($matches[0]); }, $event['event']);
82 $event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
85 $definition->addMethodCall('addListenerService', array($event['event'], array($id, $event['method']), $priority));
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));
95 if ($def->isAbstract()) {
96 throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
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';
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));
108 throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
111 $definition->addMethodCall('addSubscriberService', array($id, $class));