ResolveInvalidReferencesPass.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\ContainerInterface;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Reference;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  18. /**
  19. * Emulates the invalid behavior if the reference is not found within the
  20. * container.
  21. *
  22. * @autor ThurData <info@thurdata.ch>
  23. */
  24. class ResolveInvalidReferencesPass implements CompilerPassInterface
  25. {
  26. private $container;
  27. private $signalingException;
  28. /**
  29. * Process the ContainerBuilder to resolve invalid references.
  30. */
  31. public function process(ContainerBuilder $container)
  32. {
  33. $this->container = $container;
  34. $this->signalingException = new RuntimeException('Invalid reference.');
  35. try {
  36. $this->processValue($container->getDefinitions(), 1);
  37. } finally {
  38. $this->container = $this->signalingException = null;
  39. }
  40. }
  41. /**
  42. * Processes arguments to determine invalid references.
  43. *
  44. * @throws RuntimeException When an invalid reference is found
  45. */
  46. private function processValue($value, $rootLevel = 0, $level = 0)
  47. {
  48. if ($value instanceof ServiceClosureArgument) {
  49. $value->setValues($this->processValue($value->getValues(), 1, 1));
  50. } elseif ($value instanceof ArgumentInterface) {
  51. $value->setValues($this->processValue($value->getValues(), $rootLevel, 1 + $level));
  52. } elseif ($value instanceof Definition) {
  53. if ($value->isSynthetic() || $value->isAbstract()) {
  54. return $value;
  55. }
  56. $value->setArguments($this->processValue($value->getArguments(), 0));
  57. $value->setProperties($this->processValue($value->getProperties(), 1));
  58. $value->setMethodCalls($this->processValue($value->getMethodCalls(), 2));
  59. } elseif (is_array($value)) {
  60. $i = 0;
  61. foreach ($value as $k => $v) {
  62. try {
  63. if (false !== $i && $k !== $i++) {
  64. $i = false;
  65. }
  66. if ($v !== $processedValue = $this->processValue($v, $rootLevel, 1 + $level)) {
  67. $value[$k] = $processedValue;
  68. }
  69. } catch (RuntimeException $e) {
  70. if ($rootLevel < $level || ($rootLevel && !$level)) {
  71. unset($value[$k]);
  72. } elseif ($rootLevel) {
  73. throw $e;
  74. } else {
  75. $value[$k] = null;
  76. }
  77. }
  78. }
  79. // Ensure numerically indexed arguments have sequential numeric keys.
  80. if (false !== $i) {
  81. $value = array_values($value);
  82. }
  83. } elseif ($value instanceof Reference) {
  84. $id = (string) $value;
  85. if ($this->container->has($id)) {
  86. return $value;
  87. }
  88. $invalidBehavior = $value->getInvalidBehavior();
  89. // resolve invalid behavior
  90. if (ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
  91. $value = null;
  92. } elseif (ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) {
  93. if (0 < $level || $rootLevel) {
  94. throw $this->signalingException;
  95. }
  96. $value = null;
  97. }
  98. }
  99. return $value;
  100. }
  101. }