Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb434a4

Browse files
committed
[DependencyInjection] Fix fetching lazy non-shared services multiple times with as files true
1 parent 431d321 commit cb434a4

File tree

5 files changed

+69
-54
lines changed

5 files changed

+69
-54
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -928,12 +928,10 @@ protected static function {$methodName}(\$container$lazyInitialization)
928928
if (!$definition->isShared()) {
929929
$code .= sprintf(' %s ??= ', $factory);
930930

931-
if ($asFile) {
932-
$code .= "self::do(...);\n\n";
933-
} elseif ($definition->isPublic()) {
934-
$code .= sprintf("fn () => self::%s(\$container);\n\n", $methodName);
931+
if ($definition->isPublic()) {
932+
$code .= sprintf("fn () => self::%s(\$container);\n\n", $asFile ? 'do' : $methodName);
935933
} else {
936-
$code .= sprintf("self::%s(...);\n\n", $methodName);
934+
$code .= sprintf("self::%s(...);\n\n", $asFile ? 'do' : $methodName);
937935
}
938936
}
939937
$lazyLoad = $asGhostObject ? '$proxy' : 'false';

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

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -339,24 +339,6 @@ public function testDumpAsFilesWithLazyFactoriesInlined()
339339
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_lazy_inlined_factories.txt', $dump);
340340
}
341341

342-
public function testNonSharedLazyDumpAsFiles()
343-
{
344-
$container = include self::$fixturesPath.'/containers/container_non_shared_lazy.php';
345-
$container->register('non_shared_foo', \Bar\FooLazyClass::class)
346-
->setFile(realpath(self::$fixturesPath.'/includes/foo_lazy.php'))
347-
->setShared(false)
348-
->setPublic(true)
349-
->setLazy(true);
350-
$container->compile();
351-
$dumper = new PhpDumper($container);
352-
$dump = print_r($dumper->dump(['as_files' => true, 'file' => __DIR__, 'inline_factories' => false, 'inline_class_loader' => false]), true);
353-
354-
if ('\\' === \DIRECTORY_SEPARATOR) {
355-
$dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump);
356-
}
357-
$this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services_non_shared_lazy_as_files.txt', $dump);
358-
}
359-
360342
public function testServicesWithAnonymousFactories()
361343
{
362344
$container = include self::$fixturesPath.'/containers/container19.php';
@@ -772,7 +754,7 @@ public function testNonSharedLazy()
772754
$container = new ContainerBuilder();
773755

774756
$container
775-
->register('foo', Foo::class)
757+
->register('foo', \Bar\FooLazyClass::class)
776758
->setFile(realpath(self::$fixturesPath.'/includes/foo_lazy.php'))
777759
->setShared(false)
778760
->setLazy(true)
@@ -803,6 +785,48 @@ public function testNonSharedLazy()
803785
$this->assertNotSame($foo1, $foo2);
804786
}
805787

