AnalyzeServiceReferencesPass.php 5.0 KB

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