Skip to content

Commit 0d12d93

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

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,8 @@ private function addServiceConfigurator(Definition $definition, string $variable
595595

596596
if (\is_array($callable)) {
597597
if ($callable[0] instanceof Reference
598-
|| ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))
599-
) {
600-
$callable[0] = $this->dumpValue($callable[0]);
601-
602-
return sprintf(' '.('$' === $callable[0][0] ? '%s' : '(%s)')."->%s(\$%s);\n", $callable[0], $callable[1], $variableName);
598+
|| ($callable[0] instanceof Definition && $this->definitionVariables->contains($callable[0]))) {
599+
return sprintf(" %s->%s(\$%s);\n", $this->dumpValue($callable[0]), $callable[1], $variableName);
603600
}
604601

605602
$class = $this->dumpValue($callable[0]);
@@ -1629,7 +1626,7 @@ private function getServiceCall(string $id, Reference $reference = null): string
16291626
if (!$definition->isShared()) {
16301627
return $code;
16311628
}
1632-
} elseif ($this->isTrivialInstance($definition)) {
1629+
} elseif ($trivial = $this->isTrivialInstance($definition)) {
16331630
if ($e = $definition->getErrors()) {
16341631
$this->addThrow = true;
16351632

@@ -1650,6 +1647,8 @@ private function getServiceCall(string $id, Reference $reference = null): string
16501647
}
16511648
if ($definition->isShared() && !isset($this->singleUsePrivateIds[$id])) {
16521649
$code = sprintf('($this->%s[\'%s\'] ?? %s)', $definition->isPublic() ? 'services' : 'privates', $id, $code);
1650+
} elseif ($trivial ?? false) {
1651+
$code = "($code)";
16531652
}
16541653

16551654
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)