InlineServiceDefinitionsPass.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Inline service definitions where this is possible.
  17. *
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. class InlineServiceDefinitionsPass implements RepeatablePassInterface
  21. {
  22. private $graph;
  23. private $compiler;
  24. private $formatter;
  25. private $currentId;
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function setRepeatedPass(RepeatedPass $repeatedPass)
  30. {
  31. // no-op for BC
  32. }
  33. /**
  34. * Processes the ContainerBuilder for inline service definitions.
  35. */
  36. public function process(ContainerBuilder $container)
  37. {
  38. $this->compiler = $container->getCompiler();
  39. $this->formatter = $this->compiler->getLoggingFormatter();
  40. $this->graph = $this->compiler->getServiceReferenceGraph();
  41. $container->setDefinitions($this->inlineArguments($container, $container->getDefinitions(), true));
  42. }
  43. /**
  44. * Processes inline arguments.
  45. *
  46. * @param ContainerBuilder $container The ContainerBuilder
  47. * @param array $arguments An array of arguments
  48. * @param bool $isRoot If we are processing the root definitions or not
  49. *
  50. * @return array
  51. */
  52. private function inlineArguments(ContainerBuilder $container, array $arguments, $isRoot = false)
  53. {
  54. foreach ($arguments as $k => $argument) {
  55. if ($isRoot) {
  56. $this->currentId = $k;
  57. }
  58. if (\is_array($argument)) {
  59. $arguments[$k] = $this->inlineArguments($container, $argument);
  60. } elseif ($argument instanceof Reference) {
  61. if (!$container->hasDefinition($id = (string) $argument)) {
  62. continue;
  63. }
  64. if ($this->isInlineableDefinition($container, $id, $definition = $container->getDefinition($id))) {
  65. $this->compiler->addLogMessage($this->formatter->formatInlineService($this, $id, $this->currentId));
  66. if ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope(false)) {
  67. $arguments[$k] = $definition;
  68. } else {
  69. $arguments[$k] = clone $definition;
  70. }
  71. }
  72. } elseif ($argument instanceof Definition) {
  73. $argument->setArguments($this->inlineArguments($container, $argument->getArguments()));
  74. $argument->setMethodCalls($this->inlineArguments($container, $argument->getMethodCalls()));
  75. $argument->setProperties($this->inlineArguments($container, $argument->getProperties()));
  76. $configurator = $this->inlineArguments($container, array($argument->getConfigurator()));
  77. $argument->setConfigurator($configurator[0]);
  78. $factory = $this->inlineArguments($container, array($argument->getFactory()));
  79. $argument->setFactory($factory[0]);
  80. }
  81. }
  82. return $arguments;
  83. }
  84. /**
  85. * Checks if the definition is inlineable.
  86. *
  87. * @param ContainerBuilder $container
  88. * @param string $id
  89. * @param Definition $definition
  90. *
  91. * @return bool If the definition is inlineable
  92. */
  93. private function isInlineableDefinition(ContainerBuilder $container, $id, Definition $definition)
  94. {
  95. if ($definition->isDeprecated() || $definition->isLazy() || $definition->isSynthetic()) {
  96. return false;
  97. }
  98. if (!$definition->isShared() || ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
  99. return true;
  100. }
  101. if ($definition->isPublic()) {
  102. return false;
  103. }
  104. if (!$this->graph->hasNode($id)) {
  105. return true;
  106. }
  107. if ($this->currentId == $id) {
  108. return false;
  109. }
  110. $ids = array();
  111. foreach ($this->graph->getNode($id)->getInEdges() as $edge) {
  112. $ids[] = $edge->getSourceNode()->getId();
  113. }
  114. if (\count(array_unique($ids)) > 1) {
  115. return false;
  116. }
  117. if (\count($ids) > 1 && \is_array($factory = $definition->getFactory()) && ($factory[0] instanceof Reference || $factory[0] instanceof Definition)) {
  118. return false;
  119. }
  120. if (\count($ids) > 1 && $definition->getFactoryService(false)) {
  121. return false;
  122. }
  123. return $container->getDefinition(reset($ids))->getScope(false) === $definition->getScope(false);
  124. }
  125. }