diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index e4a7ae55d5a64..0d4cfa00ee3a9 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -1073,7 +1073,7 @@ private function addInlineService(string $id, Definition $definition, Definition } $code .= $this->addServiceProperties($inlineDef, $name); - $code .= $this->addServiceMethodCalls($inlineDef, $name, !$this->getProxyDumper()->isProxyCandidate($inlineDef) && $inlineDef->isShared() && !isset($this->singleUsePrivateIds[$id]) ? $id : null); + $code .= $this->addServiceMethodCalls($inlineDef, $name, !$this->getProxyDumper()->isProxyCandidate($inlineDef) && $inlineDef->isPublic() && $inlineDef->isShared() && !isset($this->singleUsePrivateIds[$id]) ? $id : null); $code .= $this->addServiceConfigurator($inlineDef, $name); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index fe747281ff559..e3e5971264be1 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -42,6 +42,7 @@ use Symfony\Component\DependencyInjection\ServiceLocator; use Symfony\Component\DependencyInjection\Tests\Compiler\Foo; use Symfony\Component\DependencyInjection\Tests\Compiler\Wither; +use Symfony\Component\DependencyInjection\Tests\Compiler\WitherConsumer; use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition; use Symfony\Component\DependencyInjection\Tests\Fixtures\FooClassWithEnumAttribute; use Symfony\Component\DependencyInjection\Tests\Fixtures\FooUnitEnum; @@ -1458,6 +1459,34 @@ public function testWither() $this->assertInstanceOf(Foo::class, $wither->foo); } + public function testWitherPrivate() + { + $container = new ContainerBuilder(); + $container->register(Foo::class); + + $container + ->register('wither', Wither::class) + ->setPublic(false) + ->setAutowired(true) + ; + + $container + ->register('wither_consumer', WitherConsumer::class) + ->setPublic(true) + ->addArgument(new Reference('wither')); + + $container->compile(); + $dumper = new PhpDumper($container); + $dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Service_WitherPrivate']); + $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_wither_private.php', $dump); + eval('?>'.$dump); + + $container = new \Symfony_DI_PhpDumper_Service_WitherPrivate(); + + $witherConsumer = $container->get('wither_consumer'); + $this->assertInstanceOf(Foo::class, $witherConsumer->wither->foo); + } + /** * @requires PHP 8 */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php index 96c2b68b7d083..bb872493ff6fe 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php @@ -304,6 +304,16 @@ public function withFoo2(Foo $foo): self } } +final class WitherConsumer +{ + public $wither; + + public function __construct(Wither $wither) + { + $this->wither = $wither; + } +} + class SetterInjectionParent { /** @required*/ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_private.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_private.php new file mode 100644 index 0000000000000..4917afad83f16 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_private.php @@ -0,0 +1,66 @@ +services = $this->privates = []; + $this->methodMap = [ + 'wither_consumer' => 'getWitherConsumerService', + ]; + + $this->aliases = []; + } + + public function compile(): void + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled(): bool + { + return true; + } + + public function getRemovedIds(): array + { + return [ + 'Psr\\Container\\ContainerInterface' => true, + 'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true, + 'Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo' => true, + 'wither' => true, + ]; + } + + /** + * Gets the public 'wither_consumer' shared service. + * + * @return \Symfony\Component\DependencyInjection\Tests\Compiler\WitherConsumer + */ + protected function getWitherConsumerService() + { + $a = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither(); + + $b = new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo(); + + $a = $a->withFoo1($b); + $a = $a->withFoo2($b); + $a->setFoo($b); + + return $this->services['wither_consumer'] = new \Symfony\Component\DependencyInjection\Tests\Compiler\WitherConsumer($a); + } +}