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

Skip to content

Commit 98d4dfd

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 32e1400 commit 98d4dfd

File tree

5 files changed

+145
-3
lines changed

5 files changed

+145
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,8 +687,8 @@ protected function {$methodName}($lazyInitialization)
687687
$code .= $this->addServiceInclude($id, $definition);
688688

689689
if ($this->getProxyDumper()->isProxyCandidate($definition)) {
690-
$factoryCode = $asFile ? "\$this->load('%s.php', false)" : '$this->%s(false)';
691-
$code .= $this->getProxyDumper()->getProxyFactoryCode($definition, $id, sprintf($factoryCode, $methodName));
690+
$factoryCode = $asFile ? ($definition->isShared() ? "\$this->load('%s.php', false)" : "\$this->factories['%2$s'](false)") : '$this->%s(false)';
691+
$code .= $this->getProxyDumper()->getProxyFactoryCode($definition, $id, sprintf($factoryCode, $methodName, $this->export($id)));
692692
}
693693

694694
if ($definition->isDeprecated()) {
@@ -862,7 +862,9 @@ private function generateServiceFiles(array $services)
862862
}
863863
$code[1] = implode("\n", array_map(function ($line) { return $line ? ' '.$line : $line; }, explode("\n", $code[1])));
864864
$factory = sprintf('$this->factories%s[\'%s\']', $definition->isPublic() ? '' : "['service_container']", $id);
865-
$code[1] = sprintf("%s = function () {\n%s};\n\nreturn %1\$s();\n", $factory, $code[1]);
865+
$lazyloadInitialization = $definition->isLazy() ? '$lazyLoad = true' : '';
866+
867+
$code[1] = sprintf("%s = function (%s) {\n%s};\n\nreturn %1\$s();\n", $factory, $lazyloadInitialization, $code[1]);
866868
$code = $code[0].$code[1];
867869
}
868870

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,24 @@ public function testDumpAsFiles()
230230
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_as_files.txt', $dump);
231231
}
232232

233+
public function testNonSharedLazyDumpAsFiles()
234+
{
235+
$container = include self::$fixturesPath.'/containers/container_non_shared_lazy.php';
236+
$container->register('non_shared_foo', \Bar\FooLazyClass::class)
237+
->setFile(realpath(self::$fixturesPath.'/includes/foo_lazy.php'))
238+
->setShared(false)
239+
->setPublic(true)
240+
->setLazy(true);
241+
$container->compile();
242+
$dumper = new PhpDumper($container);
243+
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__]), true);
244+
245+
if ('\\' === \DIRECTORY_SEPARATOR) {
246+
$dump = str_replace('\\\\Fixtures\\\\includes\\\\foo_lazy.php', '/Fixtures/includes/foo_lazy.php', $dump);
247+
}
248+
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services_non_shared_lazy_as_files.txt', $dump);
249+
}
250+
233251
public function testServicesWithAnonymousFactories()
234252
{
235253
$container = include self::$fixturesPath.'/containers/container19.php';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\ContainerBuilder;
4+
5+
return new ContainerBuilder();
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+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
Array
2+
(
3+
[Container%s/removed-ids.php] => <?php
4+
5+
return [
6+
'Psr\\Container\\ContainerInterface' => true,
7+
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
8+
];
9+
10+
[Container%s/getNonSharedFooService.php] => <?php
11+
12+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
13+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
14+
15+
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
16+
// Returns the public 'non_shared_foo' service.
17+
18+
include_once ($this->targetDirs[0].'/Fixtures/includes/foo_lazy.php');
19+
20+
$this->factories['non_shared_foo'] = function ($lazyLoad = true) {
21+
return new \Bar\FooLazyClass();
22+
};
23+
24+
return $this->factories['non_shared_foo']();
25+
26+
[Container%s/ProjectServiceContainer.php] => <?php
27+
28+
namespace Container%s;
29+
30+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
31+
use Symfony\Component\DependencyInjection\ContainerInterface;
32+
use Symfony\Component\DependencyInjection\Container;
33+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
34+
use Symfony\Component\DependencyInjection\Exception\LogicException;
35+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
36+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
37+
38+
/**
39+
* This class has been auto-generated
40+
* by the Symfony Dependency Injection Component.
41+
*
42+
* @final since Symfony 3.3
43+
*/
44+
class ProjectServiceContainer extends Container
45+
{
46+
private $buildParameters;
47+
private $containerDir;
48+
private $parameters;
49+
private $targetDirs = [];
50+
51+
public function __construct(array $buildParameters = [], $containerDir = __DIR__)
52+
{
53+
$dir = $this->targetDirs[0] = \dirname($containerDir);
54+
for ($i = 1; $i <= 5; ++$i) {
55+
$this->targetDirs[$i] = $dir = \dirname($dir);
56+
}
57+
$this->buildParameters = $buildParameters;
58+
$this->containerDir = $containerDir;
59+
$this->services = $this->privates = [];
60+
$this->fileMap = [
61+
'non_shared_foo' => 'getNonSharedFooService.php',
62+
];
63+
64+
$this->aliases = [];
65+
}
66+
67+
public function compile()
68+
{
69+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
70+
}
71+
72+
public function isCompiled()
73+
{
74+
return true;
75+
}
76+
77+
public function getRemovedIds()
78+
{
79+
return require $this->containerDir.\DIRECTORY_SEPARATOR.'removed-ids.php';
80+
}
81+
82+
protected function load($file, $lazyLoad = true)
83+
{
84+
return require $this->containerDir.\DIRECTORY_SEPARATOR.$file;
85+
}
86+
}
87+
88+
[ProjectServiceContainer.php] => <?php
89+
90+
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
91+
92+
if (\class_exists(\Container%s\ProjectServiceContainer::class, false)) {
93+
// no-op
94+
} elseif (!include __DIR__.'/Container%s/ProjectServiceContainer.php') {
95+
touch(__DIR__.'/Container%s.legacy');
96+
97+
return;
98+
}
99+
100+
if (!\class_exists(ProjectServiceContainer::class, false)) {
101+
\class_alias(\Container%s\ProjectServiceContainer::class, ProjectServiceContainer::class, false);
102+
}
103+
104+
return new \Container%s\ProjectServiceContainer([
105+
'container.build_hash' => '%s',
106+
'container.build_id' => '%s',
107+
'container.build_time' => %d,
108+
], __DIR__.\DIRECTORY_SEPARATOR.'Container%s');
109+
110+
)

0 commit comments

Comments
 (0)
0