Skip to content

[DI] Fix false-positive circular exception #25247

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

Merged
merged 1 commit into from
Dec 1, 2017
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 @@ -478,7 +478,7 @@ private function addServiceInlinedDefinitions($id, Definition $definition, \SplO
// $b = new ServiceB();
// $a = new ServiceA(ServiceB $b);
// $b->setServiceA(ServiceA $a);
if ($this->hasReference($id, array($def->getArguments(), $def->getFactory()))) {
if (isset($inlinedDefinition[$definition]) && $this->hasReference($id, array($def->getArguments(), $def->getFactory()))) {
throw new ServiceCircularReferenceException($id, array($id));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,9 @@ public function testAlmostCircular($visibility)
$this->assertSame($foo2, $foo2->bar->foobar->foo);

$this->assertSame(array(), (array) $container->get('foobar4'));

$foo5 = $container->get('foo5');
$this->assertSame($foo5, $foo5->bar->foo);
}

public function provideAlmostCircular()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,9 @@ public function testAlmostCircular($visibility)
$this->assertSame($foo2, $foo2->bar->foobar->foo);

$this->assertSame(array(), (array) $container->get('foobar4'));

$foo5 = $container->get('foo5');
$this->assertSame($foo5, $foo5->bar->foo);
}

public function provideAlmostCircular()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@
$container->register('foobar4', 'stdClass')->setPublic(true)
->addArgument(new Reference('foo4'));

// loop on the constructor of a setter-injected dep with property

$container->register('foo5', 'stdClass')->setPublic(true)
->setProperty('bar', new Reference('bar5'));

$container->register('bar5', 'stdClass')->setPublic($public)
->addArgument(new Reference('foo5'))
->setProperty('foo', new Reference('foo5'));

return $container;
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct()
'bar3' => 'getBar3Service',
'foo' => 'getFooService',
'foo2' => 'getFoo2Service',
'foo5' => 'getFoo5Service',
'foobar4' => 'getFoobar4Service',
);

Expand All @@ -39,6 +40,7 @@ public function getRemovedIds()
'Psr\\Container\\ContainerInterface' => true,
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
'bar' => true,
'bar5' => true,
'foo4' => true,
'foobar' => true,
'foobar2' => true,
Expand Down Expand Up @@ -125,6 +127,24 @@ protected function getFoo2Service()
return $this->services['foo2'] = new \FooCircular($a);
}

/**
* Gets the public 'foo5' shared service.
*
* @return \stdClass
*/
protected function getFoo5Service()
{
$this->services['foo5'] = $instance = new \stdClass();

$a = new \stdClass(${($_ = isset($this->services['foo5']) ? $this->services['foo5'] : $this->getFoo5Service()) && false ?: '_'});

$a->foo = $instance;

$instance->bar = $a;

return $instance;
}

/**
* Gets the public 'foobar4' shared service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public function __construct()
$this->methodMap = array(
'bar' => 'getBarService',
'bar3' => 'getBar3Service',
'bar5' => 'getBar5Service',
'foo' => 'getFooService',
'foo2' => 'getFoo2Service',
'foo4' => 'getFoo4Service',
'foo5' => 'getFoo5Service',
'foobar' => 'getFoobarService',
'foobar2' => 'getFoobar2Service',
'foobar3' => 'getFoobar3Service',
Expand Down Expand Up @@ -93,6 +95,26 @@ protected function getBar3Service()
return $instance;
}

/**
* Gets the public 'bar5' shared service.
*
* @return \stdClass
*/
protected function getBar5Service()
{
$a = ${($_ = isset($this->services['foo5']) ? $this->services['foo5'] : $this->getFoo5Service()) && false ?: '_'};

if (isset($this->services['bar5'])) {
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this come first, before defining the $a variable ?

Copy link
Member

Choose a reason for hiding this comment

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

ah no, understand. This is the call to $this->getFoo5Service() which can register it in $this->services

return $this->services['bar5'];
}

$this->services['bar5'] = $instance = new \stdClass($a);

$instance->foo = $a;

return $instance;
}

/**
* Gets the public 'foo' shared service.
*
Expand Down Expand Up @@ -139,6 +161,20 @@ protected function getFoo4Service()
return $instance;
}

/**
* Gets the public 'foo5' shared service.
*
* @return \stdClass
*/
protected function getFoo5Service()
{
$this->services['foo5'] = $instance = new \stdClass();

$instance->bar = ${($_ = isset($this->services['bar5']) ? $this->services['bar5'] : $this->getBar5Service()) && false ?: '_'};

return $instance;
}

/**
* Gets the public 'foobar' shared service.
*
Expand Down