ResolveInvalidReferencesPass.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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\Argument\ArgumentInterface;
  12. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  17. use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
  18. use Symfony\Component\DependencyInjection\Reference;
  19. use Symfony\Component\DependencyInjection\TypedReference;
  20. /**
  21. * Emulates the invalid behavior if the reference is not found within the
  22. * container.
  23. *
  24. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  25. */
  26. class ResolveInvalidReferencesPass implements CompilerPassInterface
  27. {
  28. private $container;
  29. private $signalingException;
  30. private $currentId;
  31. /**
  32. * Process the ContainerBuilder to resolve invalid references.
  33. */
  34. public function process(ContainerBuilder $container)
  35. {
  36. $this->container = $container;
  37. $this->signalingException = new RuntimeException('Invalid reference.');
  38. try {
  39. foreach ($container->getDefinitions() as $this->currentId => $definition) {
  40. $this->processValue($definition);
  41. }
  42. } finally {
  43. $this->container = $this->signalingException = null;
  44. }
  45. }
  46. /**
  47. * Processes arguments to determine invalid references.
  48. *
  49. * @return mixed
  50. *
  51. * @throws RuntimeException When an invalid reference is found
  52. */
  53. private function processValue($value, int $rootLevel = 0, int $level = 0)
  54. {
  55. if ($value instanceof ServiceClosureArgument) {
  56. $value->setValues($this->processValue($value->getValues(), 1, 1));
  57. } elseif ($value instanceof ArgumentInterface) {
  58. $value->setValues($this->processValue($value->getValues(), $rootLevel, 1 + $level));
  59. } elseif ($value instanceof Definition) {
  60. if ($value->isSynthetic() || $value->isAbstract()) {
  61. return $value;
  62. }
  63. $value->setArguments($this->processValue($value->getArguments(), 0));
  64. $value->setProperties($this->processValue($value->getProperties(), 1));
  65. $value->setMethodCalls($this->processValue($value->getMethodCalls(), 2));
  66. } elseif (\is_array($value)) {
  67. $i = 0;
  68. foreach ($value as $k => $v) {
  69. try {
  70. if (false !== $i && $k !== $i++) {
  71. $i = false;
  72. }
  73. if ($v !== $processedValue = $this->processValue($v, $rootLevel, 1 + $level)) {
  74. $value[$k] = $processedValue;
  75. }
  76. } catch (RuntimeException $e) {
  77. if ($rootLevel < $level || ($rootLevel && !$level)) {
  78. unset($value[$k]);
  79. } elseif ($rootLevel) {
  80. throw $e;
  81. } else {
  82. $value[$k] = null;
  83. }
  84. }
  85. }
  86. // Ensure numerically indexed arguments have sequential numeric keys.
  87. if (false !== $i) {
  88. $value = array_values($value);
  89. }
  90. } elseif ($value instanceof Reference) {
  91. if ($this->container->has($id = (string) $value)) {
  92. return $value;
  93. }
  94. $currentDefinition = $this->container->getDefinition($this->currentId);
  95. // resolve decorated service behavior depending on decorator service
  96. if ($currentDefinition->innerServiceId === $id && ContainerInterface::NULL_ON_INVALID_REFERENCE === $currentDefinition->decorationOnInvalid) {
  97. return null;
  98. }
  99. $invalidBehavior = $value->getInvalidBehavior();
  100. if (ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior && $value instanceof TypedReference && !$this->container->has($id)) {
  101. $e = new ServiceNotFoundException($id, $this->currentId);
  102. // since the error message varies by $id and $this->currentId, so should the id of the dummy errored definition
  103. $this->container->register($id = sprintf('.errored.%s.%s', $this->currentId, $id), $value->getType())
  104. ->addError($e->getMessage());
  105. return new TypedReference($id, $value->getType(), $value->getInvalidBehavior());
  106. }
  107. // resolve invalid behavior
  108. if (ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
  109. $value = null;
  110. } elseif (ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) {
  111. if (0 < $level || $rootLevel) {
  112. throw $this->signalingException;
  113. }
  114. $value = null;
  115. }
  116. }
  117. return $value;
  118. }
  119. }