annotate vendor/symfony/dependency-injection/Compiler/ResolveInvalidReferencesPass.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\DependencyInjection\Compiler;
Chris@0 13
Chris@14 14 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
Chris@14 15 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
Chris@17 16 use Symfony\Component\DependencyInjection\ContainerBuilder;
Chris@0 17 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@14 18 use Symfony\Component\DependencyInjection\Definition;
Chris@17 19 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
Chris@0 20 use Symfony\Component\DependencyInjection\Reference;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Emulates the invalid behavior if the reference is not found within the
Chris@0 24 * container.
Chris@0 25 *
Chris@0 26 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
Chris@0 27 */
Chris@0 28 class ResolveInvalidReferencesPass implements CompilerPassInterface
Chris@0 29 {
Chris@0 30 private $container;
Chris@14 31 private $signalingException;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Process the ContainerBuilder to resolve invalid references.
Chris@0 35 */
Chris@0 36 public function process(ContainerBuilder $container)
Chris@0 37 {
Chris@0 38 $this->container = $container;
Chris@14 39 $this->signalingException = new RuntimeException('Invalid reference.');
Chris@0 40
Chris@14 41 try {
Chris@14 42 $this->processValue($container->getDefinitions(), 1);
Chris@14 43 } finally {
Chris@14 44 $this->container = $this->signalingException = null;
Chris@0 45 }
Chris@0 46 }
Chris@0 47
Chris@0 48 /**
Chris@0 49 * Processes arguments to determine invalid references.
Chris@0 50 *
Chris@14 51 * @throws RuntimeException When an invalid reference is found
Chris@0 52 */
Chris@14 53 private function processValue($value, $rootLevel = 0, $level = 0)
Chris@0 54 {
Chris@14 55 if ($value instanceof ServiceClosureArgument) {
Chris@14 56 $value->setValues($this->processValue($value->getValues(), 1, 1));
Chris@14 57 } elseif ($value instanceof ArgumentInterface) {
Chris@14 58 $value->setValues($this->processValue($value->getValues(), $rootLevel, 1 + $level));
Chris@14 59 } elseif ($value instanceof Definition) {
Chris@14 60 if ($value->isSynthetic() || $value->isAbstract()) {
Chris@14 61 return $value;
Chris@14 62 }
Chris@14 63 $value->setArguments($this->processValue($value->getArguments(), 0));
Chris@14 64 $value->setProperties($this->processValue($value->getProperties(), 1));
Chris@14 65 $value->setMethodCalls($this->processValue($value->getMethodCalls(), 2));
Chris@17 66 } elseif (\is_array($value)) {
Chris@14 67 $i = 0;
Chris@0 68
Chris@14 69 foreach ($value as $k => $v) {
Chris@14 70 try {
Chris@14 71 if (false !== $i && $k !== $i++) {
Chris@14 72 $i = false;
Chris@14 73 }
Chris@14 74 if ($v !== $processedValue = $this->processValue($v, $rootLevel, 1 + $level)) {
Chris@14 75 $value[$k] = $processedValue;
Chris@14 76 }
Chris@14 77 } catch (RuntimeException $e) {
Chris@14 78 if ($rootLevel < $level || ($rootLevel && !$level)) {
Chris@14 79 unset($value[$k]);
Chris@14 80 } elseif ($rootLevel) {
Chris@14 81 throw $e;
Chris@14 82 } else {
Chris@14 83 $value[$k] = null;
Chris@14 84 }
Chris@14 85 }
Chris@14 86 }
Chris@0 87
Chris@14 88 // Ensure numerically indexed arguments have sequential numeric keys.
Chris@14 89 if (false !== $i) {
Chris@14 90 $value = array_values($value);
Chris@14 91 }
Chris@14 92 } elseif ($value instanceof Reference) {
Chris@14 93 if ($this->container->has($value)) {
Chris@14 94 return $value;
Chris@14 95 }
Chris@14 96 $invalidBehavior = $value->getInvalidBehavior();
Chris@0 97
Chris@14 98 // resolve invalid behavior
Chris@14 99 if (ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
Chris@14 100 $value = null;
Chris@14 101 } elseif (ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) {
Chris@14 102 if (0 < $level || $rootLevel) {
Chris@14 103 throw $this->signalingException;
Chris@0 104 }
Chris@14 105 $value = null;
Chris@0 106 }
Chris@0 107 }
Chris@0 108
Chris@14 109 return $value;
Chris@0 110 }
Chris@0 111 }