Drupal investigation

InlineServiceDefinitionsPass.php 4.9KB

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\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  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 $repeatedPass;
  23. private $graph;
  24. private $compiler;
  25. private $formatter;
  26. private $currentId;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function setRepeatedPass(RepeatedPass $repeatedPass)
  31. {
  32. $this->repeatedPass = $repeatedPass;
  33. }
  34. /**
  35. * Processes the ContainerBuilder for inline service definitions.
  36. *
  37. * @param ContainerBuilder $container
  38. */
  39. public function process(ContainerBuilder $container)
  40. {
  41. $this->compiler = $container->getCompiler();
  42. $this->formatter = $this->compiler->getLoggingFormatter();
  43. $this->graph = $this->compiler->getServiceReferenceGraph();
  44. $container->setDefinitions($this->inlineArguments($container, $container->getDefinitions(), true));
  45. }
  46. /**
  47. * Processes inline arguments.
  48. *
  49. * @param ContainerBuilder $container The ContainerBuilder
  50. * @param array $arguments An array of arguments
  51. * @param bool $isRoot If we are processing the root definitions or not
  52. *
  53. * @return array
  54. */
  55. private function inlineArguments(ContainerBuilder $container, array $arguments, $isRoot = false)
  56. {
  57. foreach ($arguments as $k => $argument) {
  58. if ($isRoot) {
  59. $this->currentId = $k;
  60. }
  61. if (is_array($argument)) {
  62. $arguments[$k] = $this->inlineArguments($container, $argument);
  63. } elseif ($argument instanceof Reference) {
  64. if (!$container->hasDefinition($id = (string) $argument)) {
  65. continue;
  66. }
  67. if ($this->isInlineableDefinition($container, $id, $definition = $container->getDefinition($id))) {
  68. $this->compiler->addLogMessage($this->formatter->formatInlineService($this, $id, $this->currentId));
  69. if ($definition->isShared() && ContainerInterface::SCOPE_PROTOTYPE !== $definition->getScope(false)) {
  70. $arguments[$k] = $definition;
  71. } else {
  72. $arguments[$k] = clone $definition;
  73. }
  74. }
  75. } elseif ($argument instanceof Definition) {
  76. $argument->setArguments($this->inlineArguments($container, $argument->getArguments()));
  77. $argument->setMethodCalls($this->inlineArguments($container, $argument->getMethodCalls()));
  78. $argument->setProperties($this->inlineArguments($container, $argument->getProperties()));
  79. $configurator = $this->inlineArguments($container, array($argument->getConfigurator()));
  80. $argument->setConfigurator($configurator[0]);
  81. $factory = $this->inlineArguments($container, array($argument->getFactory()));
  82. $argument->setFactory($factory[0]);
  83. }
  84. }
  85. return $arguments;
  86. }
  87. /**
  88. * Checks if the definition is inlineable.
  89. *
  90. * @param ContainerBuilder $container
  91. * @param string $id
  92. * @param Definition $definition
  93. *
  94. * @return bool If the definition is inlineable
  95. */
  96. private function isInlineableDefinition(ContainerBuilder $container, $id, Definition $definition)
  97. {
  98. if (!$definition->isShared() || ContainerInterface::SCOPE_PROTOTYPE === $definition->getScope(false)) {
  99. return true;
  100. }
  101. if ($definition->isPublic() || $definition->isLazy()) {
  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. }