Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DependencyInjection\Compiler; Chris@0: Chris@0: use Symfony\Component\DependencyInjection\Definition; Chris@0: use Symfony\Component\DependencyInjection\Reference; Chris@0: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@0: Chris@0: /** Chris@0: * Run this pass before passes that need to know more about the relation of Chris@0: * your services. Chris@0: * Chris@0: * This class will populate the ServiceReferenceGraph with information. You can Chris@0: * retrieve the graph in other passes from the compiler. Chris@0: * Chris@0: * @author Johannes M. Schmitt Chris@0: */ Chris@0: class AnalyzeServiceReferencesPass implements RepeatablePassInterface Chris@0: { Chris@0: private $graph; Chris@0: private $container; Chris@0: private $currentId; Chris@0: private $currentDefinition; Chris@0: private $repeatedPass; Chris@0: private $onlyConstructorArguments; Chris@0: Chris@0: /** Chris@0: * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls Chris@0: */ Chris@0: public function __construct($onlyConstructorArguments = false) Chris@0: { Chris@0: $this->onlyConstructorArguments = (bool) $onlyConstructorArguments; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setRepeatedPass(RepeatedPass $repeatedPass) Chris@0: { Chris@0: $this->repeatedPass = $repeatedPass; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Processes a ContainerBuilder object to populate the service reference graph. Chris@0: * Chris@0: * @param ContainerBuilder $container Chris@0: */ Chris@0: public function process(ContainerBuilder $container) Chris@0: { Chris@0: $this->container = $container; Chris@0: $this->graph = $container->getCompiler()->getServiceReferenceGraph(); Chris@0: $this->graph->clear(); Chris@0: Chris@0: foreach ($container->getDefinitions() as $id => $definition) { Chris@0: if ($definition->isSynthetic() || $definition->isAbstract()) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $this->currentId = $id; Chris@0: $this->currentDefinition = $definition; Chris@0: Chris@0: $this->processArguments($definition->getArguments()); Chris@0: if (is_array($definition->getFactory())) { Chris@0: $this->processArguments($definition->getFactory()); Chris@0: } Chris@0: Chris@0: if (!$this->onlyConstructorArguments) { Chris@0: $this->processArguments($definition->getMethodCalls()); Chris@0: $this->processArguments($definition->getProperties()); Chris@0: if ($definition->getConfigurator()) { Chris@0: $this->processArguments(array($definition->getConfigurator())); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: foreach ($container->getAliases() as $id => $alias) { Chris@0: $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Processes service definitions for arguments to find relationships for the service graph. Chris@0: * Chris@0: * @param array $arguments An array of Reference or Definition objects relating to service definitions Chris@0: */ Chris@0: private function processArguments(array $arguments) Chris@0: { Chris@0: foreach ($arguments as $argument) { Chris@0: if (is_array($argument)) { Chris@0: $this->processArguments($argument); Chris@0: } elseif ($argument instanceof Reference) { Chris@0: $this->graph->connect( Chris@0: $this->currentId, Chris@0: $this->currentDefinition, Chris@0: $this->getDefinitionId((string) $argument), Chris@0: $this->getDefinition((string) $argument), Chris@0: $argument Chris@0: ); Chris@0: } elseif ($argument instanceof Definition) { Chris@0: $this->processArguments($argument->getArguments()); Chris@0: $this->processArguments($argument->getMethodCalls()); Chris@0: $this->processArguments($argument->getProperties()); Chris@0: Chris@0: if (is_array($argument->getFactory())) { Chris@0: $this->processArguments($argument->getFactory()); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a service definition given the full name or an alias. Chris@0: * Chris@0: * @param string $id A full id or alias for a service definition Chris@0: * Chris@0: * @return Definition|null The definition related to the supplied id Chris@0: */ Chris@0: private function getDefinition($id) Chris@0: { Chris@0: $id = $this->getDefinitionId($id); Chris@0: Chris@0: return null === $id ? null : $this->container->getDefinition($id); Chris@0: } Chris@0: Chris@0: private function getDefinitionId($id) Chris@0: { Chris@0: while ($this->container->hasAlias($id)) { Chris@0: $id = (string) $this->container->getAlias($id); Chris@0: } Chris@0: Chris@0: if (!$this->container->hasDefinition($id)) { Chris@0: return; Chris@0: } Chris@0: Chris@0: return $id; Chris@0: } Chris@0: }