4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\Definition;
15 use Symfony\Component\DependencyInjection\Reference;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
19 * Run this pass before passes that need to know more about the relation of
22 * This class will populate the ServiceReferenceGraph with information. You can
23 * retrieve the graph in other passes from the compiler.
25 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
27 class AnalyzeServiceReferencesPass implements RepeatablePassInterface
32 private $currentDefinition;
33 private $repeatedPass;
34 private $onlyConstructorArguments;
37 * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
39 public function __construct($onlyConstructorArguments = false)
41 $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
47 public function setRepeatedPass(RepeatedPass $repeatedPass)
49 $this->repeatedPass = $repeatedPass;
53 * Processes a ContainerBuilder object to populate the service reference graph.
55 * @param ContainerBuilder $container
57 public function process(ContainerBuilder $container)
59 $this->container = $container;
60 $this->graph = $container->getCompiler()->getServiceReferenceGraph();
61 $this->graph->clear();
63 foreach ($container->getDefinitions() as $id => $definition) {
64 if ($definition->isSynthetic() || $definition->isAbstract()) {
68 $this->currentId = $id;
69 $this->currentDefinition = $definition;
71 $this->processArguments($definition->getArguments());
72 if ($definition->getFactoryService(false)) {
73 $this->processArguments(array(new Reference($definition->getFactoryService(false))));
75 if (is_array($definition->getFactory())) {
76 $this->processArguments($definition->getFactory());
79 if (!$this->onlyConstructorArguments) {
80 $this->processArguments($definition->getMethodCalls());
81 $this->processArguments($definition->getProperties());
82 if ($definition->getConfigurator()) {
83 $this->processArguments(array($definition->getConfigurator()));
88 foreach ($container->getAliases() as $id => $alias) {
89 $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
94 * Processes service definitions for arguments to find relationships for the service graph.
96 * @param array $arguments An array of Reference or Definition objects relating to service definitions
98 private function processArguments(array $arguments)
100 foreach ($arguments as $argument) {
101 if (is_array($argument)) {
102 $this->processArguments($argument);
103 } elseif ($argument instanceof Reference) {
104 $this->graph->connect(
106 $this->currentDefinition,
107 $this->getDefinitionId((string) $argument),
108 $this->getDefinition((string) $argument),
111 } elseif ($argument instanceof Definition) {
112 $this->processArguments($argument->getArguments());
113 $this->processArguments($argument->getMethodCalls());
114 $this->processArguments($argument->getProperties());
116 if (is_array($argument->getFactory())) {
117 $this->processArguments($argument->getFactory());
119 if ($argument->getFactoryService(false)) {
120 $this->processArguments(array(new Reference($argument->getFactoryService(false))));
127 * Returns a service definition given the full name or an alias.
129 * @param string $id A full id or alias for a service definition
131 * @return Definition|null The definition related to the supplied id
133 private function getDefinition($id)
135 $id = $this->getDefinitionId($id);
137 return null === $id ? null : $this->container->getDefinition($id);
140 private function getDefinitionId($id)
142 while ($this->container->hasAlias($id)) {
143 $id = (string) $this->container->getAlias($id);
146 if (!$this->container->hasDefinition($id)) {