From a12184ce5265532668653bc4206a67fc4695e606 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Mon, 17 Jul 2023 00:02:33 +0200 Subject: [PATCH] [DependencyInjection] Fix fetching lazy non-shared services multiple times with as files true --- .../DependencyInjection/Dumper/PhpDumper.php | 8 +-- .../Tests/Dumper/PhpDumperTest.php | 62 +++++++++++++------ .../containers/container_non_shared_lazy.php | 5 -- .../php/services_non_shared_lazy_as_files.txt | 42 ++++++------- .../php/services_non_shared_lazy_public.php | 6 +- 5 files changed, 69 insertions(+), 54 deletions(-) delete mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_non_shared_lazy.php diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 5ecdee93549c1..38270e16b0e38 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -928,12 +928,10 @@ protected static function {$methodName}(\$container$lazyInitialization) if (!$definition->isShared()) { $code .= sprintf(' %s ??= ', $factory); - if ($asFile) { - $code .= "self::do(...);\n\n"; - } elseif ($definition->isPublic()) { - $code .= sprintf("fn () => self::%s(\$container);\n\n", $methodName); + if ($definition->isPublic()) { + $code .= sprintf("fn () => self::%s(\$container);\n\n", $asFile ? 'do' : $methodName); } else { - $code .= sprintf("self::%s(...);\n\n", $methodName); + $code .= sprintf("self::%s(...);\n\n", $asFile ? 'do' : $methodName); } } $lazyLoad = $asGhostObject ? '$proxy' : 'false'; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index ede09490faf61..91f8a20dcc224 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -339,24 +339,6 @@ public function testDumpAsFilesWithLazyFactoriesInlined() $this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_lazy_inlined_factories.txt', $dump); } - public function testNonSharedLazyDumpAsFiles() - { - $container = include self::$fixturesPath.'/containers/container_non_shared_lazy.php'; - $container->register('non_shared_foo', \Bar\FooLazyClass::class) - ->setFile(realpath(self::$fixturesPath.'/includes/foo_lazy.php')) - ->setShared(false) - ->setPublic(true) - ->setLazy(true); - $container->compile(); - $dumper = new PhpDumper($container); - $dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'inline_factories' => false, 'inline_class_loader' => false]), true); - - if ('\\' === \DIRECTORY_SEPARATOR) { - $dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump); - } - $this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services_non_shared_lazy_as_files.txt', $dump); - } - public function testServicesWithAnonymousFactories() { $container = include self::$fixturesPath.'/containers/container19.php'; @@ -772,7 +754,7 @@ public function testNonSharedLazy() $container = new ContainerBuilder(); $container - ->register('foo', Foo::class) + ->register('foo', \Bar\FooLazyClass::class) ->setFile(realpath(self::$fixturesPath.'/includes/foo_lazy.php')) ->setShared(false) ->setLazy(true) @@ -803,6 +785,48 @@ public function testNonSharedLazy() $this->assertNotSame($foo1, $foo2); } + public function testNonSharedLazyAsFiles() + { + $container = new ContainerBuilder(); + + $container + ->register('non_shared_foo', \Bar\FooLazyClass::class) + ->setFile(realpath(self::$fixturesPath.'/includes/foo_lazy.php')) + ->setShared(false) + ->setLazy(true) + ->setPublic(true); + + $container->compile(); + $dumper = new PhpDumper($container); + $dumps = $dumper->dump([ + 'class' => 'Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File', + 'as_files' => true, + 'inline_factories' => false, + 'inline_class_loader' => false, + ]); + + $stringDump = print_r($dumps, true); + $this->assertStringMatchesFormatFile( + self::$fixturesPath.'/php/services_non_shared_lazy_as_files.txt', + '\\' === \DIRECTORY_SEPARATOR ? str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $stringDump) : $stringDump + ); + + $lastDump = array_pop($dumps); + foreach (array_reverse($dumps) as $dump) { + eval('?>'.$dump); + } + + $container = eval('?>'.$lastDump); + + $foo1 = $container->get('non_shared_foo'); + $this->assertTrue($foo1->resetLazyObject()); + + $foo2 = $container->get('non_shared_foo'); + $this->assertTrue($foo2->resetLazyObject()); + + $this->assertNotSame($foo1, $foo2); + } + /** * @testWith [false] * [true] diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_non_shared_lazy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_non_shared_lazy.php deleted file mode 100644 index 58ff0bd5dcd57..0000000000000 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_non_shared_lazy.php +++ /dev/null @@ -1,5 +0,0 @@ -factories['non_shared_foo'] ??= self::do(...); + $container->factories['non_shared_foo'] ??= fn () => self::do($container); if (true === $lazyLoad) { - return $container->createProxy('FooLazyClassGhost0fc418d', static fn () => \FooLazyClassGhost0fc418d::createLazyGhost(static fn ($proxy) => self::do($container, $proxy))); + return $container->createProxy('FooLazyClassGhost%s', static fn () => \FooLazyClassGhost%s::createLazyGhost(static fn ($proxy) => self::do($container, $proxy))); } static $include = true; if ($include) { - include_once $container->targetDir.''.'/Fixtures/includes/foo_lazy.php'; + include_once '%s/Fixtures/includes/foo_lazy.php'; $include = false; } @@ -37,11 +37,11 @@ class getNonSharedFooService extends ProjectServiceContainer } } - [Container%s/FooLazyClassGhost0fc418d.php] => targetDir = \dirname($containerDir); $this->services = $this->privates = []; $this->fileMap = [ 'non_shared_foo' => 'getNonSharedFooService', @@ -124,7 +122,7 @@ class ProjectServiceContainer extends Container } } - [ProjectServiceContainer.preload.php] => = 7.4 when preloading is desired @@ -135,9 +133,9 @@ if (in_array(PHP_SAPI, ['cli', 'phpdbg'], true)) { return; } -require dirname(__DIR__, %d).'%svendor/autoload.php'; -(require __DIR__.'/ProjectServiceContainer.php')->set(\Container%s\ProjectServiceContainer::class, null); -require __DIR__.'/Container%s/FooLazyClassGhost0fc418d.php'; +require '%s/vendor/autoload.php'; +(require __DIR__.'/Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File.php')->set(\Container%s\Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File::class, null); +require __DIR__.'/Container%s/FooLazyClassGhost%s.php'; require __DIR__.'/Container%s/getNonSharedFooService.php'; $classes = []; @@ -146,23 +144,23 @@ $classes[] = 'Symfony\Component\DependencyInjection\ContainerInterface'; $preloaded = Preloader::preload($classes); - [ProjectServiceContainer.php] => '%s', 'container.build_id' => '%s', 'container.build_time' => %d, diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_public.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_public.php index 3d087c18f665a..dbf0f5d5e1134 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_public.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_public.php @@ -44,14 +44,14 @@ protected function createProxy($class, \Closure $factory) /** * Gets the public 'foo' service. * - * @return \Symfony\Component\DependencyInjection\Tests\Compiler\Foo + * @return \Bar\FooLazyClass */ protected static function getFooService($container, $lazyLoad = true) { $container->factories['foo'] ??= fn () => self::getFooService($container); if (true === $lazyLoad) { - return $container->createProxy('FooGhost80f7cfc', static fn () => \FooGhost80f7cfc::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy))); + return $container->createProxy('FooLazyClassGhost2108fce', static fn () => \FooLazyClassGhost2108fce::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy))); } static $include = true; @@ -66,7 +66,7 @@ protected static function getFooService($container, $lazyLoad = true) } } -class FooGhost80f7cfc extends \Symfony\Component\DependencyInjection\Tests\Compiler\Foo implements \Symfony\Component\VarExporter\LazyObjectInterface +class FooLazyClassGhost2108fce extends \Bar\FooLazyClass implements \Symfony\Component\VarExporter\LazyObjectInterface { use \Symfony\Component\VarExporter\LazyGhostTrait;