Drupal investigation

ReplaceAliasByActualDefinitionPass.php 5.6KB

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