PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php

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