diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 75409395cfaa5..5eab1b56d0e9c 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -1154,6 +1154,9 @@ private function addNewInstance(Definition $definition, string $return = '', str if (null !== $definition->getFactory()) { $callable = $definition->getFactory(); + if ('current' === $callable && [0] === array_keys($definition->getArguments()) && \is_array($value) && [0] === array_keys($value)) { + return $return.$this->dumpValue($value[0]).$tail; + } if (['Closure', 'fromCallable'] === $callable && [0] === array_keys($definition->getArguments())) { $callable = $definition->getArgument(0); if ($callable instanceof ServiceClosureArgument) { diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index 2c5c61b5bea59..57f7325e8615e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -1503,6 +1503,37 @@ public function testWitherWithStaticReturnType() $this->assertInstanceOf(Foo::class, $wither->foo); } + public function testCurrentFactoryInlining() + { + $container = new ContainerBuilder(); + $container->register(Foo::class); + + $container + ->register('inlined_current', Foo::class) + ->setFactory('current') + ->setPublic(true) + ->setArguments([[new Reference(Foo::class)]]); + + $container + ->register('not_inlined_current', Foo::class) + ->setFactory('current') + ->setPublic(true) + ->setArguments([[new Reference(Foo::class), 123]]); + + $container->compile(); + $dumper = new PhpDumper($container); + $dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Service_CurrentFactoryInlining']); + file_put_contents(self::$fixturesPath.'/php/services_current_factory_inlining.php', $dump); + $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_current_factory_inlining.php', $dump); + eval('?>'.$dump); + + $container = new \Symfony_DI_PhpDumper_Service_CurrentFactoryInlining(); + + $foo = $container->get('inlined_current'); + $this->assertInstanceOf(Foo::class, $foo); + $this->assertSame($foo, $container->get('not_inlined_current')); + } + public function testDumpServiceWithAbstractArgument() { $this->expectException(RuntimeException::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_current_factory_inlining.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_current_factory_inlining.php new file mode 100644 index 0000000000000..4c641bc877871 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_current_factory_inlining.php @@ -0,0 +1,68 @@ +ref = \WeakReference::create($this); + $this->services = $this->privates = []; + $this->methodMap = [ + 'inlined_current' => 'getInlinedCurrentService', + 'not_inlined_current' => 'getNotInlinedCurrentService', + ]; + + $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 [ + 'Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo' => true, + ]; + } + + /** + * Gets the public 'inlined_current' shared service. + * + * @return \Symfony\Component\DependencyInjection\Tests\Compiler\Foo + */ + protected static function getInlinedCurrentService($container) + { + return $container->services['inlined_current'] = ($container->privates['Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo()); + } + + /** + * Gets the public 'not_inlined_current' shared service. + * + * @return \Symfony\Component\DependencyInjection\Tests\Compiler\Foo + */ + protected static function getNotInlinedCurrentService($container) + { + return $container->services['not_inlined_current'] = \current([($container->privates['Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo()), 123]); + } +}