Drupal investigation

ResolveInvalidReferencesPass.php 3.4KB

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