8000 [DependencyInjection] fix #29930 add $lazyLoad flag to the generated … · symfony/symfony@5c84edf · GitHub
[go: up one dir, main page]

Skip to content

Commit 5c84edf

Browse files
author
Anthony MARTIN
committed
[DependencyInjection] fix #29930 add $lazyLoad flag to the generated factory code for lazy non-shared services
| Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29930 | License | MIT | Doc PR | n/a Fix #29930 by adding $lazyLoad context to the generated code for lazy non-shared service by PhpDumper
1 parent a57ce22 commit 5c84edf

File tree

4 files changed

+616
-1
lines changed

4 files changed

+616
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,9 @@ private function generateServiceFiles()
831831
}
832832
$code[1] = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code[1])));
833833
$factory = sprintf('$this->factories%s[\'%s\']', $definition->isPublic() ? '' : "['service_container']", $id);
834-
$code[1] = sprintf("%s = function () {\n%s};\n\nreturn %1\$s();\n", $factory, $code[1]);
834+
$lazyloadInitialization = $definition->isLazy() ? '$lazyLoad = true' : '';
835+
836+
$code[1] = sprintf("%s = function (%s) {\n%s};\n\nreturn %1\$s();\n", $factory, $lazyloadInitialization, $code[1]);
835837
$code = $code[0].$code[1];
836838
}
837839

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,26 @@ public function testDumpAsFiles()
223223
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_as_files.txt', $dump);
224224
}
225225

226+
public function testNonSharedLazyDumpAsFiles()
227+
{
228+
$container = include self::$fixturesPath.'/containers/container9.php';
229+
$container->getDefinition('bar')->addTag('hot');
230+
$container->register('non_shared_foo', \Bar\FooLazyClass::class)
231+
->setFile(realpath(self::$fixturesPath.'/includes/foo_lazy.php'))
232+
->setShared(false)
233+
->setPublic(true)
234+
->setLazy(true);
235+
$container->compile();
236+
$dumper = new PhpDumper($container);
237+
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'hot_path_tag' => 'hot']), true);
238+
239+
if ('\\' === \DIRECTORY_SEPARATOR) {
240+
$dump = str_replace('\\\\Fixtures\\\\includes\\\\foo.php', '/Fixtures/includes/foo.php', $dump);
241+
$dump = str_replace('\\\\Fixtures\\\\includes\\\\foo_lazy.php', '/Fixtures/includes/foo_lazy.php', $dump);
242+
}
243+
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9bis_as_files.txt', $dump);
244+
}
245+
226246
public function testServicesWithAnonymousFactories()
227247
{
228248
$container = include self::$fixturesPath.'/containers/container19.php';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Bar;
4+
5+
class FooLazyClass
6+
{
7+
}

0 commit comments

Comments
 (0)
0