AnalyzeServiceReferencesPass.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. /**
  17. * Run this pass before passes that need to know more about the relation of
  18. * your services.
  19. *
  20. * This class will populate the ServiceReferenceGraph with information. You can
  21. * retrieve the graph in other passes from the compiler.
  22. *
  23. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  24. * @author Nicolas Grekas <p@tchwork.com>
  25. */
  26. class AnalyzeServiceReferencesPass extends AbstractRecursivePass implements RepeatablePassInterface
  27. {
  28. private $graph;
  29. private $currentDefinition;
  30. private $onlyConstructorArguments;
  31. private $hasProxyDumper;
  32. private $lazy;
  33. private $byConstructor;
  34. private $definitions;
  35. private $aliases;
  36. /**
  37. * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
  38. */
  39. public function __construct(bool $onlyConstructorArguments = false, bool $hasProxyDumper = true)
  40. {
  41. $this->onlyConstructorArguments = $onlyConstructorArguments;
  42. $this->hasProxyDumper = $hasProxyDumper;
  43. $this->enableExpressionProcessing();
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function setRepeatedPass(RepeatedPass $repeatedPass)
  49. {
  50. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2.', __METHOD__), E_USER_DEPRECATED);
  51. }
  52. /**
  53. * Processes a ContainerBuilder object to populate the service reference graph.
  54. */
  55. public function process(ContainerBuilder $container)
  56. {
  57. $this->container = $container;
  58. $this->graph = $container->getCompiler()->getServiceReferenceGraph();
  59. $this->graph->clear();
  60. $this->lazy = false;
  61. $this->byConstructor = false;
  62. $this->definitions = $container->getDefinitions();
  63. $this->aliases = $container->getAliases();
  64. foreach ($this->aliases as $id => $alias) {
  65. $targetId = $this->getDefinitionId((string) $alias);
  66. $this->graph->connect($id, $alias, $targetId, null !== $targetId ? $this->container->getDefinition($targetId) : null, null);
  67. }
  68. try {
  69. parent::process($container);
  70. } finally {
  71. $this->aliases = $this->definitions = [];
  72. }
  73. }
  74. protected function processValue($value, $isRoot = false)
  75. {
  76. $lazy = $this->lazy;
  77. $inExpression = $this->inExpression();
  78. if ($value instanceof ArgumentInterface) {
  79. $this->lazy = true;
  80. parent::processValue($value->getValues());
  81. $this->lazy = $lazy;
  82. return $value;
  83. }
  84. if ($value instanceof Reference) {
  85. $targetId = $this->getDefinitionId((string) $value);
  86. $targetDefinition = null !== $targetId ? $this->container->getDefinition($targetId) : null;
  87. $this->graph->connect(
  88. $this->currentId,
  89. $this->currentDefinition,
  90. $targetId,
  91. $targetDefinition,
  92. $value,
  93. $this->lazy || ($this->hasProxyDumper && $targetDefinition && $targetDefinition->isLazy()),
  94. ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior(),
  95. $this->byConstructor
  96. );
  97. if ($inExpression) {
  98. $this->graph->connect(
  99. '.internal.reference_in_expression',
  100. null,
  101. $targetId,
  102. $targetDefinition,
  103. $value,
  104. $this->lazy || ($targetDefinition && $targetDefinition->isLazy()),
  105. true
  106. );
  107. }
  108. return $value;
  109. }
  110. if (!$value instanceof Definition) {
  111. return parent::processValue($value, $isRoot);
  112. }
  113. if ($isRoot) {
  114. if ($value->isSynthetic() || $value->isAbstract()) {
  115. return $value;
  116. }
  117. $this->currentDefinition = $value;
  118. } elseif ($this->currentDefinition === $value) {
  119. return $value;
  120. }
  121. $this->lazy = false;
  122. $byConstructor = $this->byConstructor;
  123. $this->byConstructor = $isRoot || $byConstructor;
  124. $this->processValue($value->getFactory());
  125. $this->processValue($value->getArguments());
  126. $properties = $value->getProperties();
  127. $setters = $value->getMethodCalls();
  128. // Any references before a "wither" are part of the constructor-instantiation graph
  129. $lastWitherIndex = null;
  130. foreach ($setters as $k => $call) {
  131. if ($call[2] ?? false) {
  132. $lastWitherIndex = $k;
  133. }
  134. }
  135. if (null !== $lastWitherIndex) {
  136. $this->processValue($properties);
  137. $setters = $properties = [];
  138. foreach ($value->getMethodCalls() as $k => $call) {
  139. if (null === $lastWitherIndex) {
  140. $setters[] = $call;
  141. continue;
  142. }
  143. if ($lastWitherIndex === $k) {
  144. $lastWitherIndex = null;
  145. }
  146. $this->processValue($call);
  147. }
  148. }
  149. $this->byConstructor = $byConstructor;
  150. if (!$this->onlyConstructorArguments) {
  151. $this->processValue($properties);
  152. $this->processValue($setters);
  153. $this->processValue($value->getConfigurator());
  154. }
  155. $this->lazy = $lazy;
  156. return $value;
  157. }
  158. private function getDefinitionId(string $id): ?string
  159. {
  160. while (isset($this->aliases[$id])) {
  161. $id = (string) $this->aliases[$id];
  162. }
  163. return isset($this->definitions[$id]) ? $id : null;
  164. }
  165. }