ResolveInvalidReferencesPass.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\Reference;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. /**
  15. * Emulates the invalid behavior if the reference is not found within the
  16. * container.
  17. *
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. class ResolveInvalidReferencesPass implements CompilerPassInterface
  21. {
  22. private $container;
  23. /**
  24. * Process the ContainerBuilder to resolve invalid 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(
  36. $this->processArguments($definition->getArguments())
  37. );
  38. $calls = array();
  39. foreach ($definition->getMethodCalls() as $call) {
  40. try {
  41. $calls[] = array($call[0], $this->processArguments($call[1], true));
  42. } catch (\RuntimeException $ignore) {
  43. // this call is simply removed
  44. }
  45. }
  46. $definition->setMethodCalls($calls);
  47. $properties = array();
  48. foreach ($definition->getProperties() as $name => $value) {
  49. try {
  50. $value = $this->processArguments(array($value), true);
  51. $properties[$name] = reset($value);
  52. } catch (\RuntimeException $ignore) {
  53. // ignore property
  54. }
  55. }
  56. $definition->setProperties($properties);
  57. }
  58. }
  59. /**
  60. * Processes arguments to determine invalid references.
  61. *
  62. * @param array $arguments An array of Reference objects
  63. * @param Boolean $inMethodCall
  64. *
  65. * @return array
  66. *
  67. * @throws \RuntimeException When the config is invalid
  68. */
  69. private function processArguments(array $arguments, $inMethodCall = false)
  70. {
  71. foreach ($arguments as $k => $argument) {
  72. if (is_array($argument)) {
  73. $arguments[$k] = $this->processArguments($argument, $inMethodCall);
  74. } elseif ($argument instanceof Reference) {
  75. $id = (string) $argument;
  76. $invalidBehavior = $argument->getInvalidBehavior();
  77. $exists = $this->container->has($id);
  78. // resolve invalid behavior
  79. if ($exists && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
  80. $arguments[$k] = new Reference($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $argument->isStrict());
  81. } elseif (!$exists && ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
  82. $arguments[$k] = null;
  83. } elseif (!$exists && ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) {
  84. if ($inMethodCall) {
  85. throw new \RuntimeException('Method shouldn\'t be called.');
  86. }
  87. $arguments[$k] = null;
  88. }
  89. }
  90. }
  91. return $arguments;
  92. }
  93. }