ResolveReferencesToAliasesPass.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Replaces all references to aliases with references to the actual service.
  17. *
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. class ResolveReferencesToAliasesPass implements CompilerPassInterface
  21. {
  22. private $container;
  23. /**
  24. * Processes the ContainerBuilder to replace references to aliases with actual service references.
  25. */
  26. public function process(ContainerBuilder $container)
  27. {
  28. $this->container = $container;
  29. foreach ($container->getDefinitions() as $definition) {
  30. if ($definition->isSynthetic() || $definition->isAbstract()) {
  31. continue;
  32. }
  33. $definition->setArguments($this->processArguments($definition->getArguments()));
  34. $definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
  35. $definition->setProperties($this->processArguments($definition->getProperties()));
  36. $definition->setFactory($this->processFactory($definition->getFactory()));
  37. $definition->setFactoryService($this->processFactoryService($definition->getFactoryService(false)), false);
  38. }
  39. foreach ($container->getAliases() as $id => $alias) {
  40. $aliasId = (string) $alias;
  41. if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) {
  42. $container->setAlias($id, new Alias($defId, $alias->isPublic()));
  43. }
  44. }
  45. }
  46. /**
  47. * Processes the arguments to replace aliases.
  48. *
  49. * @param array $arguments An array of References
  50. *
  51. * @return array An array of References
  52. */
  53. private function processArguments(array $arguments)
  54. {
  55. foreach ($arguments as $k => $argument) {
  56. if (\is_array($argument)) {
  57. $arguments[$k] = $this->processArguments($argument);
  58. } elseif ($argument instanceof Reference) {
  59. $defId = $this->getDefinitionId($id = (string) $argument);
  60. if ($defId !== $id) {
  61. $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior(), $argument->isStrict(false));
  62. }
  63. }
  64. }
  65. return $arguments;
  66. }
  67. private function processFactoryService($factoryService)
  68. {
  69. if (null === $factoryService) {
  70. return;
  71. }
  72. return $this->getDefinitionId($factoryService);
  73. }
  74. private function processFactory($factory)
  75. {
  76. if (null === $factory || !\is_array($factory) || !$factory[0] instanceof Reference) {
  77. return $factory;
  78. }
  79. $defId = $this->getDefinitionId($id = (string) $factory[0]);
  80. if ($defId !== $id) {
  81. $factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior(), $factory[0]->isStrict(false));
  82. }
  83. return $factory;
  84. }
  85. /**
  86. * Resolves an alias into a definition id.
  87. *
  88. * @param string $id The definition or alias id to resolve
  89. *
  90. * @return string The definition id with aliases resolved
  91. */
  92. private function getDefinitionId($id)
  93. {
  94. $seen = array();
  95. while ($this->container->hasAlias($id)) {
  96. if (isset($seen[$id])) {
  97. throw new ServiceCircularReferenceException($id, array_keys($seen));
  98. }
  99. $seen[$id] = true;
  100. $id = (string) $this->container->getAlias($id);
  101. }
  102. return $id;
  103. }
  104. }