2011-01-05 11:13:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-05 11:13:27 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-05 11:13:27 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2011-01-05 11:13:27 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
|
|
|
|
2016-12-13 17:37:51 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
|
2017-02-26 17:31:03 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
2017-01-17 20:04:53 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Definition;
|
2011-01-15 13:29:43 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Reference;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
2011-12-04 23:51:22 +00:00
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
2011-01-15 13:29:43 +00:00
|
|
|
|
2011-01-05 11:13:27 +00:00
|
|
|
/**
|
|
|
|
* Emulates the invalid behavior if the reference is not found within the
|
|
|
|
* container.
|
|
|
|
*
|
|
|
|
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
|
|
|
*/
|
|
|
|
class ResolveInvalidReferencesPass implements CompilerPassInterface
|
|
|
|
{
|
2011-03-11 13:50:46 +00:00
|
|
|
private $container;
|
2017-01-17 20:04:53 +00:00
|
|
|
private $signalingException;
|
2011-01-05 11:13:27 +00:00
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Process the ContainerBuilder to resolve invalid references.
|
|
|
|
*
|
2011-03-04 14:26:00 +00:00
|
|
|
* @param ContainerBuilder $container
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2011-01-05 11:13:27 +00:00
|
|
|
public function process(ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
2017-01-17 20:04:53 +00:00
|
|
|
$this->signalingException = new RuntimeException('Invalid reference.');
|
2011-03-04 14:26:00 +00:00
|
|
|
|
2017-01-17 20:04:53 +00:00
|
|
|
try {
|
|
|
|
$this->processValue($container->getDefinitions(), 1);
|
|
|
|
} finally {
|
|
|
|
$this->container = $this->signalingException = null;
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
2011-03-01 17:48:35 +00:00
|
|
|
* Processes arguments to determine invalid references.
|
2011-02-13 18:06:41 +00:00
|
|
|
*
|
2017-01-17 20:04:53 +00:00
|
|
|
* @throws RuntimeException When an invalid reference is found
|
2011-02-13 18:06:41 +00:00
|
|
|
*/
|
2017-01-17 20:04:53 +00:00
|
|
|
private function processValue($value, $rootLevel = 0, $level = 0)
|
2011-01-05 11:13:27 +00:00
|
|
|
{
|
2017-02-26 17:31:03 +00:00
|
|
|
if ($value instanceof ServiceClosureArgument) {
|
|
|
|
$value->setValues($this->processValue($value->getValues(), 1, 1));
|
|
|
|
} elseif ($value instanceof ArgumentInterface) {
|
2017-02-26 17:31:03 +00:00
|
|
|
$value->setValues($this->processValue($value->getValues(), $rootLevel, 1 + $level));
|
|
|
|
} elseif ($value instanceof Definition) {
|
2017-01-17 20:04:53 +00:00
|
|
|
if ($value->isSynthetic() || $value->isAbstract()) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
$value->setArguments($this->processValue($value->getArguments(), 0));
|
|
|
|
$value->setProperties($this->processValue($value->getProperties(), 1));
|
|
|
|
$value->setMethodCalls($this->processValue($value->getMethodCalls(), 2));
|
|
|
|
} elseif (is_array($value)) {
|
|
|
|
$i = 0;
|
2011-01-05 11:13:27 +00:00
|
|
|
|
2017-01-17 20:04:53 +00:00
|
|
|
foreach ($value as $k => $v) {
|
|
|
|
try {
|
|
|
|
if (false !== $i && $k !== $i++) {
|
|
|
|
$i = false;
|
2015-12-24 13:13:22 +00:00
|
|
|
}
|
2017-01-17 20:04:53 +00:00
|
|
|
if ($v !== $processedValue = $this->processValue($v, $rootLevel, 1 + $level)) {
|
|
|
|
$value[$k] = $processedValue;
|
|
|
|
}
|
|
|
|
} catch (RuntimeException $e) {
|
|
|
|
if ($rootLevel < $level || ($rootLevel && !$level)) {
|
|
|
|
unset($value[$k]);
|
|
|
|
} elseif ($rootLevel) {
|
|
|
|
throw $e;
|
|
|
|
} else {
|
|
|
|
$value[$k] = null;
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 20:04:53 +00:00
|
|
|
// Ensure numerically indexed arguments have sequential numeric keys.
|
|
|
|
if (false !== $i) {
|
|
|
|
$value = array_values($value);
|
|
|
|
}
|
|
|
|
} elseif ($value instanceof Reference) {
|
|
|
|
$id = (string) $value;
|
|
|
|
|
|
|
|
if ($this->container->has($id)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
$invalidBehavior = $value->getInvalidBehavior();
|
|
|
|
|
|
|
|
// resolve invalid behavior
|
|
|
|
if (ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
|
|
|
|
$value = null;
|
|
|
|
} elseif (ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) {
|
|
|
|
if (0 < $level || $rootLevel) {
|
|
|
|
throw $this->signalingException;
|
|
|
|
}
|
|
|
|
$value = null;
|
|
|
|
}
|
2015-12-24 13:13:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 20:04:53 +00:00
|
|
|
return $value;
|
2011-01-05 11:13:27 +00:00
|
|
|
}
|
2011-04-12 09:07:46 +01:00
|
|
|
}
|