Source of file RegisterEventListenersAndSubscribersPass.php
Size: 5,067 Bytes - Last Modified: 2013-07-17T08:22:21+02:00
/home/theseer/Downloads/Symfony/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/DependencyInjection/CompilerPass/RegisterEventListenersAndSubscribersPass.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 | <?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; /** * Registers event listeners and subscribers to the available doctrine connections. * * @author Jeremy Mikola <jmikola@gmail.com> * @author Alexander <iam.asm89@gmail.com> */ class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface { private $connections; private $container; private $eventManagers; private $managerTemplate; private $tagPrefix; /** * Constructor. * * @param string $connections Parameter ID for connections * @param string $managerTemplate sprintf() template for generating the event * manager's service ID for a connection name * @param string $tagPrefix Tag prefix for listeners and subscribers */ public function __construct($connections, $managerTemplate, $tagPrefix) { $this->connections = $connections; $this->managerTemplate = $managerTemplate; $this->tagPrefix = $tagPrefix; } /** * {@inheritDoc} */ public function process(ContainerBuilder $container) { if (!$container->hasParameter($this->connections)) { return; } $this->container = $container; $this->connections = $container->getParameter($this->connections); $sortFunc = function($a, $b) { $a = isset($a['priority']) ? $a['priority'] : 0; $b = isset($b['priority']) ? $b['priority'] : 0; return $a > $b ? -1 : 1; }; $subscribersPerCon = $this->groupByConnection($container->findTaggedServiceIds($this->tagPrefix.'.event_subscriber')); foreach ($subscribersPerCon as $con => $subscribers) { $em = $this->getEventManager($con); uasort($subscribers, $sortFunc); foreach ($subscribers as $id => $instance) { $em->addMethodCall('addEventSubscriber', array(new Reference($id))); } } $listenersPerCon = $this->groupByConnection($container->findTaggedServiceIds($this->tagPrefix.'.event_listener'), true); foreach ($listenersPerCon as $con => $listeners) { $em = $this->getEventManager($con); uasort($listeners, $sortFunc); foreach ($listeners as $id => $instance) { $em->addMethodCall('addEventListener', array( array_unique($instance['event']), isset($instance['lazy']) && $instance['lazy'] ? $id : new Reference($id), )); } } } private function groupByConnection(array $services, $isListener = false) { $grouped = array(); foreach ($allCons = array_keys($this->connections) as $con) { $grouped[$con] = array(); } foreach ($services as $id => $instances) { foreach ($instances as $instance) { if ($isListener) { if (!isset($instance['event'])) { throw new \InvalidArgumentException(sprintf('Doctrine event listener "%s" must specify the "event" attribute.', $id)); } $instance['event'] = array($instance['event']); if (isset($instance['lazy']) && $instance['lazy']) { $this->container->getDefinition($id)->setPublic(true); } } $cons = isset($instance['connection']) ? array($instance['connection']) : $allCons; foreach ($cons as $con) { if (!isset($grouped[$con])) { throw new \RuntimeException(sprintf('The Doctrine connection "%s" referenced in service "%s" does not exist. Available connections names: %s', $con, $id, implode(', ', array_keys($this->connections)))); } if ($isListener && isset($grouped[$con][$id])) { $grouped[$con][$id]['event'] = array_merge($grouped[$con][$id]['event'], $instance['event']); } else { $grouped[$con][$id] = $instance; } } } } return $grouped; } private function getEventManager($name) { if (null === $this->eventManagers) { $this->eventManagers = array(); foreach ($this->connections as $n => $id) { $this->eventManagers[$n] = $this->container->getDefinition(sprintf($this->managerTemplate, $n)); } } return $this->eventManagers[$name]; } } |