Skip to content

Commit bda7e66

Browse files
author
Robin Chalas
committed
[DI] Fix dumping expressions accessing single-use private services
1 parent f063587 commit bda7e66

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,8 @@ private function getServiceCall(string $id, Reference $reference = null): string
16211621
return '$this';
16221622
}
16231623

1624+
$trivial = false;
1625+
16241626
if ($this->container->hasDefinition($id) && $definition = $this->container->getDefinition($id)) {
16251627
if ($definition->isSynthetic()) {
16261628
$code = sprintf('$this->get(\'%s\'%s)', $id, null !== $reference ? ', '.$reference->getInvalidBehavior() : '');
@@ -1629,7 +1631,7 @@ private function getServiceCall(string $id, Reference $reference = null): string
16291631
if (!$definition->isShared()) {
16301632
return $code;
16311633
}
1632-
} elseif ($this->isTrivialInstance($definition)) {
1634+
} elseif ($trivial = $this->isTrivialInstance($definition)) {
16331635
if ($e = $definition->getErrors()) {
16341636
$this->addThrow = true;
16351637

@@ -1650,6 +1652,8 @@ private function getServiceCall(string $id, Reference $reference = null): string
16501652
}
16511653
if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) {
16521654
$code = sprintf('($this->%s[\'%s\'] ?? %s)', $definition->isPublic() ? 'services' : 'privates', $id, $code);
1655+
} elseif ($trivial) {
1656+
$code = "($code)";
16531657
}
16541658

16551659
return $code;

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ public function testExpressionReferencingPrivateService()
799799
->setPublic(false);
800800
$container->register('public_foo', 'stdClass')
801801
->setPublic(true)
802-
->addArgument(new Expression('service("private_foo")'));
802+
->addArgument(new Expression('service("private_foo").bar'));
803803

804804
$container->compile();
805805
$dumper = new PhpDumper($container);

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_private_in_expression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ public function getRemovedIds()
5656
*/
5757
protected function getPublicFooService()
5858
{
59-
return $this->services['public_foo'] = new \stdClass(new \stdClass());
59+
return $this->services['public_foo'] = new \stdClass((new \stdClass())->bar);
6060
}
6161
}

0 commit comments

Comments
 (0)