Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DI] Failing test case for complex near-circular situation + lazy #32157

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected function processValue($value, $isRoot = false)
$this->lazy = false;

$byConstructor = $this->byConstructor;
$this->byConstructor = true;
$this->byConstructor = $isRoot || $byConstructor;
$this->processValue($value->getFactory());
$this->processValue($value->getArguments());
$this->byConstructor = $byConstructor;
Expand Down
113 changes: 72 additions & 41 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public function dump(array $options = [])
}
$this->container->getCompiler()->getServiceReferenceGraph()->clear();
$checkedNodes = [];
$this->singleUsePrivateIds = array_diff_key($this->singleUsePrivateIds, $this->circularReferences);

$this->docStar = $options['debug'] ? '*' : '';

Expand Down Expand Up @@ -338,10 +339,10 @@ private function getProxyDumper(): ProxyDumper
return $this->proxyDumper;
}

private function analyzeCircularReferences($sourceId, array $edges, &$checkedNodes, &$currentPath = [])
private function analyzeCircularReferences($sourceId, array $edges, &$checkedNodes, &$currentPath = [], $byConstructor = true)
{
$checkedNodes[$sourceId] = true;
$currentPath[$sourceId] = $sourceId;
$currentPath[$sourceId] = $byConstructor;

foreach ($edges as $edge) {
$node = $edge->getDestNode();
Expand All @@ -350,44 +351,52 @@ private function analyzeCircularReferences($sourceId, array $edges, &$checkedNod
if (!$node->getValue() instanceof Definition || $sourceId === $id || $edge->isLazy() || $edge->isWeak()) {
// no-op
} elseif (isset($currentPath[$id])) {
$currentId = $id;
foreach (array_reverse($currentPath) as $parentId) {
$this->circularReferences[$parentId][$currentId] = $currentId;
if ($parentId === $id) {
break;
}
$currentId = $parentId;
}
$this->addCircularReferences($id, $currentPath, $edge->isReferencedByConstructor());
} elseif (!isset($checkedNodes[$id])) {
$this->analyzeCircularReferences($id, $node->getOutEdges(), $checkedNodes, $currentPath);
$this->analyzeCircularReferences($id, $node->getOutEdges(), $checkedNodes, $currentPath, $edge->isReferencedByConstructor());
} elseif (isset($this->circularReferences[$id])) {
$this->connectCircularReferences($id, $currentPath);
$this->connectCircularReferences($id, $currentPath, $edge->isReferencedByConstructor());
}
}
unset($currentPath[$sourceId]);
}

private function connectCircularReferences($sourceId, &$currentPath, &$subPath = [])
private function connectCircularReferences($sourceId, &$currentPath, $byConstructor, &$subPath = [])
{
$subPath[$sourceId] = $sourceId;
$currentPath[$sourceId] = $sourceId;
$currentPath[$sourceId] = $subPath[$sourceId] = $byConstructor;

foreach ($this->circularReferences[$sourceId] as $id) {
foreach ($this->circularReferences[$sourceId] as $id => $byConstructor) {
if (isset($currentPath[$id])) {
$currentId = $id;
foreach (array_reverse($currentPath) as $parentId) {
$this->circularReferences[$parentId][$currentId] = $currentId;
if ($parentId === $id) {
break;
}
$currentId = $parentId;
}
$this->addCircularReferences($id, $currentPath, $byConstructor);
} elseif (!isset($subPath[$id]) && isset($this->circularReferences[$id])) {
$this->connectCircularReferences($id, $currentPath, $subPath);
$this->connectCircularReferences($id, $currentPath, $byConstructor, $subPath);
}
}
unset($currentPath[$sourceId]);
unset($subPath[$sourceId]);
unset($currentPath[$sourceId], $subPath[$sourceId]);
}

private function addCircularReferences($id, $currentPath, $byConstructor)
{
$currentPath[$id] = $byConstructor;
$circularRefs = [];

foreach (array_reverse($currentPath) as $parentId => $v) {
$byConstructor = $byConstructor && $v;
$circularRefs[] = $parentId;

if ($parentId === $id) {
break;
}
}

$currentId = $id;
foreach ($circularRefs as $parentId) {
if (empty($this->circularReferences[$parentId][$currentId])) {
$this->circularReferences[$parentId][$currentId] = $byConstructor;
}

$currentId = $parentId;
}
}

private function collectLineage($class, array &$lineage)
Expand Down Expand Up @@ -655,7 +664,6 @@ private function addService(string $id, Definition $definition): array
$autowired = $definition->isAutowired() ? ' autowired' : '';

if ($definition->isLazy()) {
unset($this->circularReferences[$id]);
$lazyInitialization = '$lazyLoad = true';
} else {
$lazyInitialization = '';
Expand Down Expand Up @@ -731,12 +739,12 @@ private function addInlineVariables(string $id, Definition $definition, array $a

private function addInlineReference(string $id, Definition $definition, string $targetId, bool $forConstructor): string
{
list($callCount, $behavior) = $this->serviceCalls[$targetId];

while ($this->container->hasAlias($targetId)) {
$targetId = (string) $this->container->getAlias($targetId);
}

list($callCount, $behavior) = $this->serviceCalls[$targetId];

if ($id === $targetId) {
return $this->addInlineService($id, $definition, $definition);
}
Expand All @@ -745,9 +753,13 @@ private function addInlineReference(string $id, Definition $definition, string $
return '';
}

$hasSelfRef = isset($this->circularReferences[$id][$targetId]);
$forConstructor = $forConstructor && !isset($this->definitionVariables[$definition]);
$code = $hasSelfRef && !$forConstructor ? $this->addInlineService($id, $definition, $definition) : '';
$hasSelfRef = isset($this->circularReferences[$id][$targetId]) && !isset($this->definitionVariables[$definition]);

if ($hasSelfRef && !$forConstructor && !$forConstructor = !$this->circularReferences[$id][$targetId]) {
$code = $this->addInlineService($id, $definition, $definition);
} else {
$code = '';
}

if (isset($this->referenceVariables[$targetId]) || (2 > $callCount && (!$hasSelfRef || !$forConstructor))) {
return $code;
Expand Down Expand Up @@ -780,15 +792,23 @@ private function addInlineReference(string $id, Definition $definition, string $

private function addInlineService(string $id, Definition $definition, Definition $inlineDef = null, bool $forConstructor = true): string
{
$isSimpleInstance = $isRootInstance = null === $inlineDef;
$code = '';

if ($isSimpleInstance = $isRootInstance = null === $inlineDef) {
foreach ($this->serviceCalls as $targetId => list($callCount, $behavior, $byConstructor)) {
if ($byConstructor && isset($this->circularReferences[$id][$targetId]) && !$this->circularReferences[$id][$targetId]) {
$code .= $this->addInlineReference($id, $definition, $targetId, $forConstructor);
}
}
}

if (isset($this->definitionVariables[$inlineDef = $inlineDef ?: $definition])) {
return '';
return $code;
}

$arguments = [$inlineDef->getArguments(), $inlineDef->getFactory()];

$code = $this->addInlineVariables($id, $definition, $arguments, $forConstructor);
$code .= $this->addInlineVariables($id, $definition, $arguments, $forConstructor);

if ($arguments = array_filter([$inlineDef->getProperties(), $inlineDef->getMethodCalls(), $inlineDef->getConfigurator()])) {
$isSimpleInstance = false;
Expand Down Expand Up @@ -1403,20 +1423,24 @@ private function getServiceConditionals($value): string
return implode(' && ', $conditions);
}

private function getDefinitionsFromArguments(array $arguments, \SplObjectStorage $definitions = null, array &$calls = []): \SplObjectStorage
private function getDefinitionsFromArguments(array $arguments, \SplObjectStorage $definitions = null, array &$calls = [], $byConstructor = null): \SplObjectStorage
{
if (null === $definitions) {
$definitions = new \SplObjectStorage();
}

foreach ($arguments as $argument) {
if (\is_array($argument)) {
$this->getDefinitionsFromArguments($argument, $definitions, $calls);
$this->getDefinitionsFromArguments($argument, $definitions, $calls, $byConstructor);
} elseif ($argument instanceof Reference) {
$id = (string) $argument;

while ($this->container->hasAlias($id)) {
$id = (string) $this->container->getAlias($id);
}

if (!isset($calls[$id])) {
$calls[$id] = [0, $argument->getInvalidBehavior()];
$calls[$id] = [0, $argument->getInvalidBehavior(), $byConstructor];
} else {
$calls[$id][1] = min($calls[$id][1], $argument->getInvalidBehavior());
}
Expand All @@ -1428,8 +1452,10 @@ private function getDefinitionsFromArguments(array $arguments, \SplObjectStorage
$definitions[$argument] = 1 + $definitions[$argument];
} else {
$definitions[$argument] = 1;
$arguments = [$argument->getArguments(), $argument->getFactory(), $argument->getProperties(), $argument->getMethodCalls(), $argument->getConfigurator()];
$this->getDefinitionsFromArguments($arguments, $definitions, $calls);
$arguments = [$argument->getArguments(), $argument->getFactory()];
$this->getDefinitionsFromArguments($arguments, $definitions, $calls, null === $byConstructor || $byConstructor);
$arguments = [$argument->getProperties(), $argument->getMethodCalls(), $argument->getConfigurator()];
$this->getDefinitionsFromArguments($arguments, $definitions, $calls, null !== $byConstructor && $byConstructor);
}
}

Expand Down Expand Up @@ -1548,6 +1574,11 @@ private function dumpValue($value, bool $interpolate = true): string
return '$'.$value;
} elseif ($value instanceof Reference) {
$id = (string) $value;

while ($this->container->hasAlias($id)) {
$id = (string) $this->container->getAlias($id);
}

if (null !== $this->referenceVariables && isset($this->referenceVariables[$id])) {
return $this->dumpValue($this->referenceVariables[$id], $interpolate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,13 @@ public function testAlmostCircular($visibility)
$this->assertEquals((object) ['bar6' => (object) []], $foo6);

$this->assertInstanceOf(\stdClass::class, $container->get('root'));

$manager3 = $container->get('manager3');
$listener3 = $container->get('listener3');
$this->assertSame($manager3, $listener3->manager, 'Both should identically be the manager3 service');

$listener4 = $container->get('listener4');
$this->assertInstanceOf('stdClass', $listener4);
}

public function provideAlmostCircular()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,13 @@ public function testAlmostCircular($visibility)
$this->assertEquals((object) ['bar6' => (object) []], $foo6);

$this->assertInstanceOf(\stdClass::class, $container->get('root'));

$manager3 = $container->get('manager3');
$listener3 = $container->get('listener3');
$this->assertSame($manager3, $listener3->manager);

$listener4 = $container->get('listener4');
$this->assertInstanceOf('stdClass', $listener4);
}

public function provideAlmostCircular()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Tests\Fixtures\FooForCircularWithAddCalls;

Expand Down Expand Up @@ -102,6 +101,35 @@
$container->register('subscriber2', 'stdClass')->setPublic(false)
->addArgument(new Reference('manager2'));

// doctrine-like event system with listener

$container->register('manager3', 'stdClass')
->setLazy(true)
->setPublic(true)
->addArgument(new Reference('connection3'));

$container->register('connection3', 'stdClass')
->setPublic($public)
->setProperty('listener', [new Reference('listener3')]);

$container->register('listener3', 'stdClass')
->setPublic(true)
->setProperty('manager', new Reference('manager3'));

// doctrine-like event system with small differences

$container->register('manager4', 'stdClass')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a second case that's actually unrelated, but I stumbled upon it. For some reason, if connection4 is NOT public, you get a nesting exception.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be worth to add this explanation as a comment code ?

->setLazy(true)
->addArgument(new Reference('connection4'));

$container->register('connection4', 'stdClass')
->setPublic($public)
->setProperty('listener', [new Reference('listener4')]);

$container->register('listener4', 'stdClass')
->setPublic(true)
->addArgument(new Reference('manager4'));

// private service involved in a loop

$container->register('foo6', 'stdClass')
Expand Down