Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CheckCircularReferencesPass implements CompilerPassInterface
{
private array $currentPath;
private array $checkedNodes;
private array $checkedLazyNodes;

/**
* Checks the ContainerBuilder object for circular references.
Expand Down Expand Up @@ -59,22 +60,36 @@ private function checkOutEdges(array $edges): void
$node = $edge->getDestNode();
$id = $node->getId();

if (empty($this->checkedNodes[$id])) {
// Don't check circular references for lazy edges
if (!$node->getValue() || (!$edge->isLazy() && !$edge->isWeak())) {
$searchKey = array_search($id, $this->currentPath);
$this->currentPath[] = $id;
if (!empty($this->checkedNodes[$id])) {
continue;
}

$isLeaf = !!$node->getValue();
$isConcrete = !$edge->isLazy() && !$edge->isWeak();

// Skip already checked lazy services if they are still lazy. Will not gain any new information.
if (!empty($this->checkedLazyNodes[$id]) && (!$isLeaf || !$isConcrete)) {
continue;
}

if (false !== $searchKey) {
throw new ServiceCircularReferenceException($id, \array_slice($this->currentPath, $searchKey));
}
// Process concrete references, otherwise defer check circular references for lazy edges.
if (!$isLeaf || $isConcrete) {
$searchKey = array_search($id, $this->currentPath);
$this->currentPath[] = $id;

$this->checkOutEdges($node->getOutEdges());
if (false !== $searchKey) {
throw new ServiceCircularReferenceException($id, \array_slice($this->currentPath, $searchKey));
}

$this->checkOutEdges($node->getOutEdges());

$this->checkedNodes[$id] = true;
array_pop($this->currentPath);
unset($this->checkedLazyNodes[$id]);
} else {
$this->checkedLazyNodes[$id] = true;
}

array_pop($this->currentPath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
use Symfony\Component\DependencyInjection\Compiler\CheckCircularReferencesPass;
use Symfony\Component\DependencyInjection\Compiler\Compiler;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -126,6 +129,21 @@ public function testProcessIgnoresLazyServices()
$this->addToAssertionCount(1);
}

public function testProcessDefersLazyServices()
{
$container = new ContainerBuilder();

$container->register('a')->addArgument(new ServiceLocatorArgument(new TaggedIteratorArgument('tag', needsIndexes: true)));
$container->register('b')->addArgument(new Reference('c'))->addTag('tag');
$container->register('c')->addArgument(new Reference('b'));

(new ServiceLocatorTagPass())->process($container);

$this->expectException(ServiceCircularReferenceException::class);

$this->process($container);
}

public function testProcessIgnoresIteratorArguments()
{
$container = new ContainerBuilder();
Expand Down
Loading