AnalyzeServiceReferencesPass.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. $this->repeatedPass = $repeatedPass;
  45. }
  46. /**
  47. * Processes a ContainerBuilder object to populate the service reference graph.
  48. *
  49. * @param ContainerBuilder $container
  50. */
  51. public function process(ContainerBuilder $container)
  52. {
  53. $this->container = $container;
  54. $this->graph = $container->getCompiler()->getServiceReferenceGraph();
  55. $this->graph->clear();
  56. foreach ($container->getDefinitions() as $id => $definition) {
  57. if ($definition->isSynthetic() || $definition->isAbstract()) {
  58. continue;
  59. }
  60. $this->currentId = $id;
  61. $this->currentDefinition = $definition;
  62. $this->processArguments($definition->getArguments());
  63. if (!$this->onlyConstructorArguments) {
  64. $this->processArguments($definition->getMethodCalls());
  65. $this->processArguments($definition->getProperties());
  66. }
  67. }
  68. foreach ($container->getAliases() as $id => $alias) {
  69. $this->graph->connect($id, $alias, (string) $alias, $this->getDefinition((string) $alias), null);
  70. }
  71. }
  72. /**
  73. * Processes service definitions for arguments to find relationships for the service graph.
  74. *
  75. * @param array $arguments An array of Reference or Definition objects relating to service definitions
  76. */
  77. private function processArguments(array $arguments)
  78. {
  79. foreach ($arguments as $argument) {
  80. if (is_array($argument)) {
  81. $this->processArguments($argument);
  82. } else if ($argument instanceof Reference) {
  83. $this->graph->connect(
  84. $this->currentId,
  85. $this->currentDefinition,
  86. (string) $argument,
  87. $this->getDefinition((string) $argument),
  88. $argument
  89. );
  90. } else if ($argument instanceof Definition) {
  91. $this->processArguments($argument->getArguments());
  92. $this->processArguments($argument->getMethodCalls());
  93. $this->processArguments($argument->getProperties());
  94. }
  95. }
  96. }
  97. /**
  98. * Returns a service definition given the full name or an alias.
  99. *
  100. * @param string $id A full id or alias for a service definition.
  101. * @return Definition The definition related to the supplied id
  102. */
  103. private function getDefinition($id)
  104. {
  105. while ($this->container->hasAlias($id)) {
  106. $id = (string) $this->container->getAlias($id);
  107. }
  108. if (!$this->container->hasDefinition($id)) {
  109. return null;
  110. }
  111. return $this->container->getDefinition($id);
  112. }
  113. }