ReplaceAliasByActualDefinitionPass.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "%s".', $alias, $id), 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. }
  82. }
  83. /**
  84. * Updates argument references.
  85. *
  86. * @param array $arguments An array of Arguments
  87. * @param string $currentId The alias identifier
  88. * @param string $newId The identifier the alias points to
  89. *
  90. * @return array
  91. */
  92. private function updateArgumentReferences(array $arguments, $currentId, $newId)
  93. {
  94. foreach ($arguments as $k => $argument) {
  95. if (is_array($argument)) {
  96. $arguments[$k] = $this->updateArgumentReferences($argument, $currentId, $newId);
  97. } elseif ($argument instanceof Reference) {
  98. if ($currentId === (string) $argument) {
  99. $arguments[$k] = new Reference($newId, $argument->getInvalidBehavior());
  100. $this->compiler->addLogMessage($this->formatter->formatUpdateReference($this, $this->sourceId, $currentId, $newId));
  101. }
  102. }
  103. }
  104. return $arguments;
  105. }
  106. }