AnalyzeServiceReferencesPass.php 6.4 KB

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