Drupal investigation

ResolveReferencesToAliasesPass.php 3.9KB

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\Exception\ServiceCircularReferenceException;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. use Symfony\Component\DependencyInjection\ContainerBuilder;
  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. * @param ContainerBuilder $container
  27. */
  28. public function process(ContainerBuilder $container)
  29. {
  30. $this->container = $container;
  31. foreach ($container->getDefinitions() as $definition) {
  32. if ($definition->isSynthetic() || $definition->isAbstract()) {
  33. continue;
  34. }
  35. $definition->setArguments($this->processArguments($definition->getArguments()));
  36. $definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
  37. $definition->setProperties($this->processArguments($definition->getProperties()));
  38. $definition->setFactory($this->processFactory($definition->getFactory()));
  39. $definition->setFactoryService($this->processFactoryService($definition->getFactoryService(false)), false);
  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 Reference) {
  61. $defId = $this->getDefinitionId($id = (string) $argument);
  62. if ($defId !== $id) {
  63. $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior(), $argument->isStrict(false));
  64. }
  65. }
  66. }
  67. return $arguments;
  68. }
  69. private function processFactoryService($factoryService)
  70. {
  71. if (null === $factoryService) {
  72. return;
  73. }
  74. return $this->getDefinitionId($factoryService);
  75. }
  76. private function processFactory($factory)
  77. {
  78. if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
  79. return $factory;
  80. }
  81. $defId = $this->getDefinitionId($id = (string) $factory[0]);
  82. if ($defId !== $id) {
  83. $factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior(), $factory[0]->isStrict(false));
  84. }
  85. return $factory;
  86. }
  87. /**
  88. * Resolves an alias into a definition id.
  89. *
  90. * @param string $id The definition or alias id to resolve
  91. *
  92. * @return string The definition id with aliases resolved
  93. */
  94. private function getDefinitionId($id)
  95. {
  96. $seen = array();
  97. while ($this->container->hasAlias($id)) {
  98. if (isset($seen[$id])) {
  99. throw new ServiceCircularReferenceException($id, array_keys($seen));
  100. }
  101. $seen[$id] = true;
  102. $id = (string) $this->container->getAlias($id);
  103. }
  104. return $id;
  105. }
  106. }