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\ExpressionLanguage;
16 use Symfony\Component\DependencyInjection\Reference;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 use Symfony\Component\ExpressionLanguage\Expression;
21 * Run this pass before passes that need to know more about the relation of
24 * This class will populate the ServiceReferenceGraph with information. You can
25 * retrieve the graph in other passes from the compiler.
27 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
29 class AnalyzeServiceReferencesPass implements RepeatablePassInterface
34 private $currentDefinition;
35 private $repeatedPass;
36 private $onlyConstructorArguments;
37 private $expressionLanguage;
40 * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
42 public function __construct($onlyConstructorArguments = false)
44 $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
50 public function setRepeatedPass(RepeatedPass $repeatedPass)
52 $this->repeatedPass = $repeatedPass;
56 * Processes a ContainerBuilder object to populate the service reference graph.
58 * @param ContainerBuilder $container
60 public function process(ContainerBuilder $container)
62 $this->container = $container;
63 $this->graph = $container->getCompiler()->getServiceReferenceGraph();
64 $this->graph->clear();
66 foreach ($container->getDefinitions() as $id => $definition) {
67 if ($definition->isSynthetic() || $definition->isAbstract()) {
71 $this->currentId = $id;
72 $this->currentDefinition = $definition;
74 $this->processArguments($definition->getArguments());
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 Expression) {
104 $this->getExpressionLanguage()->compile((string) $argument, array('this' => 'container'));
105 } elseif ($argument instanceof Reference) {
106 $this->graph->connect(
108 $this->currentDefinition,
109 $this->getDefinitionId((string) $argument),
110 $this->getDefinition((string) $argument),
113 } elseif ($argument instanceof Definition) {
114 $this->processArguments($argument->getArguments());
115 $this->processArguments($argument->getMethodCalls());
116 $this->processArguments($argument->getProperties());
118 if (is_array($argument->getFactory())) {
119 $this->processArguments($argument->getFactory());
126 * Returns a service definition given the full name or an alias.
128 * @param string $id A full id or alias for a service definition
130 * @return Definition|null The definition related to the supplied id
132 private function getDefinition($id)
134 $id = $this->getDefinitionId($id);
136 return null === $id ? null : $this->container->getDefinition($id);
139 private function getDefinitionId($id)
141 while ($this->container->hasAlias($id)) {
142 $id = (string) $this->container->getAlias($id);
145 if (!$this->container->hasDefinition($id)) {
152 private function getExpressionLanguage()
154 if (null === $this->expressionLanguage) {
155 $providers = $this->container->getExpressionLanguageProviders();
156 $this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) {
157 if ('""' === substr_replace($arg, '', 1, -1)) {
158 $id = stripcslashes(substr($arg, 1, -1));
160 $this->graph->connect(
162 $this->currentDefinition,
163 $this->getDefinitionId($id),
164 $this->getDefinition($id)
168 return sprintf('$this->get(%s)', $arg);
172 return $this->expressionLanguage;