Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c156d03

Browse filesBrowse 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 c156d03
Copy full SHA for c156d03

File tree

Expand file treeCollapse file tree

5 files changed

+145
-2
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+145
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ 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)';
690+
$factoryCode = $asFile ? ($definition->isShared() ? "\$this->load('%s.php', false)" : "\$this->factories['%s'](false)") : '$this->%s(false)';
691691
$code .= $this->getProxyDumper()->getProxyFactoryCode($definition, $id, sprintf($factoryCode, $methodName));
692692
}
693693

@@ -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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,25 @@ 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 =
244+
print_r($dumper->dump(['as_files' => true, 'file' => __DIR__])) & die; //, true);
245+
246+
if ('\\' === \DIRECTORY_SEPARATOR) {
247+
$dump = str_replace('\\\\Fixtures\\\\includes\\\\foo_lazy.php', '/Fixtures/includes/foo_lazy.php', $dump);
248+
}
249+
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services_non_shared_lazy_as_files.txt', $dump);
250+
}
251+
233252
public function testServicesWithAnonymousFactories()
234253
{
235254
$container = include self::$fixturesPath.'/containers/container19.php';
+5Lines 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();
+7Lines 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+
}
+110Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.