ReplaceAliasByActualDefinitionPass.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\Exception\ServiceNotFoundException;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Replaces aliases with actual service definitions, effectively removing these
  17. * aliases.
  18. *
  19. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  20. */
  21. class ReplaceAliasByActualDefinitionPass extends AbstractRecursivePass
  22. {
  23. private $replacements;
  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. // First collect all alias targets that need to be replaced
  32. $seenAliasTargets = [];
  33. $replacements = [];
  34. foreach ($container->getAliases() as $definitionId => $target) {
  35. $targetId = (string) $target;
  36. // Special case: leave this target alone
  37. if ('service_container' === $targetId) {
  38. continue;
  39. }
  40. // Check if target needs to be replaces
  41. if (isset($replacements[$targetId])) {
  42. $container->setAlias($definitionId, $replacements[$targetId])->setPublic($target->isPublic());
  43. }
  44. // No need to process the same target twice
  45. if (isset($seenAliasTargets[$targetId])) {
  46. continue;
  47. }
  48. // Process new target
  49. $seenAliasTargets[$targetId] = true;
  50. try {
  51. $definition = $container->getDefinition($targetId);
  52. } catch (ServiceNotFoundException $e) {
  53. if ('' !== $e->getId() && '@' === $e->getId()[0]) {
  54. throw new ServiceNotFoundException($e->getId(), $e->getSourceId(), null, [substr($e->getId(), 1)]);
  55. }
  56. throw $e;
  57. }
  58. if ($definition->isPublic()) {
  59. continue;
  60. }
  61. // Remove private definition and schedule for replacement
  62. $definition->setPublic($target->isPublic());
  63. $container->setDefinition($definitionId, $definition);
  64. $container->removeDefinition($targetId);
  65. $replacements[$targetId] = $definitionId;
  66. }
  67. $this->replacements = $replacements;
  68. parent::process($container);
  69. $this->replacements = [];
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. protected function processValue($value, bool $isRoot = false)
  75. {
  76. if ($value instanceof Reference && isset($this->replacements[$referenceId = (string) $value])) {
  77. // Perform the replacement
  78. $newId = $this->replacements[$referenceId];
  79. $value = new Reference($newId, $value->getInvalidBehavior());
  80. $this->container->log($this, sprintf('Changed reference of service "%s" previously pointing to "%s" to "%s".', $this->currentId, $referenceId, $newId));
  81. }
  82. return parent::processValue($value, $isRoot);
  83. }
  84. }