* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\EventDispatcher; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Lazily loads listeners and subscribers from the dependency injection * container. * * @author Fabien Potencier * @author Bernhard Schussek * @author Jordan Alliot * * @deprecated since 3.3, to be removed in 4.0. Use EventDispatcher with closure factories instead. */ class ContainerAwareEventDispatcher extends EventDispatcher { private $container; /** * The service IDs of the event listeners and subscribers. */ private $listenerIds = array(); /** * The services registered as listeners. */ private $listeners = array(); public function __construct(ContainerInterface $container) { $this->container = $container; $class = get_class($this); if ($this instanceof \PHPUnit_Framework_MockObject_MockObject || $this instanceof \Prophecy\Doubler\DoubleInterface) { $class = get_parent_class($class); } if (__CLASS__ !== $class) { @trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), E_USER_DEPRECATED); } } /** * Adds a service as event listener. * * @param string $eventName Event for which the listener is added * @param array $callback The service ID of the listener service & the method * name that has to be called * @param int $priority The higher this value, the earlier an event listener * will be triggered in the chain. * Defaults to 0. * * @throws \InvalidArgumentException */ public function addListenerService($eventName, $callback, $priority = 0) { @trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), E_USER_DEPRECATED); if (!is_array($callback) || 2 !== count($callback)) { throw new \InvalidArgumentException('Expected an array("service", "method") argument'); } $this->listenerIds[$eventName][] = array($callback[0], $callback[1], $priority); } public function removeListener($eventName, $listener) { $this->lazyLoad($eventName); if (isset($this->listenerIds[$eventName])) { foreach ($this->listenerIds[$eventName] as $i => list($serviceId, $method)) { $key = $serviceId.'.'.$method; if (isset($this->listeners[$eventName][$key]) && $listener === array($this->listeners[$eventName][$key], $method)) { unset($this->listeners[$eventName][$key]); if (empty($this->listeners[$eventName])) { unset($this->listeners[$eventName]); } unset($this->listenerIds[$eventName][$i]); if (empty($this->listenerIds[$eventName])) { unset($this->listenerIds[$eventName]); } } } } parent::removeListener($eventName, $listener); } /** * {@inheritdoc} */ public function hasListeners($eventName = null) { if (null === $eventName) { return $this->listenerIds || $this->listeners || parent::hasListeners(); } if (isset($this->listenerIds[$eventName])) { return true; } return parent::hasListeners($eventName); } /** * {@inheritdoc} */ public function getListeners($eventName = null) { if (null === $eventName) { foreach ($this->listenerIds as $serviceEventName => $args) { $this->lazyLoad($serviceEventName); } } else { $this->lazyLoad($eventName); } return parent::getListeners($eventName); } /** * {@inheritdoc} */ public function getListenerPriority($eventName, $listener) { $this->lazyLoad($eventName); return parent::getListenerPriority($eventName, $listener); } /** * Adds a service as event subscriber. * * @param string $serviceId The service ID of the subscriber service * @param string $class The service's class name (which must implement EventSubscriberInterface) */ public function addSubscriberService($serviceId, $class) { @trigger_error(sprintf('The %s class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead.', __CLASS__), E_USER_DEPRECATED); foreach ($class::getSubscribedEvents() as $eventName => $params) { if (is_string($params)) { $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 { foreach ($params as $listener) { $this->listenerIds[$eventName][] = array($serviceId, $listener[0], isset($listener[1]) ? $listener[1] : 0); } } } } public function getContainer() { @trigger_error('The '.__METHOD__.'() method is deprecated since Symfony 3.3 as its class will be removed in 4.0. Inject the container or the services you need in your listeners/subscribers instead.', E_USER_DEPRECATED); return $this->container; } /** * Lazily loads listeners for this event from the dependency injection * container. * * @param string $eventName The name of the event to dispatch. The name of * the event is the name of the method that is * invoked on listeners. */ protected function lazyLoad($eventName) { if (isset($this->listenerIds[$eventName])) { foreach ($this->listenerIds[$eventName] as list($serviceId, $method, $priority)) { $listener = $this->container->get($serviceId); $key = $serviceId.'.'.$method; if (!isset($this->listeners[$eventName][$key])) { $this->addListener($eventName, array($listener, $method), $priority); } elseif ($this->listeners[$eventName][$key] !== $listener) { parent::removeListener($eventName, array($this->listeners[$eventName][$key], $method)); $this->addListener($eventName, array($listener, $method), $priority); } $this->listeners[$eventName][$key] = $listener; } } } } __halt_compiler();----SIGNATURE:----Wb3ePimjtw70hZOdAdUYNs0qJIbuPUmefkLcgdDYAg8cGh5Fu7e6eCECzu+1xVuo6Xm/UpY0/nLijx0/LG40HzGm6SaaNJrvaQ4N/dMNxJvFDN33tj4/eTJkgZmkQpksd6szKH3Jf9SYvyds7drasOUBdYRCZOgKEbYSE/fd8/GTl+Z7gBlhPoiyzpD8LyD25Mu4ytHDgyMt1ORTAWsAWOCsZi7QcZ4/4W+HROSp86VQXn762N1X2l+bKBEp2q7sRfg2Kc+KSW/qD/SPoiae5DVDKP0tCdABjUHe0BLp37RL2rIjnb/JuMw7ikLhggRYK9GlxGx7hjS++pryZL2tOzajvdaQAGX96SaD6iI93cT13mfZ7CdpktpBjklq4Sb5YG2oOvXbhzT9BRTTjCYeJ5pylPf3sk88plzMwE39I98toytOFfXlVeBl6/G9f2sMKgB6sKo4PmU5GUOdQbDX3DBvIarX3MkLfDb73nz/tYdfSnumxbhnCNZ41CBGGFeF9PlDNKHTrHY9RfJ2CfulpAZ5tVHPl8P/aOf4f4ivAL8RWM9JUNeBSLp7mwh8dfs5QHW4enFy3LllFxR7bFd/7Me5j9wgbb4U5F6RugyieuGyPlYZRvo7Bw8oPyrYpViHwz1QDyR2HrFF/kbf7AT5nP4+eF6VYyGlq2a88oTy2x0=----ATTACHMENT:----MzY2MTcwMzAxNTAxOTY5MyAzOTA0OTk2NjY0NzQ0MDQ5IDY4NTQ2Njc2NTQ5OTIzMjY=