AnalyzeServiceReferencesPass.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Boolean $onlyConstructorArguments Sets this Service Reference pass to ignore method calls
  35. */
  36. public function __construct($onlyConstructorArguments = false)
  37. {
  38. $this->onlyConstructorArguments = (Boolean) $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 (!$this->onlyConstructorArguments) {
  65. $this->processArguments($definition->getMethodCalls());
  66. $this->processArguments($definition->getProperties());
  67. if ($definition->getConfigurator()) {
  68. $this->processArguments(array($definition->getConfigurator()));
  69. }
  70. }
  71. }
  72. foreach ($container->getAliases() as $id => $alias) {
  73. $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
  74. }
  75. }
  76. /**
  77. * Processes service definitions for arguments to find relationships for the service graph.
  78. *
  79. * @param array $arguments An array of Reference or Definition objects relating to service definitions
  80. */
  81. private function processArguments(array $arguments)
  82. {
  83. foreach ($arguments as $argument) {
  84. if (is_array($argument)) {
  85. $this->processArguments($argument);
  86. } elseif ($argument instanceof Reference) {
  87. $this->graph->connect(
  88. $this->currentId,
  89. $this->currentDefinition,
  90. $this->getDefinitionId((string) $argument),
  91. $this->getDefinition((string) $argument),
  92. $argument
  93. );
  94. } elseif ($argument instanceof Definition) {
  95. $this->processArguments($argument->getArguments());
  96. $this->processArguments($argument->getMethodCalls());
  97. $this->processArguments($argument->getProperties());
  98. }
  99. }
  100. }
  101. /**
  102. * Returns a service definition given the full name or an alias.
  103. *
  104. * @param string $id A full id or alias for a service definition.
  105. *
  106. * @return Definition|null The definition related to the supplied id
  107. */
  108. private function getDefinition($id)
  109. {
  110. $id = $this->getDefinitionId($id);
  111. return null === $id ? null : $this->container->getDefinition($id);
  112. }
  113. private function getDefinitionId($id)
  114. {
  115. while ($this->container->hasAlias($id)) {
  116. $id = (string) $this->container->getAlias($id);
  117. }
  118. if (!$this->container->hasDefinition($id)) {
  119. return null;
  120. }
  121. return $id;
  122. }
  123. }