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\Argument\ArgumentInterface;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\DependencyInjection\Definition;
18 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
19 use Symfony\Component\DependencyInjection\ExpressionLanguage;
20 use Symfony\Component\DependencyInjection\Reference;
21 use Symfony\Component\ExpressionLanguage\Expression;
24 * Run this pass before passes that need to know more about the relation of
27 * This class will populate the ServiceReferenceGraph with information. You can
28 * retrieve the graph in other passes from the compiler.
30 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
32 class AnalyzeServiceReferencesPass extends AbstractRecursivePass implements RepeatablePassInterface
35 private $currentDefinition;
36 private $onlyConstructorArguments;
37 private $hasProxyDumper;
39 private $expressionLanguage;
42 * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
44 public function __construct($onlyConstructorArguments = false, $hasProxyDumper = true)
46 $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
47 $this->hasProxyDumper = (bool) $hasProxyDumper;
53 public function setRepeatedPass(RepeatedPass $repeatedPass)
59 * Processes a ContainerBuilder object to populate the service reference graph.
61 public function process(ContainerBuilder $container)
63 $this->container = $container;
64 $this->graph = $container->getCompiler()->getServiceReferenceGraph();
65 $this->graph->clear();
68 foreach ($container->getAliases() as $id => $alias) {
69 $targetId = $this->getDefinitionId((string) $alias);
70 $this->graph->connect($id, $alias, $targetId, $this->getDefinition($targetId), null);
73 parent::process($container);
76 protected function processValue($value, $isRoot = false)
80 if ($value instanceof ArgumentInterface) {
82 parent::processValue($value->getValues());
87 if ($value instanceof Expression) {
88 $this->getExpressionLanguage()->compile((string) $value, array('this' => 'container'));
92 if ($value instanceof Reference) {
93 $targetId = $this->getDefinitionId((string) $value);
94 $targetDefinition = $this->getDefinition($targetId);
96 $this->graph->connect(
98 $this->currentDefinition,
102 $this->lazy || ($this->hasProxyDumper && $targetDefinition && $targetDefinition->isLazy()),
103 ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior()
108 if (!$value instanceof Definition) {
109 return parent::processValue($value, $isRoot);
112 if ($value->isSynthetic() || $value->isAbstract()) {
115 $this->currentDefinition = $value;
116 } elseif ($this->currentDefinition === $value) {
121 $this->processValue($value->getFactory());
122 $this->processValue($value->getArguments());
124 if (!$this->onlyConstructorArguments) {
125 $this->processValue($value->getProperties());
126 $this->processValue($value->getMethodCalls());
127 $this->processValue($value->getConfigurator());
135 * Returns a service definition given the full name or an alias.
137 * @param string $id A full id or alias for a service definition
139 * @return Definition|null The definition related to the supplied id
141 private function getDefinition($id)
143 return null === $id ? null : $this->container->getDefinition($id);
146 private function getDefinitionId($id)
148 while ($this->container->hasAlias($id)) {
149 $id = (string) $this->container->getAlias($id);
152 if (!$this->container->hasDefinition($id)) {
156 return $this->container->normalizeId($id);
159 private function getExpressionLanguage()
161 if (null === $this->expressionLanguage) {
162 if (!class_exists(ExpressionLanguage::class)) {
163 throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
166 $providers = $this->container->getExpressionLanguageProviders();
167 $this->expressionLanguage = new ExpressionLanguage(null, $providers, function ($arg) {
168 if ('""' === substr_replace($arg, '', 1, -1)) {
169 $id = stripcslashes(substr($arg, 1, -1));
170 $id = $this->getDefinitionId($id);
172 $this->graph->connect(
174 $this->currentDefinition,
176 $this->getDefinition($id)
180 return sprintf('$this->get(%s)', $arg);
184 return $this->expressionLanguage;