InlineServiceDefinitionsPass.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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\Definition;
  14. use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. /**
  17. * Inline service definitions where this is possible.
  18. *
  19. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  20. */
  21. class InlineServiceDefinitionsPass extends AbstractRecursivePass implements RepeatablePassInterface
  22. {
  23. private $analyzingPass;
  24. private $repeatedPass;
  25. private $cloningIds = [];
  26. private $connectedIds = [];
  27. private $notInlinedIds = [];
  28. private $inlinedIds = [];
  29. private $notInlinableIds = [];
  30. private $graph;
  31. public function __construct(AnalyzeServiceReferencesPass $analyzingPass = null)
  32. {
  33. $this->analyzingPass = $analyzingPass;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function setRepeatedPass(RepeatedPass $repeatedPass)
  39. {
  40. @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2.', __METHOD__), \E_USER_DEPRECATED);
  41. $this->repeatedPass = $repeatedPass;
  42. }
  43. public function process(ContainerBuilder $container)
  44. {
  45. $this->container = $container;
  46. if ($this->analyzingPass) {
  47. $analyzedContainer = new ContainerBuilder();
  48. $analyzedContainer->setAliases($container->getAliases());
  49. $analyzedContainer->setDefinitions($container->getDefinitions());
  50. foreach ($container->getExpressionLanguageProviders() as $provider) {
  51. $analyzedContainer->addExpressionLanguageProvider($provider);
  52. }
  53. } else {
  54. $analyzedContainer = $container;
  55. }
  56. try {
  57. $remainingInlinedIds = [];
  58. $this->connectedIds = $this->notInlinedIds = $container->getDefinitions();
  59. do {
  60. if ($this->analyzingPass) {
  61. $analyzedContainer->setDefinitions(array_intersect_key($analyzedContainer->getDefinitions(), $this->connectedIds));
  62. $this->analyzingPass->process($analyzedContainer);
  63. }
  64. $this->graph = $analyzedContainer->getCompiler()->getServiceReferenceGraph();
  65. $notInlinedIds = $this->notInlinedIds;
  66. $this->connectedIds = $this->notInlinedIds = $this->inlinedIds = [];
  67. foreach ($analyzedContainer->getDefinitions() as $id => $definition) {
  68. if (!$this->graph->hasNode($id)) {
  69. continue;
  70. }
  71. foreach ($this->graph->getNode($id)->getOutEdges() as $edge) {
  72. if (isset($notInlinedIds[$edge->getSourceNode()->getId()])) {
  73. $this->currentId = $id;
  74. $this->processValue($definition, true);
  75. break;
  76. }
  77. }
  78. }
  79. foreach ($this->inlinedIds as $id => $isPublicOrNotShared) {
  80. if ($isPublicOrNotShared) {
  81. $remainingInlinedIds[$id] = $id;
  82. } else {
  83. $container->removeDefinition($id);
  84. $analyzedContainer->removeDefinition($id);
  85. }
  86. }
  87. } while ($this->inlinedIds && $this->analyzingPass);
  88. if ($this->inlinedIds && $this->repeatedPass) {
  89. $this->repeatedPass->setRepeat();
  90. }
  91. foreach ($remainingInlinedIds as $id) {
  92. if (isset($this->notInlinableIds[$id])) {
  93. continue;
  94. }
  95. $definition = $container->getDefinition($id);
  96. if (!$definition->isShared() && !$definition->isPublic()) {
  97. $container->removeDefinition($id);
  98. }
  99. }
  100. } finally {
  101. $this->container = null;
  102. $this->connectedIds = $this->notInlinedIds = $this->inlinedIds = [];
  103. $this->notInlinableIds = [];
  104. $this->graph = null;
  105. }
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. protected function processValue($value, $isRoot = false)
  111. {
  112. if ($value instanceof ArgumentInterface) {
  113. // Reference found in ArgumentInterface::getValues() are not inlineable
  114. return $value;
  115. }
  116. if ($value instanceof Definition && $this->cloningIds) {
  117. if ($value->isShared()) {
  118. return $value;
  119. }
  120. $value = clone $value;
  121. }
  122. if (!$value instanceof Reference) {
  123. return parent::processValue($value, $isRoot);
  124. } elseif (!$this->container->hasDefinition($id = (string) $value)) {
  125. return $value;
  126. }
  127. $definition = $this->container->getDefinition($id);
  128. if (!$this->isInlineableDefinition($id, $definition)) {
  129. $this->notInlinableIds[$id] = true;
  130. return $value;
  131. }
  132. $this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId));
  133. $this->inlinedIds[$id] = $definition->isPublic() || !$definition->isShared();
  134. $this->notInlinedIds[$this->currentId] = true;
  135. if ($definition->isShared()) {
  136. return $definition;
  137. }
  138. if (isset($this->cloningIds[$id])) {
  139. $ids = array_keys($this->cloningIds);
  140. $ids[] = $id;
  141. throw new ServiceCircularReferenceException($id, \array_slice($ids, array_search($id, $ids)));
  142. }
  143. $this->cloningIds[$id] = true;
  144. try {
  145. return $this->processValue($definition);
  146. } finally {
  147. unset($this->cloningIds[$id]);
  148. }
  149. }
  150. /**
  151. * Checks if the definition is inlineable.
  152. */
  153. private function isInlineableDefinition(string $id, Definition $definition): bool
  154. {
  155. if ($definition->hasErrors() || $definition->isDeprecated() || $definition->isLazy() || $definition->isSynthetic()) {
  156. return false;
  157. }
  158. if (!$definition->isShared()) {
  159. if (!$this->graph->hasNode($id)) {
  160. return true;
  161. }
  162. foreach ($this->graph->getNode($id)->getInEdges() as $edge) {
  163. $srcId = $edge->getSourceNode()->getId();
  164. $this->connectedIds[$srcId] = true;
  165. if ($edge->isWeak() || $edge->isLazy()) {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. if ($definition->isPublic()) {
  172. return false;
  173. }
  174. if (!$this->graph->hasNode($id)) {
  175. return true;
  176. }
  177. if ($this->currentId == $id) {
  178. return false;
  179. }
  180. $this->connectedIds[$id] = true;
  181. $srcIds = [];
  182. $srcCount = 0;
  183. $isReferencedByConstructor = false;
  184. foreach ($this->graph->getNode($id)->getInEdges() as $edge) {
  185. $isReferencedByConstructor = $isReferencedByConstructor || $edge->isReferencedByConstructor();
  186. $srcId = $edge->getSourceNode()->getId();
  187. $this->connectedIds[$srcId] = true;
  188. if ($edge->isWeak() || $edge->isLazy()) {
  189. return false;
  190. }
  191. $srcIds[$srcId] = true;
  192. ++$srcCount;
  193. }
  194. if (1 !== \count($srcIds)) {
  195. $this->notInlinedIds[$id] = true;
  196. return false;
  197. }
  198. if ($srcCount > 1 && \is_array($factory = $definition->getFactory()) && ($factory[0] instanceof Reference || $factory[0] instanceof Definition)) {
  199. return false;
  200. }
  201. return $this->container->getDefinition($srcId)->isShared();
  202. }
  203. }