788+
public function testNonSharedLazyAsFiles()
789+
{
790+
$container = new ContainerBuilder();
791+
792+
$container
793+
->register('non_shared_foo', \Bar\FooLazyClass::class)
794+
->setFile(realpath(self::$fixturesPath.'/includes/foo_lazy.php'))
795+
->setShared(false)
796+
->setLazy(true)
797+
->setPublic(true);
798+
799+
$container->compile();
800+
$dumper = new PhpDumper($container);
801+
$dumps = $dumper->dump([
802+
'class' => 'Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File',
803+
'as_files' => true,
804+
'inline_factories' => false,
805+
'inline_class_loader' => false,
806+
]);
807+
808+
$stringDump = print_r($dumps, true);
809+
$this->assertStringMatchesFormatFile(
810+
self::$fixturesPath.'/php/services_non_shared_lazy_as_files.txt',
811+
'\\' === \DIRECTORY_SEPARATOR ? str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $stringDump) : $stringDump
812+
);
813+
814+
$lastDump = array_pop($dumps);
815+
foreach (array_reverse($dumps) as $dump) {
816+
eval('?>'.$dump);
817+
}
818+
819+
$container = eval('?>'.$lastDump);
820+
821+
$foo1 = $container->get('non_shared_foo');
822+
$this->assertTrue($foo1->resetLazyObject());
823+
824+
$foo2 = $container->get('non_shared_foo');
825+
$this->assertTrue($foo2->resetLazyObject());
826+
827+
$this->assertNotSame($foo1, $foo2);
828+
}
829+
806830
/**
807831
* @testWith [false]
808832
* [true]

src/Symfony/Component/DependencyInjection/Tests/Fixtures/containers/container_non_shared_lazy.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1010
/**
1111
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
1212
*/
13-
class getNonSharedFooService extends ProjectServiceContainer
13+
class getNonSharedFooService extends Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File
1414
{
1515
/**
1616
* Gets the public 'non_shared_foo' service.
@@ -19,16 +19,16 @@ class getNonSharedFooService extends ProjectServiceContainer
1919
*/
2020
public static function do($container, $lazyLoad = true)
2121
{
22-
$container->factories['non_shared_foo'] ??= self::do(...);
22+
$container->factories['non_shared_foo'] ??= fn () => self::do($container);
2323

2424
if (true === $lazyLoad) {
25-
return $container->createProxy('FooLazyClassGhost0fc418d', static fn () => \FooLazyClassGhost0fc418d::createLazyGhost(static fn ($proxy) => self::do($container, $proxy)));
25+
return $container->createProxy('FooLazyClassGhost%s', static fn () => \FooLazyClassGhost%s::createLazyGhost(static fn ($proxy) => self::do($container, $proxy)));
2626
}
2727

2828
static $include = true;
2929

3030
if ($include) {
31-
include_once $container->targetDir.''.'/Fixtures/includes/foo_lazy.php';
31+
include_once '%sfoo_lazy.php';
3232

3333
$include = false;
3434
}
@@ -37,11 +37,11 @@ class getNonSharedFooService extends ProjectServiceContainer
3737
}
3838
}
3939

40-
[Container%s/FooLazyClassGhost0fc418d.php] => <?php
40+
[Container%s/FooLazyClassGhost%s.php] => <?php
4141

4242
namespace Container%s;
4343

44-
class FooLazyClassGhost0fc418d extends \Bar\FooLazyClass implements \Symfony\Component\VarExporter\LazyObjectInterface
44+
class FooLazyClassGhost%s extends \Bar\FooLazyClass implements \Symfony\Component\VarExporter\LazyObjectInterface
4545
{
4646
use \Symfony\Component\VarExporter\LazyGhostTrait;
4747

@@ -53,11 +53,11 @@ class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);
5353
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class);
5454
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
5555

