ResolveReferencesToAliasesPass.php 4.0 KB

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