ResolveReferencesToAliasesPass.php 2.8 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\Alias;
  12. use Symfony\Component\DependencyInjection\Reference;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. /**
  15. * Replaces all references to aliases with references to the actual service.
  16. *
  17. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18. */
  19. class ResolveReferencesToAliasesPass implements CompilerPassInterface
  20. {
  21. private $container;
  22. /**
  23. * Processes the ContainerBuilder to replace references to aliases with actual service references.
  24. *
  25. * @param ContainerBuilder $container
  26. */
  27. public function process(ContainerBuilder $container)
  28. {
  29. $this->container = $container;
  30. foreach ($container->getDefinitions() as $definition) {
  31. if ($definition->isSynthetic() || $definition->isAbstract()) {
  32. continue;
  33. }
  34. $definition->setArguments($this->processArguments($definition->getArguments()));
  35. $definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
  36. $definition->setProperties($this->processArguments($definition->getProperties()));
  37. }
  38. foreach ($container->getAliases() as $id => $alias) {
  39. $aliasId = (string) $alias;
  40. if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) {
  41. $container->setAlias($id, new Alias($defId, $alias->isPublic()));
  42. }
  43. }
  44. }
  45. /**
  46. * Processes the arguments to replace aliases.
  47. *
  48. * @param array $arguments An array of References
  49. *
  50. * @return array An array of References
  51. */
  52. private function processArguments(array $arguments)
  53. {
  54. foreach ($arguments as $k => $argument) {
  55. if (is_array($argument)) {
  56. $arguments[$k] = $this->processArguments($argument);
  57. } elseif ($argument instanceof Reference) {
  58. $defId = $this->getDefinitionId($id = (string) $argument);
  59. if ($defId !== $id) {
  60. $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior(), $argument->isStrict());
  61. }
  62. }
  63. }
  64. return $arguments;
  65. }
  66. /**
  67. * Resolves an alias into a definition id.
  68. *
  69. * @param string $id The definition or alias id to resolve
  70. *
  71. * @return string The definition id with aliases resolved
  72. */
  73. private function getDefinitionId($id)
  74. {
  75. while ($this->container->hasAlias($id)) {
  76. $id = (string) $this->container->getAlias($id);
  77. }
  78. return $id;
  79. }
  80. }