InlineServiceDefinitionsPass.php 7.7 KB

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