ReplaceAliasByActualDefinitionPass.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. private $sourceId;
  25. /**
  26. * Process the Container to replace aliases with service definitions.
  27. *
  28. * @param ContainerBuilder $container
  29. *
  30. * @throws InvalidArgumentException if the service definition does not exist
  31. */
  32. public function process(ContainerBuilder $container)
  33. {
  34. $this->compiler = $container->getCompiler();
  35. $this->formatter = $this->compiler->getLoggingFormatter();
  36. foreach ($container->getAliases() as $id => $alias) {
  37. $aliasId = (string) $alias;
  38. try {
  39. $definition = $container->getDefinition($aliasId);
  40. } catch (InvalidArgumentException $e) {
  41. throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $id, $alias), null, $e);
  42. }
  43. if ($definition->isPublic()) {
  44. continue;
  45. }
  46. $definition->setPublic(true);
  47. $container->setDefinition($id, $definition);
  48. $container->removeDefinition($aliasId);
  49. $this->updateReferences($container, $aliasId, $id);
  50. // we have to restart the process due to concurrent modification of
  51. // the container
  52. $this->process($container);
  53. break;
  54. }
  55. }
  56. /**
  57. * Updates references to remove aliases.
  58. *
  59. * @param ContainerBuilder $container The container
  60. * @param string $currentId The alias identifier being replaced
  61. * @param string $newId The id of the service the alias points to
  62. */
  63. private function updateReferences($container, $currentId, $newId)
  64. {
  65. foreach ($container->getAliases() as $id => $alias) {
  66. if ($currentId === (string) $alias) {
  67. $container->setAlias($id, $newId);
  68. }
  69. }
  70. foreach ($container->getDefinitions() as $id => $definition) {
  71. $this->sourceId = $id;
  72. $definition->setArguments(
  73. $this->updateArgumentReferences($definition->getArguments(), $currentId, $newId)
  74. );
  75. $definition->setMethodCalls(
  76. $this->updateArgumentReferences($definition->getMethodCalls(), $currentId, $newId)
  77. );
  78. $definition->setProperties(
  79. $this->updateArgumentReferences($definition->getProperties(), $currentId, $newId)
  80. );
  81. $definition->setFactoryService($this->updateFactoryServiceReference($definition->getFactoryService(false), $currentId, $newId), false);
  82. $definition->setFactory($this->updateFactoryReference($definition->getFactory(), $currentId, $newId));
  83. }
  84. }
  85. /**
  86. * Updates argument references.
  87. *
  88. * @param array $arguments An array of Arguments
  89. * @param string $currentId The alias identifier
  90. * @param string $newId The identifier the alias points to
  91. *
  92. * @return array
  93. */
  94. private function updateArgumentReferences(array $arguments, $currentId, $newId)
  95. {
  96. foreach ($arguments as $k => $argument) {
  97. if (is_array($argument)) {
  98. $arguments[$k] = $this->updateArgumentReferences($argument, $currentId, $newId);
  99. } elseif ($argument instanceof Reference) {
  100. if ($currentId === (string) $argument) {
  101. $arguments[$k] = new Reference($newId, $argument->getInvalidBehavior());
  102. $this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $this->sourceId, $currentId, $newId));
  103. }
  104. }
  105. }
  106. return $arguments;
  107. }
  108. private function updateFactoryServiceReference($factoryService, $currentId, $newId)
  109. {
  110. if (null === $factoryService) {
  111. return;
  112. }
  113. return $currentId === $factoryService ? $newId : $factoryService;
  114. }
  115. private function updateFactoryReference($factory, $currentId, $newId)
  116. {
  117. if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
  118. return $factory;
  119. }
  120. if ($currentId === (string) $factory[0]) {
  121. $factory[0] = new Reference($newId, $factory[0]->getInvalidBehavior());
  122. }
  123. return $factory;
  124. }
  125. }