Drupal investigation

AnalyzeServiceReferencesPass.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * Run this pass before passes that need to know more about the relation of
  16. * your services.
  17. *
  18. * This class will populate the ServiceReferenceGraph with information. You can
  19. * retrieve the graph in other passes from the compiler.
  20. *
  21. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  22. */
  23. class AnalyzeServiceReferencesPass implements RepeatablePassInterface
  24. {
  25. private $graph;
  26. private $container;
  27. private $currentId;
  28. private $currentDefinition;
  29. private $repeatedPass;
  30. private $onlyConstructorArguments;
  31. /**
  32. * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
  33. */
  34. public function __construct($onlyConstructorArguments = false)
  35. {
  36. $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function setRepeatedPass(RepeatedPass $repeatedPass)
  42. {
  43. $this->repeatedPass = $repeatedPass;
  44. }
  45. /**
  46. * Processes a ContainerBuilder object to populate the service reference graph.
  47. *
  48. * @param ContainerBuilder $container
  49. */
  50. public function process(ContainerBuilder $container)
  51. {
  52. $this->container = $container;
  53. $this->graph = $container->getCompiler()->getServiceReferenceGraph();
  54. $this->graph->clear();
  55. foreach ($container->getDefinitions() as $id => $definition) {
  56. if ($definition->isSynthetic() || $definition->isAbstract()) {
  57. continue;
  58. }
  59. $this->currentId = $id;
  60. $this->currentDefinition = $definition;
  61. $this->processArguments($definition->getArguments());
  62. if ($definition->getFactoryService(false)) {
  63. $this->processArguments(array(new Reference($definition->getFactoryService(false))));
  64. }
  65. if (is_array($definition->getFactory())) {
  66. $this->processArguments($definition->getFactory());
  67. }
  68. if (!$this->onlyConstructorArguments) {
  69. $this->processArguments($definition->getMethodCalls());
  70. $this->processArguments($definition->getProperties());
  71. if ($definition->getConfigurator()) {
  72. $this->processArguments(array($definition->getConfigurator()));
  73. }
  74. }
  75. }
  76. foreach ($container->getAliases() as $id => $alias) {
  77. $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
  78. }
  79. }
  80. /**
  81. * Processes service definitions for arguments to find relationships for the service graph.
  82. *
  83. * @param array $arguments An array of Reference or Definition objects relating to service definitions
  84. */
  85. private function processArguments(array $arguments)
  86. {
  87. foreach ($arguments as $argument) {
  88. if (is_array($argument)) {
  89. $this->processArguments($argument);
  90. } elseif ($argument instanceof Reference) {
  91. $this->graph->connect(
  92. $this->currentId,
  93. $this->currentDefinition,
  94. $this->getDefinitionId((string) $argument),
  95. $this->getDefinition((string) $argument),
  96. $argument
  97. );
  98. } elseif ($argument instanceof Definition) {
  99. $this->processArguments($argument->getArguments());
  100. $this->processArguments($argument->getMethodCalls());
  101. $this->processArguments($argument->getProperties());
  102. if (is_array($argument->getFactory())) {
  103. $this->processArguments($argument->getFactory());
  104. }
  105. if ($argument->getFactoryService(false)) {
  106. $this->processArguments(array(new Reference($argument->getFactoryService(false))));
  107. }
  108. }
  109. }
  110. }
  111. /**
  112. * Returns a service definition given the full name or an alias.
  113. *
  114. * @param string $id A full id or alias for a service definition
  115. *
  116. * @return Definition|null The definition related to the supplied id
  117. */
  118. private function getDefinition($id)
  119. {
  120. $id = $this->getDefinitionId($id);
  121. return null === $id ? null : $this->container->getDefinition($id);
  122. }
  123. private function getDefinitionId($id)
  124. {
  125. while ($this->container->hasAlias($id)) {
  126. $id = (string) $this->container->getAlias($id);
  127. }
  128. if (!$this->container->hasDefinition($id)) {
  129. return;
  130. }
  131. return $id;
  132. }
  133. }