Skip to content

Commit ed53735

Browse files
Merge branch '6.2' into 6.3
* 6.2: [Runtime][ErrorHandler] Don't mess with ini_set('assert.warning') [DependencyInjection] Fix fetching lazy non-shared services multiple times
2 parents 7a8fead + 87cd715 commit ed53735

File tree

4 files changed

+237
-1
lines changed

4 files changed

+237
-1
lines changed

Dumper/PhpDumper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,8 @@ protected static function {$methodName}(\$container$lazyInitialization)
930930

931931
if ($asFile) {
932932
$code .= "self::do(...);\n\n";
933+
} elseif ($definition->isPublic()) {
934+
$code .= sprintf("fn () => self::%s(\$container);\n\n", $methodName);
933935
} else {
934936
$code .= sprintf("self::%s(...);\n\n", $methodName);
935937
}

Tests/Dumper/PhpDumperTest.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,42 @@ public function testInlinedDefinitionReferencingServiceContainer()
767767
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services13.php', $dumper->dump(), '->dump() dumps inline definitions which reference service_container');
768768
}
769769

770+
public function testNonSharedLazy()
771+
{
772+
$container = new ContainerBuilder();
773+
774+
$container
775+
->register('foo', Foo::class)
776+
->setFile(realpath(self::$fixturesPath.'/includes/foo_lazy.php'))
777+
->setShared(false)
778+
->setLazy(true)
779+
->setPublic(true);
780+
781+
$container->compile();
782+
$dumper = new PhpDumper($container);
783+
$dump = $dumper->dump([
784+
'class' => 'Symfony_DI_PhpDumper_Service_Non_Shared_Lazy',
785+
'file' => __DIR__,
786+
'inline_factories' => false,
787+
'inline_class_loader' => false,
788+
]);
789+
$this->assertStringEqualsFile(
790+
self::$fixturesPath.'/php/services_non_shared_lazy_public.php',
791+
'\\' === \DIRECTORY_SEPARATOR ? str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump) : $dump
792+
);
793+
eval('?>'.$dump);
794+
795+
$container = new \Symfony_DI_PhpDumper_Service_Non_Shared_Lazy();
796+
797+
$foo1 = $container->get('foo');
798+
$this->assertTrue($foo1->resetLazyObject());
799+
800+
$foo2 = $container->get('foo');
801+
$this->assertTrue($foo2->resetLazyObject());
802+
803+
$this->assertNotSame($foo1, $foo2);
804+
}
805+
770806
/**
771807
* @testWith [false]
772808
* [true]
@@ -775,7 +811,7 @@ public function testNonSharedLazyDefinitionReferences(bool $asGhostObject)
775811
{
776812
$container = new ContainerBuilder();
777813
$container->register('foo', 'stdClass')->setShared(false)->setLazy(true);
778-
$container->register('bar', 'stdClass')->addArgument(new Reference('foo', ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, false))->setPublic(true);
814+
$container->register('bar', 'stdClass')->addArgument(new Reference('foo', ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE))->setPublic(true);
779815
$container->compile();
780816

781817
$dumper = new PhpDumper($container);
@@ -1563,6 +1599,37 @@ public function testLazyWither()
15631599
$this->assertTrue($wither->resetLazyObject());
15641600
}
15651601

1602+
public function testLazyWitherNonShared()
1603+
{
1604+
$container = new ContainerBuilder();
1605+
$container->register(Foo::class);
1606+
1607+
$container
1608+
->register('wither', Wither::class)
1609+
->setShared(false)
1610+
->setLazy(true)
1611+
->setPublic(true)
1612+
->setAutowired(true);
1613+
1614+
$container->compile();
1615+
$dumper = new PhpDumper($container);
1616+
$dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Service_Wither_Lazy_Non_Shared']);
1617+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_wither_lazy_non_shared.php', $dump);
1618+
eval('?>'.$dump);
1619+
1620+
$container = new \Symfony_DI_PhpDumper_Service_Wither_Lazy_Non_Shared();
1621+
1622+
$wither1 = $container->get('wither');
1623+
$this->assertInstanceOf(Foo::class, $wither1->foo);
1624+
$this->assertTrue($wither1->resetLazyObject());
1625+
1626+
$wither2 = $container->get('wither');
1627+
$this->assertInstanceOf(Foo::class, $wither2->foo);
1628+
$this->assertTrue($wither2->resetLazyObject());
1629+
1630+
$this->assertNotSame($wither1, $wither2);
1631+
}
1632+
15661633
public function testWitherWithStaticReturnType()
15671634
{
15681635
$container = new ContainerBuilder();
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
4+
use Symfony\Component\DependencyInjection\ContainerInterface;
5+
use Symfony\Component\DependencyInjection\Container;
6+
use Symfony\Component\DependencyInjection\Exception\LogicException;
7+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
8+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
10+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
11+
12+
/**
13+
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
14+
*/
15+
class Symfony_DI_PhpDumper_Service_Non_Shared_Lazy extends Container
16+
{
17+
protected $parameters = [];
18+
19+
public function __construct()
20+
{
21+
$this->services = $this->privates = [];
22+
$this->methodMap = [
23+
'foo' => 'getFooService',
24+
];
25+
26+
$this->aliases = [];
27+
}
28+
29+
public function compile(): void
30+
{
31+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
32+
}
33+
34+
public function isCompiled(): bool
35+
{
36+
return true;
37+
}
38+
39+
protected function createProxy($class, \Closure $factory)
40+
{
41+
return $factory();
42+
}
43+
44+
/**
45+
* Gets the public 'foo' service.
46+
*
47+
* @return \Symfony\Component\DependencyInjection\Tests\Compiler\Foo
48+
*/
49+
protected static function getFooService($container, $lazyLoad = true)
50+
{
51+
$container->factories['foo'] ??= fn () => self::getFooService($container);
52+
53+
if (true === $lazyLoad) {
54+
return $container->createProxy('FooGhost80f7cfc', static fn () => \FooGhost80f7cfc::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy)));
55+
}
56+
57+
static $include = true;
58+
59+
if ($include) {
60+
include_once __DIR__.'/Fixtures/includes/foo_lazy.php';
61+
62+
$include = false;
63+
}
64+
65+
return $lazyLoad;
66+
}
67+
}
68+
69+
class FooGhost80f7cfc extends \Symfony\Component\DependencyInjection\Tests\Compiler\Foo implements \Symfony\Component\VarExporter\LazyObjectInterface
70+
{
71+
use \Symfony\Component\VarExporter\LazyGhostTrait;
72+
73+
private const LAZY_OBJECT_PROPERTY_SCOPES = [];
74+
}
75+
76+
// Help opcache.preload discover always-needed symbols
77+
class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);
78+
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class);
79+
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
4+
use Symfony\Component\DependencyInjection\ContainerInterface;
5+
use Symfony\Component\DependencyInjection\Container;
6+
use Symfony\Component\DependencyInjection\Exception\LogicException;
7+
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
8+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
10+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
11+
12+
/**
13+
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
14+
*/
15+
class Symfony_DI_PhpDumper_Service_Wither_Lazy_Non_Shared extends Container
16+
{
17+
protected $parameters = [];
18+
19+
public function __construct()
20+
{
21+
$this->services = $this->privates = [];
22+
$this->methodMap = [
23+
'wither' => 'getWitherService',
24+
];
25+
26+
$this->aliases = [];
27+
}
28+
29+
public function compile(): void
30+
{
31+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
32+
}
33+
34+
public function isCompiled(): bool
35+
{
36+
return true;
37+
}
38+
39+
public function getRemovedIds(): array
40+
{
41+
return [
42+
'Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo' => true,
43+
];
44+
}
45+
46+
protected function createProxy($class, \Closure $factory)
47+
{
48+
return $factory();
49+
}
50+
51+
/**
52+
* Gets the public 'wither' autowired service.
53+
*
54+
* @return \Symfony\Component\DependencyInjection\Tests\Compiler\Wither
55+
*/
56+
protected static function getWitherService($container, $lazyLoad = true)
57+
{
58+
$container->factories['wither'] ??= fn () => self::getWitherService($container);
59+
60+
if (true === $lazyLoad) {
61+
return $container->createProxy('WitherProxyDd381be', static fn () => \WitherProxyDd381be::createLazyProxy(static fn () => self::getWitherService($container, false)));
62+
}
63+
64+
$instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither();
65+
66+
$a = ($container->privates['Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo());
67+
68+
$instance = $instance->withFoo1($a);
69+
$instance = $instance->withFoo2($a);
70+
$instance->setFoo($a);
71+
72+
return $instance;
73+
}
74+
}
75+
76+
class WitherProxyDd381be extends \Symfony\Component\DependencyInjection\Tests\Compiler\Wither implements \Symfony\Component\VarExporter\LazyObjectInterface
77+
{
78+
use \Symfony\Component\VarExporter\LazyProxyTrait;
79+
80+
private const LAZY_OBJECT_PROPERTY_SCOPES = [
81+
'foo' => [parent::class, 'foo', null],
82+
];
83+
}
84+
85+
// Help opcache.preload discover always-needed symbols
86+
class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);
87+
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class);
88+
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);

0 commit comments

Comments
 (0)