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 $onlyConstructorArguments;
36 * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
38 public function __construct($onlyConstructorArguments = false)
40 $this->onlyConstructorArguments
= (bool) $onlyConstructorArguments;
46 public function setRepeatedPass(RepeatedPass
$repeatedPass)
52 * Processes a ContainerBuilder object to populate the service reference graph.
54 public function process(ContainerBuilder
$container)
56 $this->container
= $container;
57 $this->graph
= $container->getCompiler()->getServiceReferenceGraph();
58 $this->graph
->clear();
60 foreach ($container->getDefinitions() as $id => $definition) {
61 if ($definition->isSynthetic() ||
$definition->isAbstract()) {
65 $this->currentId
= $id;
66 $this->currentDefinition
= $definition;
68 $this->processArguments($definition->getArguments());
69 if ($definition->getFactoryService(false)) {
70 $this->processArguments(array(new Reference($definition->getFactoryService(false))));
72 if (is_array($definition->getFactory())) {
73 $this->processArguments($definition->getFactory());
76 if (!$this->onlyConstructorArguments
) {
77 $this->processArguments($definition->getMethodCalls());
78 $this->processArguments($definition->getProperties());
79 if ($definition->getConfigurator()) {
80 $this->processArguments(array($definition->getConfigurator()));
85 foreach ($container->getAliases() as $id => $alias) {
86 $this->graph
->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
91 * Processes service definitions for arguments to find relationships for the service graph.
93 * @param array $arguments An array of Reference or Definition objects relating to service definitions
95 private function processArguments(array $arguments)
97 foreach ($arguments as $argument) {
98 if (is_array($argument)) {
99 $this->processArguments($argument);
100 } elseif ($argument instanceof Reference
) {
101 $this->graph
->connect(
103 $this->currentDefinition
,
104 $this->getDefinitionId((string) $argument),
105 $this->getDefinition((string) $argument),
108 } elseif ($argument instanceof Definition
) {
109 $this->processArguments($argument->getArguments());
110 $this->processArguments($argument->getMethodCalls());
111 $this->processArguments($argument->getProperties());
113 if (is_array($argument->getFactory())) {
114 $this->processArguments($argument->getFactory());
116 if ($argument->getFactoryService(false)) {
117 $this->processArguments(array(new Reference($argument->getFactoryService(false))));
124 * Returns a service definition given the full name or an alias.
126 * @param string $id A full id or alias for a service definition
128 * @return Definition|null The definition related to the supplied id
130 private function getDefinition($id)
132 $id = $this->getDefinitionId($id);
134 return null === $id ?
null : $this->container
->getDefinition($id);
137 private function getDefinitionId($id)
139 while ($this->container
->hasAlias($id)) {
140 $id = (string) $this->container
->getAlias($id);
143 if (!$this->container
->hasDefinition($id)) {