ResolveReferencesToAliasesPass.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Argument\ArgumentInterface;
  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. 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. if (isset($definition->getChanges()['factory'])) {
  38. $definition->setFactory($this->processFactory($definition->getFactory()));
  39. }
  40. }
  41. foreach ($container->getAliases() as $id => $alias) {
  42. $aliasId = (string) $alias;
  43. if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) {
  44. $container->setAlias($id, new Alias($defId, $alias->isPublic()));
  45. }
  46. }
  47. }
  48. /**
  49. * Processes the arguments to replace aliases.
  50. *
  51. * @param array $arguments An array of References
  52. *
  53. * @return array An array of References
  54. */
  55. private function processArguments(array $arguments)
  56. {
  57. foreach ($arguments as $k => $argument) {
  58. if (is_array($argument)) {
  59. $arguments[$k] = $this->processArguments($argument);
  60. } elseif ($argument instanceof ArgumentInterface) {
  61. $argument->setValues($this->processArguments($argument->getValues()));
  62. } elseif ($argument instanceof Reference) {
  63. $defId = $this->getDefinitionId($id = (string) $argument);
  64. if ($defId !== $id) {
  65. $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior());
  66. }
  67. }
  68. }
  69. return $arguments;
  70. }
  71. private function processFactory($factory)
  72. {
  73. if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
  74. return $factory;
  75. }
  76. $defId = $this->getDefinitionId($id = (string) $factory[0]);
  77. if ($defId !== $id) {
  78. $factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior());
  79. }
  80. return $factory;
  81. }
  82. /**
  83. * Resolves an alias into a definition id.
  84. *
  85. * @param string $id The definition or alias id to resolve
  86. *
  87. * @return string The definition id with aliases resolved
  88. */
  89. private function getDefinitionId($id)
  90. {
  91. $seen = array();
  92. while ($this->container->hasAlias($id)) {
  93. if (isset($seen[$id])) {
  94. throw new ServiceCircularReferenceException($id, array_keys($seen));
  95. }
  96. $seen[$id] = true;
  97. $id = (string) $this->container->getAlias($id);
  98. }
  99. return $id;
  100. }
  101. }