Skip to content

Commit d909485

Browse files
Merge branch '3.4' into 4.1
* 3.4: Think positive KernelInterface can return null container [DI] Detect circular references with ChildDefinition parent [VarDumper] Fix global dump function return value for PHP7 [Ldap] Use shut up operator on connection errors at ldap_start_tls Implement startTest rather than startTestSuite [OptionsResolver] remove dead code and useless else [HttpFoundation] don't override StreamedResponse::setNotModified() Added relevent links for parsing to the phpdoc Add stricter checking for valid date time string Fix symfony/console (optional) dependency for MonologBridge [Form] Fix DateTimeType html5 input format
2 parents ef44ed4 + cea44a5 commit d909485

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Compiler/ResolveChildDefinitionsPass.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Definition;
1616
use Symfony\Component\DependencyInjection\Exception\ExceptionInterface;
1717
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
18+
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
1819

1920
/**
2021
* This replaces all ChildDefinition instances with their equivalent fully
@@ -25,6 +26,8 @@
2526
*/
2627
class ResolveChildDefinitionsPass extends AbstractRecursivePass
2728
{
29+
private $currentPath;
30+
2831
protected function processValue($value, $isRoot = false)
2932
{
3033
if (!$value instanceof Definition) {
@@ -36,6 +39,7 @@ protected function processValue($value, $isRoot = false)
3639
$value = $this->container->getDefinition($this->currentId);
3740
}
3841
if ($value instanceof ChildDefinition) {
42+
$this->currentPath = array();
3943
$value = $this->resolveDefinition($value);
4044
if ($isRoot) {
4145
$this->container->setDefinition($this->currentId, $value);
@@ -56,6 +60,8 @@ private function resolveDefinition(ChildDefinition $definition)
5660
{
5761
try {
5862
return $this->doResolveDefinition($definition);
63+
} catch (ServiceCircularReferenceException $e) {
64+
throw $e;
5965
} catch (ExceptionInterface $e) {
6066
$r = new \ReflectionProperty($e, 'message');
6167
$r->setAccessible(true);
@@ -71,6 +77,13 @@ private function doResolveDefinition(ChildDefinition $definition)
7177
throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
7278
}
7379

80+
$searchKey = array_search($parent, $this->currentPath);
81+
$this->currentPath[] = $parent;
82+
83+
if (false !== $searchKey) {
84+
throw new ServiceCircularReferenceException($parent, \array_slice($this->currentPath, $searchKey));
85+
}
86+
7487
$parentDef = $this->container->findDefinition($parent);
7588
if ($parentDef instanceof ChildDefinition) {
7689
$id = $this->currentId;

Tests/Compiler/ResolveChildDefinitionsPassTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,21 @@ protected function process(ContainerBuilder $container)
396396
$pass = new ResolveChildDefinitionsPass();
397397
$pass->process($container);
398398
}
399+
400+
/**
401+
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
402+
* @expectedExceptionMessageRegExp /^Circular reference detected for service "c", path: "c -> b -> a -> c"./
403+
*/
404+
public function testProcessDetectsChildDefinitionIndirectCircularReference()
405+
{
406+
$container = new ContainerBuilder();
407+
408+
$container->register('a');
409+
410+
$container->setDefinition('b', new ChildDefinition('a'));
411+
$container->setDefinition('c', new ChildDefinition('b'));
412+
$container->setDefinition('a', new ChildDefinition('c'));
413+
414+
$this->process($container);
415+
}
399416
}

0 commit comments

Comments
 (0)