Skip to content

Commit

Permalink
Merge branch '3.4'
Browse files Browse the repository at this point in the history
* 3.4:
  Remove useless phpdoc
  [DI] Fix the "almost-circular refs" fix
  [Form] Fix low deps
  • Loading branch information
nicolas-grekas committed Nov 5, 2017
2 parents f0d640d + 7a397ea commit baa6ee6
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 23 deletions.
34 changes: 21 additions & 13 deletions Dumper/PhpDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1400,29 +1400,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
13 changes: 12 additions & 1 deletion Tests/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1177,8 +1177,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
26 changes: 22 additions & 4 deletions Tests/Dumper/PhpDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,17 +763,35 @@ public function testUninitializedReference()
$this->assertEquals(array('foo1' => new \stdClass(), 'foo3' => new \stdClass()), iterator_to_array($bar->iter));
}

public function xtestAlmostCircular()
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
4 changes: 2 additions & 2 deletions Tests/Fixtures/containers/container_almost_circular.php
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
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_Private extends Container
{
private $parameters;
private $targetDirs = array();
Expand Down Expand Up @@ -64,8 +64,12 @@ public function getRemovedIds()
protected function getFooService()
{
$a = new \BarCircular();
$a->addFoobar(new \FoobarCircular(($this->services['foo'] ?? $this->getFooService())));

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

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


return $instance;
}
}
104 changes: 104 additions & 0 deletions Tests/Fixtures/php/container_almost_circular_public.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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_Public extends Container
{
private $parameters;
private $targetDirs = array();
private $privates = array();

public function __construct()
{
$this->services = $this->privates = array();
$this->methodMap = array(
'bar' => 'getBarService',
'foo' => 'getFooService',
'foobar' => 'getFoobarService',
);

$this->aliases = array();
}

public function reset()
{
$this->privates = array();
parent::reset();
}

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

public function isCompiled()
{
return true;
}

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

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

$instance->addFoobar(($this->services['foobar'] ?? $this->getFoobarService()));

return $instance;
}

/**
* Gets the public 'foo' shared service.
*
* @return \FooCircular
*/
protected function getFooService()
{
$a = ($this->services['bar'] ?? $this->getBarService());

if (isset($this->services['foo'])) {
return $this->services['foo'];
}

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

/**
* Gets the public 'foobar' shared service.
*
* @return \FoobarCircular
*/
protected function getFoobarService()
{
$a = ($this->services['foo'] ?? $this->getFooService());

if (isset($this->services['foobar'])) {
return $this->services['foobar'];
}

return $this->services['foobar'] = new \FoobarCircular($a);
}
}

0 comments on commit baa6ee6

Please sign in to comment.