ReplaceAliasByActualDefinitionPass.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Exception\InvalidArgumentException;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. /**
  15. * Replaces aliases with actual service definitions, effectively removing these
  16. * aliases.
  17. *
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface
  21. {
  22. private $compiler;
  23. private $formatter;
  24. /**
  25. * Process the Container to replace aliases with service definitions.
  26. *
  27. * @throws InvalidArgumentException if the service definition does not exist
  28. */
  29. public function process(ContainerBuilder $container)
  30. {
  31. // Setup
  32. $this->compiler = $container->getCompiler();
  33. $this->formatter = $this->compiler->getLoggingFormatter();
  34. // First collect all alias targets that need to be replaced
  35. $seenAliasTargets = array();
  36. $replacements = array();
  37. foreach ($container->getAliases() as $definitionId => $target) {
  38. $targetId = (string) $target;
  39. // Special case: leave this target alone
  40. if ('service_container' === $targetId) {
  41. continue;
  42. }
  43. // Check if target needs to be replaces
  44. if (isset($replacements[$targetId])) {
  45. $container->setAlias($definitionId, $replacements[$targetId]);
  46. }
  47. // No need to process the same target twice
  48. if (isset($seenAliasTargets[$targetId])) {
  49. continue;
  50. }
  51. // Process new target
  52. $seenAliasTargets[$targetId] = true;
  53. try {
  54. $definition = $container->getDefinition($targetId);
  55. } catch (InvalidArgumentException $e) {
  56. throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $definitionId, $targetId), null, $e);
  57. }
  58. if ($definition->isPublic()) {
  59. continue;
  60. }
  61. // Remove private definition and schedule for replacement
  62. $definition->setPublic(true);
  63. $container->setDefinition($definitionId, $definition);
  64. $container->removeDefinition($targetId);
  65. $replacements[$targetId] = $definitionId;
  66. }
  67. // Now replace target instances in all definitions
  68. foreach ($container->getDefinitions() as $definitionId => $definition) {
  69. $definition->setArguments($this->updateArgumentReferences($replacements, $definitionId, $definition->getArguments()));
  70. $definition->setMethodCalls($this->updateArgumentReferences($replacements, $definitionId, $definition->getMethodCalls()));
  71. $definition->setProperties($this->updateArgumentReferences($replacements, $definitionId, $definition->getProperties()));
  72. $definition->setFactoryService($this->updateFactoryReferenceId($replacements, $definition->getFactoryService(false)), false);
  73. $definition->setFactory($this->updateFactoryReference($replacements, $definition->getFactory()));
  74. }
  75. }
  76. /**
  77. * Recursively updates references in an array.
  78. *
  79. * @param array $replacements Table of aliases to replace
  80. * @param string $definitionId Identifier of this definition
  81. * @param array $arguments Where to replace the aliases
  82. *
  83. * @return array
  84. */
  85. private function updateArgumentReferences(array $replacements, $definitionId, array $arguments)
  86. {
  87. foreach ($arguments as $k => $argument) {
  88. // Handle recursion step
  89. if (\is_array($argument)) {
  90. $arguments[$k] = $this->updateArgumentReferences($replacements, $definitionId, $argument);
  91. continue;
  92. }
  93. // Skip arguments that don't need replacement
  94. if (!$argument instanceof Reference) {
  95. continue;
  96. }
  97. $referenceId = (string) $argument;
  98. if (!isset($replacements[$referenceId])) {
  99. continue;
  100. }
  101. // Perform the replacement
  102. $newId = $replacements[$referenceId];
  103. $arguments[$k] = new Reference($newId, $argument->getInvalidBehavior());
  104. $this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $definitionId, $referenceId, $newId));
  105. }
  106. return $arguments;
  107. }
  108. /**
  109. * Returns the updated reference for the factory service.
  110. *
  111. * @param array $replacements Table of aliases to replace
  112. * @param string|null $referenceId Factory service reference identifier
  113. *
  114. * @return string|null
  115. */
  116. private function updateFactoryReferenceId(array $replacements, $referenceId)
  117. {
  118. if (null === $referenceId) {
  119. return;
  120. }
  121. return isset($replacements[$referenceId]) ? $replacements[$referenceId] : $referenceId;
  122. }
  123. private function updateFactoryReference(array $replacements, $factory)
  124. {
  125. if (\is_array($factory) && $factory[0] instanceof Reference && isset($replacements[$referenceId = (string) $factory[0]])) {
  126. $factory[0] = new Reference($replacements[$referenceId], $factory[0]->getInvalidBehavior());
  127. }
  128. return $factory;
  129. }
  130. }