Skip to content

Commit 6ccc980

Browse files
[DI] Fix dumping ignore-on-uninitialized references to synthetic services
1 parent 55e6036 commit 6ccc980

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

Dumper/PhpDumper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,8 +1898,10 @@ private function getServiceCall($id, Reference $reference = null)
18981898
return '$this';
18991899
}
19001900

1901-
if ($this->container->hasDefinition($id) && ($definition = $this->container->getDefinition($id)) && !$definition->isSynthetic()) {
1902-
if (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
1901+
if ($this->container->hasDefinition($id) && $definition = $this->container->getDefinition($id)) {
1902+
if ($definition->isSynthetic()) {
1903+
$code = sprintf('$this->get(\'%s\'%s)', $id, null !== $reference ? ', '.$reference->getInvalidBehavior() : '');
1904+
} elseif (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
19031905
$code = 'null';
19041906
if (!$definition->isShared()) {
19051907
return $code;

Tests/ContainerBuilderTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,21 @@ public function testArgumentsHaveHigherPriorityThanBindings()
14341434
$this->assertSame('via-argument', $container->get('foo')->class1->identifier);
14351435
$this->assertSame('via-bindings', $container->get('foo')->class2->identifier);
14361436
}
1437+
1438+
public function testUninitializedSyntheticReference()
1439+
{
1440+
$container = new ContainerBuilder();
1441+
$container->register('foo', 'stdClass')->setPublic(true)->setSynthetic(true);
1442+
$container->register('bar', 'stdClass')->setPublic(true)->setShared(false)
1443+
->setProperty('foo', new Reference('foo', ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE));
1444+
1445+
$container->compile();
1446+
1447+
$this->assertEquals((object) array('foo' => null), $container->get('bar'));
1448+
1449+
$container->set('foo', (object) array(123));
1450+
$this->assertEquals((object) array('foo' => (object) array(123)), $container->get('bar'));
1451+
}
14371452
}
14381453

14391454
class FooClass

Tests/Dumper/PhpDumperTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,29 @@ public function testDumpHandlesObjectClassNames()
913913
$this->assertInstanceOf('stdClass', $container->get('bar'));
914914
}
915915

916+
public function testUninitializedSyntheticReference()
917+
{
918+
$container = new ContainerBuilder();
919+
$container->register('foo', 'stdClass')->setPublic(true)->setSynthetic(true);
920+
$container->register('bar', 'stdClass')->setPublic(true)->setShared(false)
921+
->setProperty('foo', new Reference('foo', ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE));
922+
923+
$container->compile();
924+
925+
$dumper = new PhpDumper($container);
926+
eval('?>'.$dumper->dump(array(
927+
'class' => 'Symfony_DI_PhpDumper_Test_UninitializedSyntheticReference',
928+
'inline_class_loader_parameter' => 'inline_requires',
929+
)));
930+
931+
$container = new \Symfony_DI_PhpDumper_Test_UninitializedSyntheticReference();
932+
933+
$this->assertEquals((object) array('foo' => null), $container->get('bar'));
934+
935+
$container->set('foo', (object) array(123));
936+
$this->assertEquals((object) array('foo' => (object) array(123)), $container->get('bar'));
937+
}
938+
916939
/**
917940
* @group legacy
918941
* @expectedDeprecation The "private" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead.

0 commit comments

Comments
 (0)