56-
if (!\class_exists('FooLazyClassGhost0fc418d', false)) {
57-
\class_alias(__NAMESPACE__.'\\FooLazyClassGhost0fc418d', 'FooLazyClassGhost0fc418d', false);
56+
if (!\class_exists('FooLazyClassGhost%s', false)) {
57+
\class_alias(__NAMESPACE__.'\\FooLazyClassGhost%s', 'FooLazyClassGhost%s', false);
5858
}
5959

60-
[Container%s/ProjectServiceContainer.php] => <?php
60+
[Container%s/Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File.php] => <?php
6161

6262
namespace Container%s;
6363

@@ -73,14 +73,12 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
7373
/**
7474
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
7575
*/
76-
class ProjectServiceContainer extends Container
76+
class Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File extends Container
7777
{
78-
protected $targetDir;
7978
protected $parameters = [];
8079

8180
public function __construct(private array $buildParameters = [], protected string $containerDir = __DIR__)
8281
{
83-
$this->targetDir = \dirname($containerDir);
8482
$this->services = $this->privates = [];
8583
$this->fileMap = [
8684
'non_shared_foo' => 'getNonSharedFooService',
@@ -124,7 +122,7 @@ class ProjectServiceContainer extends Container
124122
}
125123
}
126124

127-
[ProjectServiceContainer.preload.php] => <?php
125+
[Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File.preload.php] => <?php
128126

129127
// This file has been auto-generated by the Symfony Dependency Injection Component
130128
// You can reference it in the "opcache.preload" php.ini setting on PHP >= 7.4 when preloading is desired
@@ -135,9 +133,9 @@ if (in_array(PHP_SAPI, ['cli', 'phpdbg'], true)) {
135133
return;
136134
}
137135

138-
require dirname(__DIR__, %d).'%svendor/autoload.php';
139-
(require __DIR__.'/ProjectServiceContainer.php')->set(\Container%s\ProjectServiceContainer::class, null);
140-
require __DIR__.'/Container%s/FooLazyClassGhost0fc418d.php';
136+
require '%svendor/autoload.php';
137+
(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);
138+
require __DIR__.'/Container%s/FooLazyClassGhost%s.php';
141139
require __DIR__.'/Container%s/getNonSharedFooService.php';
142140

143141
$classes = [];
@@ -146,23 +144,23 @@ $classes[] = 'Symfony\Component\DependencyInjection\ContainerInterface';
146144

147145
$preloaded = Preloader::preload($classes);
148146

149-
[ProjectServiceContainer.php] => <?php
147+
[Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File.php] => <?php
150148

151149
// This file has been auto-generated by the Symfony Dependency Injection Component for internal use.
152150

153-
if (\class_exists(\Container%s\ProjectServiceContainer::class, false)) {
151+
if (\class_exists(\Container%s\Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File::class, false)) {
154152
// no-op
155-
} elseif (!include __DIR__.'/Container%s/ProjectServiceContainer.php') {
153+
} elseif (!include __DIR__.'/Container%s/Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File.php') {
156154
touch(__DIR__.'/Container%s.legacy');
157155

158156
return;
159157
}
160158

161-
if (!\class_exists(ProjectServiceContainer::class, false)) {
162-
\class_alias(\Container%s\ProjectServiceContainer::class, ProjectServiceContainer::class, false);
159+
if (!\class_exists(Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File::class, false)) {
160+
\class_alias(\Container%s\Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File::class, Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File::class, false);
163161
}
164162

165-
return new \Container%s\ProjectServiceContainer([
163+
return new \Container%s\Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File([
166164
'container.build_hash' => '%s',
167165
'container.build_id' => '%s',
168166
'container.build_time' => %d,

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_public.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ protected function createProxy($class, \Closure $factory)
4444
/**
4545
* Gets the public 'foo' service.
4646
*
47-
* @return \Symfony\Component\DependencyInjection\Tests\Compiler\Foo
47+
* @return \Bar\FooLazyClass
4848
*/
4949
protected static function getFooService($container, $lazyLoad = true)
5050
{
5151
$container->factories['foo'] ??= fn () => self::getFooService($container);
5252

5353
if (true === $lazyLoad) {
54-
return $container->createProxy('FooGhost80f7cfc', static fn () => \FooGhost80f7cfc::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy)));
54+
return $container->createProxy('FooLazyClassGhost2108fce', static fn () => \FooLazyClassGhost2108fce::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy)));
5555
}
5656

5757
static $include = true;
@@ -66,7 +66,7 @@ protected static function getFooService($container, $lazyLoad = true)
6666
}
6767
}
6868

69-
class FooGhost80f7cfc extends \Symfony\Component\DependencyInjection\Tests\Compiler\Foo implements \Symfony\Component\VarExporter\LazyObjectInterface
69+
class FooLazyClassGhost2108fce extends \Bar\FooLazyClass implements \Symfony\Component\VarExporter\LazyObjectInterface
7070
{
7171
use \Symfony\Component\VarExporter\LazyGhostTrait;
7272

0 commit comments

Comments
 (0)