From f13cbb982873be896b34d89aee26d65f9335eea7 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Sat, 27 Sep 2014 23:47:34 +0200 Subject: [PATCH] [EventDispatcher] Moves the logic of addSubscriberService to the compiler pass --- .../ContainerAwareEventDispatcher.php | 2 +- .../DependencyInjection/RegisterListenersPass.php | 12 +++++++++++- .../RegisterListenersPassTest.php | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php b/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php index e97d427ea15eb..8f005686987e3 100644 --- a/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php @@ -144,7 +144,7 @@ public function addSubscriberService($serviceId, $class) $this->listenerIds[$eventName][] = array($serviceId, $params, 0); } elseif (is_string($params[0])) { $this->listenerIds[$eventName][] = array($serviceId, $params[0], isset($params[1]) ? $params[1] : 0); - } else { + } elseif (is_array($params[0])) { foreach ($params as $listener) { $this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0); } diff --git a/src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php b/src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php index afe3ecd182212..287fb69edb166 100644 --- a/src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php +++ b/src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php @@ -100,7 +100,17 @@ public function process(ContainerBuilder $container) throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface)); } - $definition->addMethodCall('addSubscriberService', array($id, $class)); + foreach ($class::getSubscribedEvents() as $eventName => $params) { + if (is_string($params)) { + $definition->addMethodCall('addListenerService', array($eventName, array($id, $params), 0)); + } elseif (is_string($params[0])) { + $definition->addMethodCall('addListenerService', array($eventName, array($id, $params[0]), isset($params[1]) ? $params[1] : 0)); + } elseif (is_array($params[0])) { + foreach ($params as $listener) { + $definition->addMethodCall('addListenerService', array($eventName, array($id, $listener[0]), isset($listener[1]) ? $listener[1] : 0)); + } + } + } } } } diff --git a/src/Symfony/Component/EventDispatcher/Tests/DependencyInjection/RegisterListenersPassTest.php b/src/Symfony/Component/EventDispatcher/Tests/DependencyInjection/RegisterListenersPassTest.php index 9c5ee5f7e9d8d..10510832091dc 100644 --- a/src/Symfony/Component/EventDispatcher/Tests/DependencyInjection/RegisterListenersPassTest.php +++ b/src/Symfony/Component/EventDispatcher/Tests/DependencyInjection/RegisterListenersPassTest.php @@ -138,5 +138,6 @@ class SubscriberService implements \Symfony\Component\EventDispatcher\EventSubsc { public static function getSubscribedEvents() { + return array(); } }