AnalyzeServiceReferencesPass.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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
  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. * Processes a ContainerBuilder object to populate the service reference graph.
  49. */
  50. public function process(ContainerBuilder $container)
  51. {
  52. $this->container = $container;
  53. $this->graph = $container->getCompiler()->getServiceReferenceGraph();
  54. $this->graph->clear();
  55. $this->lazy = false;
  56. $this->byConstructor = false;
  57. $this->byFactory = false;
  58. $this->definitions = $container->getDefinitions();
  59. $this->aliases = $container->getAliases();
  60. foreach ($this->aliases as $id => $alias) {
  61. $targetId = $this->getDefinitionId((string) $alias);
  62. $this->graph->connect($id, $alias, $targetId, null !== $targetId ? $this->container->getDefinition($targetId) : null, null);
  63. }
  64. try {
  65. parent::process($container);
  66. } finally {
  67. $this->aliases = $this->definitions = [];
  68. }
  69. }
  70. protected function processValue($value, bool $isRoot = false)
  71. {
  72. $lazy = $this->lazy;
  73. $inExpression = $this->inExpression();
  74. if ($value instanceof ArgumentInterface) {
  75. $this->lazy = !$this->byFactory || !$value instanceof IteratorArgument;
  76. parent::processValue($value->getValues());
  77. $this->lazy = $lazy;
  78. return $value;
  79. }
  80. if ($value instanceof Reference) {
  81. $targetId = $this->getDefinitionId((string) $value);
  82. $targetDefinition = null !== $targetId ? $this->container->getDefinition($targetId) : null;
  83. $this->graph->connect(
  84. $this->currentId,
  85. $this->currentDefinition,
  86. $targetId,
  87. $targetDefinition,
  88. $value,
  89. $this->lazy || ($this->hasProxyDumper && $targetDefinition && $targetDefinition->isLazy()),
  90. ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior(),
  91. $this->byConstructor
  92. );
  93. if ($inExpression) {
  94. $this->graph->connect(
  95. '.internal.reference_in_expression',
  96. null,
  97. $targetId,
  98. $targetDefinition,
  99. $value,
  100. $this->lazy || ($targetDefinition && $targetDefinition->isLazy()),
  101. true
  102. );
  103. }
  104. return $value;
  105. }
  106. if (!$value instanceof Definition) {
  107. return parent::processValue($value, $isRoot);
  108. }
  109. if ($isRoot) {
  110. if ($value->isSynthetic() || $value->isAbstract()) {
  111. return $value;
  112. }
  113. $this->currentDefinition = $value;
  114. } elseif ($this->currentDefinition === $value) {
  115. return $value;
  116. }
  117. $this->lazy = false;
  118. $byConstructor = $this->byConstructor;
  119. $this->byConstructor = $isRoot || $byConstructor;
  120. $byFactory = $this->byFactory;
  121. $this->byFactory = true;
  122. $this->processValue($value->getFactory());
  123. $this->byFactory = $byFactory;
  124. $this->processValue($value->getArguments());
  125. $properties = $value->getProperties();
  126. $setters = $value->getMethodCalls();
  127. // Any references before a "wither" are part of the constructor-instantiation graph
  128. $lastWitherIndex = null;
  129. foreach ($setters as $k => $call) {
  130. if ($call[2] ?? false) {
  131. $lastWitherIndex = $k;
  132. }
  133. }
  134. if (null !== $lastWitherIndex) {
  135. $this->processValue($properties);
  136. $setters = $properties = [];
  137. foreach ($value->getMethodCalls() as $k => $call) {
  138. if (null === $lastWitherIndex) {
  139. $setters[] = $call;
  140. continue;
  141. }
  142. if ($lastWitherIndex === $k) {
  143. $lastWitherIndex = null;
  144. }
  145. $this->processValue($call);
  146. }
  147. }
  148. $this->byConstructor = $byConstructor;
  149. if (!$this->onlyConstructorArguments) {
  150. $this->processValue($properties);
  151. $this->processValue($setters);
  152. $this->processValue($value->getConfigurator());
  153. }
  154. $this->lazy = $lazy;
  155. return $value;
  156. }
  157. private function getDefinitionId(string $id): ?string
  158. {
  159. while (isset($this->aliases[$id])) {
  160. $id = (string) $this->aliases[$id];
  161. }
  162. return isset($this->definitions[$id]) ? $id : null;
  163. }
  164. }