Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DependencyInjection\Compiler; Chris@0: Chris@14: use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; Chris@14: use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; Chris@17: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@0: use Symfony\Component\DependencyInjection\ContainerInterface; Chris@14: use Symfony\Component\DependencyInjection\Definition; Chris@17: use Symfony\Component\DependencyInjection\Exception\RuntimeException; Chris@0: use Symfony\Component\DependencyInjection\Reference; Chris@0: Chris@0: /** Chris@0: * Emulates the invalid behavior if the reference is not found within the Chris@0: * container. Chris@0: * Chris@0: * @author Johannes M. Schmitt Chris@0: */ Chris@0: class ResolveInvalidReferencesPass implements CompilerPassInterface Chris@0: { Chris@0: private $container; Chris@14: private $signalingException; Chris@0: Chris@0: /** Chris@0: * Process the ContainerBuilder to resolve invalid references. Chris@0: */ Chris@0: public function process(ContainerBuilder $container) Chris@0: { Chris@0: $this->container = $container; Chris@14: $this->signalingException = new RuntimeException('Invalid reference.'); Chris@0: Chris@14: try { Chris@14: $this->processValue($container->getDefinitions(), 1); Chris@14: } finally { Chris@14: $this->container = $this->signalingException = null; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Processes arguments to determine invalid references. Chris@0: * Chris@14: * @throws RuntimeException When an invalid reference is found Chris@0: */ Chris@14: private function processValue($value, $rootLevel = 0, $level = 0) Chris@0: { Chris@14: if ($value instanceof ServiceClosureArgument) { Chris@14: $value->setValues($this->processValue($value->getValues(), 1, 1)); Chris@14: } elseif ($value instanceof ArgumentInterface) { Chris@14: $value->setValues($this->processValue($value->getValues(), $rootLevel, 1 + $level)); Chris@14: } elseif ($value instanceof Definition) { Chris@14: if ($value->isSynthetic() || $value->isAbstract()) { Chris@14: return $value; Chris@14: } Chris@14: $value->setArguments($this->processValue($value->getArguments(), 0)); Chris@14: $value->setProperties($this->processValue($value->getProperties(), 1)); Chris@14: $value->setMethodCalls($this->processValue($value->getMethodCalls(), 2)); Chris@17: } elseif (\is_array($value)) { Chris@14: $i = 0; Chris@0: Chris@14: foreach ($value as $k => $v) { Chris@14: try { Chris@14: if (false !== $i && $k !== $i++) { Chris@14: $i = false; Chris@14: } Chris@14: if ($v !== $processedValue = $this->processValue($v, $rootLevel, 1 + $level)) { Chris@14: $value[$k] = $processedValue; Chris@14: } Chris@14: } catch (RuntimeException $e) { Chris@14: if ($rootLevel < $level || ($rootLevel && !$level)) { Chris@14: unset($value[$k]); Chris@14: } elseif ($rootLevel) { Chris@14: throw $e; Chris@14: } else { Chris@14: $value[$k] = null; Chris@14: } Chris@14: } Chris@14: } Chris@0: Chris@14: // Ensure numerically indexed arguments have sequential numeric keys. Chris@14: if (false !== $i) { Chris@14: $value = array_values($value); Chris@14: } Chris@14: } elseif ($value instanceof Reference) { Chris@14: if ($this->container->has($value)) { Chris@14: return $value; Chris@14: } Chris@14: $invalidBehavior = $value->getInvalidBehavior(); Chris@0: Chris@14: // resolve invalid behavior Chris@14: if (ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) { Chris@14: $value = null; Chris@14: } elseif (ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) { Chris@14: if (0 < $level || $rootLevel) { Chris@14: throw $this->signalingException; Chris@0: } Chris@14: $value = null; Chris@0: } Chris@0: } Chris@0: Chris@14: return $value; Chris@0: } Chris@0: }