Skip to content

[DI] Fix the "almost-circular refs" fix #24828

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
Nov 5, 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
34 changes: 21 additions & 13 deletions src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1490,29 +1490,37 @@ private function hasReference($id, array $arguments, $deep = false, array &$visi
if ($this->hasReference($id, $argument, $deep, $visited)) {
return true;
}

continue;
} elseif ($argument instanceof Reference) {
$argumentId = (string) $argument;
if ($id === $argumentId) {
return true;
}

if ($deep && !isset($visited[$argumentId]) && 'service_container' !== $argumentId) {
$visited[$argumentId] = true;
if (!$deep || isset($visited[$argumentId]) || 'service_container' === $argumentId) {
continue;
}

$service = $this->container->getDefinition($argumentId);
$visited[$argumentId] = true;

// if the proxy manager is enabled, disable searching for references in lazy services,
// as these services will be instantiated lazily and don't have direct related references.
if ($service->isLazy() && !$this->getProxyDumper() instanceof NullDumper) {
continue;
}
$service = $this->container->getDefinition($argumentId);
} elseif ($argument instanceof Definition) {
$service = $argument;
} else {
continue;
}

$arguments = array_merge($service->getMethodCalls(), $service->getArguments(), $service->getProperties());
// if the proxy manager is enabled, disable searching for references in lazy services,
// as these services will be instantiated lazily and don't have direct related references.
if ($service->isLazy() && !$this->getProxyDumper() instanceof NullDumper) {
continue;
}

if ($this->hasReference($id, $arguments, $deep, $visited)) {
return true;
}
}
$arguments = array_merge($service->getMethodCalls(), $service->getArguments(), $service->getProperties());

if ($this->hasReference($id, $arguments, $deep, $visited)) {
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1203,8 +1203,19 @@ public function testUninitializedReference()
$this->assertEquals(array('foo1' => new \stdClass(), 'foo3' => new \stdClass()), iterator_to_array($bar->iter));
}

public function testAlmostCircular()
public function testAlmostCircularPrivate()
{
$public = false;
$container = include __DIR__.'/Fixtures/containers/container_almost_circular.php';

$foo = $container->get('foo');

$this->assertSame($foo, $foo->bar->foobar->foo);
}

public function testAlmostCircularPublic()
{
$public = true;
$container = include __DIR__.'/Fixtures/containers/container_almost_circular.php';

$foo = $container->get('foo');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,17 +764,35 @@ public function testUninitializedReference()
$this->assertEquals(array('foo1' => new \stdClass(), 'foo3' => new \stdClass()), iterator_to_array($bar->iter));
}

public function testAlmostCircular()
public function testAlmostCircularPrivate()
{
$public = false;
$container = include self::$fixturesPath.'/containers/container_almost_circular.php';
$container->compile();
$dumper = new PhpDumper($container);

$this->assertStringEqualsFile(self::$fixturesPath.'/php/container_almost_circular.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Almost_Circular')));
$this->assertStringEqualsFile(self::$fixturesPath.'/php/container_almost_circular_private.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Almost_Circular_Private')));

require self::$fixturesPath.'/php/container_almost_circular.php';
require self::$fixturesPath.'/php/container_almost_circular_private.php';

$container = new \Symfony_DI_PhpDumper_Test_Almost_Circular();
$container = new \Symfony_DI_PhpDumper_Test_Almost_Circular_Private();
$foo = $container->get('foo');

$this->assertSame($foo, $foo->bar->foobar->foo);
}

public function testAlmostCircularPublic()
{
$public = true;
$container = include self::$fixturesPath.'/containers/container_almost_circular.php';
$container->compile();
$dumper = new PhpDumper($container);

$this->assertStringEqualsFile(self::$fixturesPath.'/php/container_almost_circular_public.php', $dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Almost_Circular_Public')));

require self::$fixturesPath.'/php/container_almost_circular_public.php';

$container = new \Symfony_DI_PhpDumper_Test_Almost_Circular_Public();
$foo = $container->get('foo');

$this->assertSame($foo, $foo->bar->foobar->foo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
$container->register('foo', FooCircular::class)->setPublic(true)
->addArgument(new Reference('bar'));

$container->register('bar', BarCircular::class)
$container->register('bar', BarCircular::class)->setPublic($public)
->addMethodCall('addFoobar', array(new Reference('foobar')));

$container->register('foobar', FoobarCircular::class)
$container->register('foobar', FoobarCircular::class)->setPublic($public)
->addArgument(new Reference('foo'));

return $container;
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

/**
* This class has been auto-generated
* by the Symfony Dependency Injection Component.
*
* @final since Symfony 3.3
*/
class Symfony_DI_PhpDumper_Test_Almost_Circular_Private extends Container
{
private $parameters;
private $targetDirs = array();

public function __construct()
{
$this->services = array();
$this->methodMap = array(
'foo' => 'getFooService',
);

$this->aliases = array();
}

public function getRemovedIds()
{
return array(
'Psr\\Container\\ContainerInterface' => true,
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
'bar' => true,
'foobar' => true,
);
}

public function compile()
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}

public function isCompiled()
{
return true;
}

public function isFrozen()
{
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);

return true;
}

/**
* Gets the public 'foo' shared service.
*
* @return \FooCircular
*/
protected function getFooService()
{
$a = new \BarCircular();

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

$a->addFoobar(new \FoobarCircular($instance));


return $instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @final since Symfony 3.3
*/
class Symfony_DI_PhpDumper_Test_Almost_Circular extends Container
class Symfony_DI_PhpDumper_Test_Almost_Circular_Public extends Container
{
private $parameters;
private $targetDirs = array();
Expand All @@ -27,10 +27,6 @@ public function __construct()
'foo' => 'getFooService',
'foobar' => 'getFoobarService',
);
$this->privates = array(
'bar' => true,
'foobar' => true,
);

$this->aliases = array();
}
Expand Down Expand Up @@ -61,37 +57,37 @@ public function isFrozen()
}

/**
* Gets the public 'foo' shared service.
* Gets the public 'bar' shared service.
*
* @return \FooCircular
* @return \BarCircular
*/
protected function getFooService()
protected function getBarService()
{
$a = ${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->getBarService()) && false ?: '_'};
$this->services['bar'] = $instance = new \BarCircular();

if (isset($this->services['foo'])) {
return $this->services['foo'];
}
$instance->addFoobar(${($_ = isset($this->services['foobar']) ? $this->services['foobar'] : $this->getFoobarService()) && false ?: '_'});

return $this->services['foo'] = new \FooCircular($a);
return $instance;
}

/**
* Gets the private 'bar' shared service.
* Gets the public 'foo' shared service.
*
* @return \BarCircular
* @return \FooCircular
*/
protected function getBarService()
protected function getFooService()
{
$this->services['bar'] = $instance = new \BarCircular();
$a = ${($_ = isset($this->services['bar']) ? $this->services['bar'] : $this->getBarService()) && false ?: '_'};

$instance->addFoobar(${($_ = isset($this->services['foobar']) ? $this->services['foobar'] : $this->getFoobarService()) && false ?: '_'});
if (isset($this->services['foo'])) {
return $this->services['foo'];
}

return $instance;
return $this->services['foo'] = new \FooCircular($a);
}

/**
* Gets the private 'foobar' shared service.
* Gets the public 'foobar' shared service.
*
* @return \FoobarCircular
*/
Expand Down