Source of file AnalyzeServiceReferencesPass.php
Size: 4,512 Bytes - Last Modified: 2013-07-17T08:22:21+02:00
/home/theseer/Downloads/Symfony/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/AnalyzeServiceReferencesPass.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 | <?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\Component\DependencyInjection\Compiler; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ContainerBuilder; /** * Run this pass before passes that need to know more about the relation of * your services. * * This class will populate the ServiceReferenceGraph with information. You can * retrieve the graph in other passes from the compiler. * * @author Johannes M. Schmitt <schmittjoh@gmail.com> */ class AnalyzeServiceReferencesPass implements RepeatablePassInterface { private $graph; private $container; private $currentId; private $currentDefinition; private $repeatedPass; private $onlyConstructorArguments; /** * Constructor. * * @param Boolean $onlyConstructorArguments Sets this Service Reference pass to ignore method calls */ public function __construct($onlyConstructorArguments = false) { $this->onlyConstructorArguments = (Boolean) $onlyConstructorArguments; } /** * {@inheritDoc} */ public function setRepeatedPass(RepeatedPass $repeatedPass) { $this->repeatedPass = $repeatedPass; } /** * Processes a ContainerBuilder object to populate the service reference graph. * * @param ContainerBuilder $container */ public function process(ContainerBuilder $container) { $this->container = $container; $this->graph = $container->getCompiler()->getServiceReferenceGraph(); $this->graph->clear(); foreach ($container->getDefinitions() as $id => $definition) { if ($definition->isSynthetic() || $definition->isAbstract()) { continue; } $this->currentId = $id; $this->currentDefinition = $definition; $this->processArguments($definition->getArguments()); if (!$this->onlyConstructorArguments) { $this->processArguments($definition->getMethodCalls()); $this->processArguments($definition->getProperties()); if ($definition->getConfigurator()) { $this->processArguments(array($definition->getConfigurator())); } } } foreach ($container->getAliases() as $id => $alias) { $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null); } } /** * Processes service definitions for arguments to find relationships for the service graph. * * @param array $arguments An array of Reference or Definition objects relating to service definitions */ private function processArguments(array $arguments) { foreach ($arguments as $argument) { if (is_array($argument)) { $this->processArguments($argument); } elseif ($argument instanceof Reference) { $this->graph->connect( $this->currentId, $this->currentDefinition, $this->getDefinitionId((string) $argument), $this->getDefinition((string) $argument), $argument ); } elseif ($argument instanceof Definition) { $this->processArguments($argument->getArguments()); $this->processArguments($argument->getMethodCalls()); $this->processArguments($argument->getProperties()); } } } /** * Returns a service definition given the full name or an alias. * * @param string $id A full id or alias for a service definition. * * @return Definition|null The definition related to the supplied id */ private function getDefinition($id) { $id = $this->getDefinitionId($id); return null === $id ? null : $this->container->getDefinition($id); } private function getDefinitionId($id) { while ($this->container->hasAlias($id)) { $id = (string) $this->container->getAlias($id); } if (!$this->container->hasDefinition($id)) { return null; } return $id; } } |