AnalyzeServiceReferencesPass.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\Definition;
  13. use Symfony\Component\DependencyInjection\Reference;
  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 $onlyConstructorArguments;
  30. /**
  31. * @param bool $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
  32. */
  33. public function __construct($onlyConstructorArguments = false)
  34. {
  35. $this->onlyConstructorArguments = (bool) $onlyConstructorArguments;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function setRepeatedPass(RepeatedPass $repeatedPass)
  41. {
  42. // no-op for BC
  43. }
  44. /**
  45. * Processes a ContainerBuilder object to populate the service reference graph.
  46. */
  47. public function process(ContainerBuilder $container)
  48. {
  49. $this->container = $container;
  50. $this->graph = $container->getCompiler()->getServiceReferenceGraph();
  51. $this->graph->clear();
  52. foreach ($container->getDefinitions() as $id => $definition) {
  53. if ($definition->isSynthetic() || $definition->isAbstract()) {
  54. continue;
  55. }
  56. $this->currentId = $id;
  57. $this->currentDefinition = $definition;
  58. $this->processArguments($definition->getArguments());
  59. if ($definition->getFactoryService(false)) {
  60. $this->processArguments(array(new Reference($definition->getFactoryService(false))));
  61. }
  62. if (\is_array($definition->getFactory())) {
  63. $this->processArguments($definition->getFactory());
  64. }
  65. if (!$this->onlyConstructorArguments) {
  66. $this->processArguments($definition->getMethodCalls());
  67. $this->processArguments($definition->getProperties());
  68. if ($definition->getConfigurator()) {
  69. $this->processArguments(array($definition->getConfigurator()));
  70. }
  71. }
  72. }
  73. foreach ($container->getAliases() as $id => $alias) {
  74. $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
  75. }
  76. }
  77. /**
  78. * Processes service definitions for arguments to find relationships for the service graph.
  79. *
  80. * @param array $arguments An array of Reference or Definition objects relating to service definitions
  81. */
  82. private function processArguments(array $arguments)
  83. {
  84. foreach ($arguments as $argument) {
  85. if (\is_array($argument)) {
  86. $this->processArguments($argument);
  87. } elseif ($argument instanceof Reference) {
  88. $this->graph->connect(
  89. $this->currentId,
  90. $this->currentDefinition,
  91. $this->getDefinitionId((string) $argument),
  92. $this->getDefinition((string) $argument),
  93. $argument
  94. );
  95. } elseif ($argument instanceof Definition) {
  96. $this->processArguments($argument->getArguments());
  97. $this->processArguments($argument->getMethodCalls());
  98. $this->processArguments($argument->getProperties());
  99. if (\is_array($argument->getFactory())) {
  100. $this->processArguments($argument->getFactory());
  101. }
  102. if ($argument->getFactoryService(false)) {
  103. $this->processArguments(array(new Reference($argument->getFactoryService(false))));
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * Returns a service definition given the full name or an alias.
  110. *
  111. * @param string $id A full id or alias for a service definition
  112. *
  113. * @return Definition|null The definition related to the supplied id
  114. */
  115. private function getDefinition($id)
  116. {
  117. $id = $this->getDefinitionId($id);
  118. return null === $id ? null : $this->container->getDefinition($id);
  119. }
  120. private function getDefinitionId($id)
  121. {
  122. while ($this->container->hasAlias($id)) {
  123. $id = (string) $this->container->getAlias($id);
  124. }
  125. if (!$this->container->hasDefinition($id)) {
  126. return;
  127. }
  128. return $id;
  129. }
  130. }