diff --git a/src/Symfony/Bridge/Doctrine/CHANGELOG.md b/src/Symfony/Bridge/Doctrine/CHANGELOG.md index f1133dfefe9a6..95f65e2dcca98 100644 --- a/src/Symfony/Bridge/Doctrine/CHANGELOG.md +++ b/src/Symfony/Bridge/Doctrine/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.3 +--- + + * Reset the manager registry using native lazy objects when applicable + 7.2 --- diff --git a/src/Symfony/Bridge/Doctrine/ManagerRegistry.php b/src/Symfony/Bridge/Doctrine/ManagerRegistry.php index 6a2c7a59542ef..e1d2e1528253e 100644 --- a/src/Symfony/Bridge/Doctrine/ManagerRegistry.php +++ b/src/Symfony/Bridge/Doctrine/ManagerRegistry.php @@ -45,31 +45,60 @@ protected function resetService($name): void return; } - if (!$manager instanceof LazyLoadingInterface) { - throw new \LogicException(\sprintf('Resetting a non-lazy manager service is not supported. Declare the "%s" service as lazy.', $name)); + if (\PHP_VERSION_ID < 80400) { + if (!$manager instanceof LazyLoadingInterface) { + throw new \LogicException(\sprintf('Resetting a non-lazy manager service is not supported. Declare the "%s" service as lazy.', $name)); + } + trigger_deprecation('symfony/doctrine-bridge', '7.3', 'Support for proxy-manager is deprecated.'); + + if ($manager instanceof GhostObjectInterface) { + throw new \LogicException('Resetting a lazy-ghost-object manager service is not supported.'); + } + $manager->setProxyInitializer(\Closure::bind( + function (&$wrappedInstance, LazyLoadingInterface $manager) use ($name) { + $name = $this->aliases[$name] ?? $name; + $wrappedInstance = match (true) { + isset($this->fileMap[$name]) => $this->load($this->fileMap[$name], false), + (new \ReflectionMethod($this, $method = $this->methodMap[$name]))->isStatic() => $this->{$method}($this, false), + default => $this->{$method}(false), + }; + $manager->setProxyInitializer(null); + + return true; + }, + $this->container, + Container::class + )); + + return; } - if ($manager instanceof GhostObjectInterface) { - throw new \LogicException('Resetting a lazy-ghost-object manager service is not supported.'); + + $r = new \ReflectionClass($manager); + + if ($r->isUninitializedLazyObject($manager)) { + return; + } + + try { + $r->resetAsLazyProxy($manager, \Closure::bind( + function () use ($name) { + $name = $this->aliases[$name] ?? $name; + + return match (true) { + isset($this->fileMap[$name]) => $this->load($this->fileMap[$name], false), + (new \ReflectionMethod($this, $method = $this->methodMap[$name]))->isStatic() => $this->{$method}($this, false), + default => $this->{$method}(false), + }; + }, + $this->container, + Container::class + )); + } catch (\Error $e) { + if (__FILE__ !== $e->getFile()) { + throw $e; + } + + throw new \LogicException(\sprintf('Resetting a non-lazy manager service is not supported. Declare the "%s" service as lazy.', $name), 0, $e); } - $manager->setProxyInitializer(\Closure::bind( - function (&$wrappedInstance, LazyLoadingInterface $manager) use ($name) { - if (isset($this->aliases[$name])) { - $name = $this->aliases[$name]; - } - if (isset($this->fileMap[$name])) { - $wrappedInstance = $this->load($this->fileMap[$name], false); - } elseif ((new \ReflectionMethod($this, $this->methodMap[$name]))->isStatic()) { - $wrappedInstance = $this->{$this->methodMap[$name]}($this, false); - } else { - $wrappedInstance = $this->{$this->methodMap[$name]}(false); - } - - $manager->setProxyInitializer(null); - - return true; - }, - $this->container, - Container::class - )); } } diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 0646d04bc39b6..0551fadb23e8a 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -9,6 +9,7 @@ CHANGELOG * Don't skip classes with private constructor when autodiscovering * Add `Definition::addExcludeTag()` and `ContainerBuilder::findExcludedServiceIds()` for auto-configuration of classes excluded from the service container + * Leverage native lazy objects when possible for lazy services 7.2 --- diff --git a/src/Symfony/Component/DependencyInjection/LazyProxy/Instantiator/LazyServiceInstantiator.php b/src/Symfony/Component/DependencyInjection/LazyProxy/Instantiator/LazyServiceInstantiator.php index f5e7dead566b9..10748256261e0 100644 --- a/src/Symfony/Component/DependencyInjection/LazyProxy/Instantiator/LazyServiceInstantiator.php +++ b/src/Symfony/Component/DependencyInjection/LazyProxy/Instantiator/LazyServiceInstantiator.php @@ -29,10 +29,19 @@ public function instantiateProxy(ContainerInterface $container, Definition $defi throw new InvalidArgumentException(\sprintf('Cannot instantiate lazy proxy for service "%s".', $id)); } - if (!class_exists($proxyClass = $dumper->getProxyClass($definition, $asGhostObject), false)) { + if (\PHP_VERSION_ID >= 80400 && $asGhostObject) { + return (new \ReflectionClass($definition->getClass()))->newLazyGhost(static function ($ghost) use ($realInstantiator) { $realInstantiator($ghost); }); + } + + $class = null; + if (!class_exists($proxyClass = $dumper->getProxyClass($definition, $asGhostObject, $class), false)) { eval($dumper->getProxyCode($definition, $id)); } - return $asGhostObject ? $proxyClass::createLazyGhost($realInstantiator) : $proxyClass::createLazyProxy($realInstantiator); + if ($definition->getClass() === $proxyClass) { + return $class->newLazyProxy($realInstantiator); + } + + return \PHP_VERSION_ID < 80400 && $asGhostObject ? $proxyClass::createLazyGhost($realInstantiator) : $proxyClass::createLazyProxy($realInstantiator); } } diff --git a/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php b/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php index b335fa37857e1..60c6a4d607c72 100644 --- a/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php +++ b/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php @@ -56,9 +56,21 @@ public function isProxyCandidate(Definition $definition, ?bool &$asGhostObject = } } + if (\PHP_VERSION_ID < 80400) { + try { + $asGhostObject = (bool) ProxyHelper::generateLazyGhost(new \ReflectionClass($class)); + } catch (LogicException) { + } + + return true; + } + try { - $asGhostObject = (bool) ProxyHelper::generateLazyGhost(new \ReflectionClass($class)); - } catch (LogicException) { + $asGhostObject = (bool) (new \ReflectionClass($class))->newLazyGhost(static fn () => null); + } catch (\Error $e) { + if (__FILE__ !== $e->getFile()) { + throw $e; + } } return true; @@ -76,6 +88,16 @@ public function getProxyFactoryCode(Definition $definition, string $id, string $ $proxyClass = $this->getProxyClass($definition, $asGhostObject); if (!$asGhostObject) { + if ($definition->getClass() === $proxyClass) { + return <<newLazyProxy(static fn () => $factoryCode); + } + + + EOF; + } + return <<createProxy('$proxyClass', static fn () => \\$proxyClass::createLazyProxy(static fn () => $factoryCode)); @@ -85,11 +107,23 @@ public function getProxyFactoryCode(Definition $definition, string $id, string $ EOF; } - $factoryCode = \sprintf('static fn ($proxy) => %s', $factoryCode); + if (\PHP_VERSION_ID < 80400) { + $factoryCode = \sprintf('static fn ($proxy) => %s', $factoryCode); + + return <<createProxy('$proxyClass', static fn () => \\$proxyClass::createLazyGhost($factoryCode)); + } + + + EOF; + } + + $factoryCode = \sprintf('static function ($proxy) use ($container) { %s; }', $factoryCode); return <<createProxy('$proxyClass', static fn () => \\$proxyClass::createLazyGhost($factoryCode)); + $instantiation new \ReflectionClass('$proxyClass')->newLazyGhost($factoryCode); } @@ -104,12 +138,21 @@ public function getProxyCode(Definition $definition, ?string $id = null): string $proxyClass = $this->getProxyClass($definition, $asGhostObject, $class); if ($asGhostObject) { + if (\PHP_VERSION_ID >= 80400) { + return ''; + } + try { return ($class?->isReadOnly() ? 'readonly ' : '').'class '.$proxyClass.ProxyHelper::generateLazyGhost($class); } catch (LogicException $e) { throw new InvalidArgumentException(\sprintf('Cannot generate lazy ghost for service "%s".', $id ?? $definition->getClass()), 0, $e); } } + + if ($definition->getClass() === $proxyClass) { + return ''; + } + $interfaces = []; if ($definition->hasTag('proxy')) { @@ -144,6 +187,16 @@ public function getProxyClass(Definition $definition, bool $asGhostObject, ?\Ref $class = 'object' !== $definition->getClass() ? $definition->getClass() : 'stdClass'; $class = new \ReflectionClass($class); + if (\PHP_VERSION_ID >= 80400) { + if ($asGhostObject) { + return $class->name; + } + + if (!$definition->hasTag('proxy') && !$class->isInterface()) { + return $class->name; + } + } + return preg_replace('/^.*\\\\/', '', $definition->getClass()) .($asGhostObject ? 'Ghost' : 'Proxy') .ucfirst(substr(hash('xxh128', $this->salt.'+'.$class->name.'+'.serialize($definition->getTag('proxy'))), -7)); diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php index 44837e36fee1a..304cd3e4dc5b1 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php @@ -1919,8 +1919,12 @@ public function testLazyWither() $container->compile(); $wither = $container->get('wither'); + if (\PHP_VERSION_ID >= 80400) { + $this->assertTrue((new \ReflectionClass($wither))->isUninitializedLazyObject($wither)); + } else { + $this->assertTrue($wither->resetLazyObject()); + } $this->assertInstanceOf(Foo::class, $wither->foo); - $this->assertTrue($wither->resetLazyObject()); $this->assertInstanceOf(Wither::class, $wither->withFoo1($wither->foo)); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php index fbdc28977a013..e4b5456dc535b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php @@ -340,7 +340,7 @@ public function testDumpAsFilesWithLazyFactoriesInlined() if ('\\' === \DIRECTORY_SEPARATOR) { $dump = str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump); } - $this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/services9_lazy_inlined_factories.txt', $dump); + $this->assertStringMatchesFormatFile(self::$fixturesPath.'/php/'.(\PHP_VERSION_ID < 80400 ? 'legacy_' : '').'services9_lazy_inlined_factories.txt', $dump); } public function testServicesWithAnonymousFactories() @@ -794,7 +794,7 @@ public function testNonSharedLazy() 'inline_class_loader' => false, ]); $this->assertStringEqualsFile( - self::$fixturesPath.'/php/services_non_shared_lazy_public.php', + self::$fixturesPath.'/php/'.(\PHP_VERSION_ID < 80400 ? 'legacy_' : '').'services_non_shared_lazy_public.php', '\\' === \DIRECTORY_SEPARATOR ? str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump) : $dump ); eval('?>'.$dump); @@ -802,10 +802,18 @@ public function testNonSharedLazy() $container = new \Symfony_DI_PhpDumper_Service_Non_Shared_Lazy(); $foo1 = $container->get('foo'); - $this->assertTrue($foo1->resetLazyObject()); + if (\PHP_VERSION_ID >= 80400) { + $this->assertTrue((new \ReflectionClass($foo1))->isUninitializedLazyObject($foo1)); + } else { + $this->assertTrue($foo1->resetLazyObject()); + } $foo2 = $container->get('foo'); - $this->assertTrue($foo2->resetLazyObject()); + if (\PHP_VERSION_ID >= 80400) { + $this->assertTrue((new \ReflectionClass($foo2))->isUninitializedLazyObject($foo2)); + } else { + $this->assertTrue($foo2->resetLazyObject()); + } $this->assertNotSame($foo1, $foo2); } @@ -832,7 +840,7 @@ public function testNonSharedLazyAsFiles() $stringDump = print_r($dumps, true); $this->assertStringMatchesFormatFile( - self::$fixturesPath.'/php/services_non_shared_lazy_as_files.txt', + self::$fixturesPath.'/php/'.(\PHP_VERSION_ID < 80400 ? 'legacy_' : '').'services_non_shared_lazy_as_files.txt', '\\' === \DIRECTORY_SEPARATOR ? str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $stringDump) : $stringDump ); @@ -844,10 +852,18 @@ public function testNonSharedLazyAsFiles() $container = eval('?>'.$lastDump); $foo1 = $container->get('non_shared_foo'); - $this->assertTrue($foo1->resetLazyObject()); + if (\PHP_VERSION_ID >= 80400) { + $this->assertTrue((new \ReflectionClass($foo1))->isUninitializedLazyObject($foo1)); + } else { + $this->assertTrue($foo1->resetLazyObject()); + } $foo2 = $container->get('non_shared_foo'); - $this->assertTrue($foo2->resetLazyObject()); + if (\PHP_VERSION_ID >= 80400) { + $this->assertTrue((new \ReflectionClass($foo2))->isUninitializedLazyObject($foo2)); + } else { + $this->assertTrue($foo2->resetLazyObject()); + } $this->assertNotSame($foo1, $foo2); } @@ -869,7 +885,7 @@ public function testNonSharedLazyDefinitionReferences(bool $asGhostObject) $dumper->setProxyDumper(new \DummyProxyDumper()); } - $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_non_shared_lazy'.($asGhostObject ? '_ghost' : '').'.php', $dumper->dump()); + $this->assertStringEqualsFile(self::$fixturesPath.'/php/'.(\PHP_VERSION_ID < 80400 ? 'legacy_' : '').'services_non_shared_lazy'.($asGhostObject ? '_ghost' : '').'.php', $dumper->dump()); } public function testNonSharedDuplicates() @@ -942,7 +958,7 @@ public function testDedupLazyProxy() $dumper = new PhpDumper($container); - $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_dedup_lazy.php', $dumper->dump()); + $this->assertStringEqualsFile(self::$fixturesPath.'/php/'.(\PHP_VERSION_ID < 80400 ? 'legacy_' : '').'services_dedup_lazy.php', $dumper->dump()); } public function testLazyArgumentProvideGenerator() @@ -1607,14 +1623,18 @@ public function testLazyWither() $container->compile(); $dumper = new PhpDumper($container); $dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Service_Wither_Lazy']); - $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_wither_lazy.php', $dump); + $this->assertStringEqualsFile(self::$fixturesPath.'/php/'.(\PHP_VERSION_ID < 80400 ? 'legacy_' : '').'services_wither_lazy.php', $dump); eval('?>'.$dump); $container = new \Symfony_DI_PhpDumper_Service_Wither_Lazy(); $wither = $container->get('wither'); + if (\PHP_VERSION_ID >= 80400) { + $this->assertTrue((new \ReflectionClass($wither))->isUninitializedLazyObject($wither)); + } else { + $this->assertTrue($wither->resetLazyObject()); + } $this->assertInstanceOf(Foo::class, $wither->foo); - $this->assertTrue($wither->resetLazyObject()); } public function testLazyWitherNonShared() @@ -1632,18 +1652,26 @@ public function testLazyWitherNonShared() $container->compile(); $dumper = new PhpDumper($container); $dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Service_Wither_Lazy_Non_Shared']); - $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_wither_lazy_non_shared.php', $dump); + $this->assertStringEqualsFile(self::$fixturesPath.'/php/'.(\PHP_VERSION_ID < 80400 ? 'legacy_' : '').'services_wither_lazy_non_shared.php', $dump); eval('?>'.$dump); $container = new \Symfony_DI_PhpDumper_Service_Wither_Lazy_Non_Shared(); $wither1 = $container->get('wither'); + if (\PHP_VERSION_ID >= 80400) { + $this->assertTrue((new \ReflectionClass($wither1))->isUninitializedLazyObject($wither1)); + } else { + $this->assertTrue($wither1->resetLazyObject()); + } $this->assertInstanceOf(Foo::class, $wither1->foo); - $this->assertTrue($wither1->resetLazyObject()); $wither2 = $container->get('wither'); + if (\PHP_VERSION_ID >= 80400) { + $this->assertTrue((new \ReflectionClass($wither2))->isUninitializedLazyObject($wither2)); + } else { + $this->assertTrue($wither2->resetLazyObject()); + } $this->assertInstanceOf(Foo::class, $wither2->foo); - $this->assertTrue($wither2->resetLazyObject()); $this->assertNotSame($wither1, $wither2); } @@ -1971,15 +1999,21 @@ public function testLazyAutowireAttribute() $container->compile(); $dumper = new PhpDumper($container); - $this->assertStringEqualsFile(self::$fixturesPath.'/php/lazy_autowire_attribute.php', $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_Lazy_Autowire_Attribute'])); + $this->assertStringEqualsFile(self::$fixturesPath.'/php/'.(\PHP_VERSION_ID < 80400 ? 'legacy_' : '').'lazy_autowire_attribute.php', $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_Lazy_Autowire_Attribute'])); - require self::$fixturesPath.'/php/lazy_autowire_attribute.php'; + require self::$fixturesPath.'/php/'.(\PHP_VERSION_ID < 80400 ? 'legacy_' : '').'lazy_autowire_attribute.php'; $container = new \Symfony_DI_PhpDumper_Test_Lazy_Autowire_Attribute(); $this->assertInstanceOf(Foo::class, $container->get('bar')->foo); - $this->assertInstanceOf(LazyObjectInterface::class, $container->get('bar')->foo); - $this->assertSame($container->get('foo'), $container->get('bar')->foo->initializeLazyObject()); + if (\PHP_VERSION_ID >= 80400) { + $r = new \ReflectionClass(Foo::class); + $this->assertTrue($r->isUninitializedLazyObject($container->get('bar')->foo)); + $this->assertSame($container->get('foo'), $r->initializeLazyObject($container->get('bar')->foo)); + } else { + $this->assertInstanceOf(LazyObjectInterface::class, $container->get('bar')->foo); + $this->assertSame($container->get('foo'), $container->get('bar')->foo->initializeLazyObject()); + } } public function testLazyAutowireAttributeWithIntersection() diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php index 7349cb1a076d8..d72d7b3aec63a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes.php @@ -14,6 +14,7 @@ class Foo { public static int $counter = 0; + public int $foo = 0; #[Required] public function cloneFoo(?\stdClass $bar = null): static diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/foo_lazy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/foo_lazy.php index 1caad4b2077d1..e150e09e4badf 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/foo_lazy.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/foo_lazy.php @@ -4,4 +4,5 @@ class FooLazyClass { + public int $foo = 0; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/lazy_autowire_attribute.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/lazy_autowire_attribute.php index 4f596a2b90597..97388c0efe57e 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/lazy_autowire_attribute.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/lazy_autowire_attribute.php @@ -77,21 +77,9 @@ protected static function getFooService($container) protected static function getFoo2Service($container, $lazyLoad = true) { if (true === $lazyLoad) { - return $container->privates['.lazy.Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] = $container->createProxy('FooProxyCd8d23a', static fn () => \FooProxyCd8d23a::createLazyProxy(static fn () => self::getFoo2Service($container, false))); + return $container->privates['.lazy.Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] = new \ReflectionClass('Symfony\Component\DependencyInjection\Tests\Compiler\Foo')->newLazyProxy(static fn () => self::getFoo2Service($container, false)); } return ($container->services['foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo()); } } - -class FooProxyCd8d23a extends \Symfony\Component\DependencyInjection\Tests\Compiler\Foo implements \Symfony\Component\VarExporter\LazyObjectInterface -{ - use \Symfony\Component\VarExporter\LazyProxyTrait; - - private const LAZY_OBJECT_PROPERTY_SCOPES = []; -} - -// Help opcache.preload discover always-needed symbols -class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_lazy_autowire_attribute.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_lazy_autowire_attribute.php new file mode 100644 index 0000000000000..6cf1c86a52ade --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_lazy_autowire_attribute.php @@ -0,0 +1,99 @@ +services = $this->privates = []; + $this->methodMap = [ + 'bar' => 'getBarService', + 'foo' => 'getFooService', + ]; + + $this->aliases = []; + } + + public function compile(): void + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled(): bool + { + return true; + } + + public function getRemovedIds(): array + { + return [ + 'Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo' => true, + ]; + } + + protected function createProxy($class, \Closure $factory) + { + return $factory(); + } + + /** + * Gets the public 'bar' shared autowired service. + * + * @return \Symfony\Component\DependencyInjection\Tests\Dumper\LazyServiceConsumer + */ + protected static function getBarService($container) + { + return $container->services['bar'] = new \Symfony\Component\DependencyInjection\Tests\Dumper\LazyServiceConsumer(($container->privates['.lazy.Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] ?? self::getFoo2Service($container))); + } + + /** + * Gets the public 'foo' shared service. + * + * @return \Symfony\Component\DependencyInjection\Tests\Compiler\Foo + */ + protected static function getFooService($container) + { + return $container->services['foo'] = new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo(); + } + + /** + * Gets the private '.lazy.Symfony\Component\DependencyInjection\Tests\Compiler\Foo' shared service. + * + * @return \Symfony\Component\DependencyInjection\Tests\Compiler\Foo + */ + protected static function getFoo2Service($container, $lazyLoad = true) + { + if (true === $lazyLoad) { + return $container->privates['.lazy.Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] = $container->createProxy('FooProxyCd8d23a', static fn () => \FooProxyCd8d23a::createLazyProxy(static fn () => self::getFoo2Service($container, false))); + } + + return ($container->services['foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo()); + } +} + +class FooProxyCd8d23a extends \Symfony\Component\DependencyInjection\Tests\Compiler\Foo implements \Symfony\Component\VarExporter\LazyObjectInterface +{ + use \Symfony\Component\VarExporter\LazyProxyTrait; + + private const LAZY_OBJECT_PROPERTY_SCOPES = [ + 'foo' => [parent::class, 'foo', null, 4], + ]; +} + +// Help opcache.preload discover always-needed symbols +class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services9_lazy_inlined_factories.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services9_lazy_inlined_factories.txt new file mode 100644 index 0000000000000..f945fdd50069b --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services9_lazy_inlined_factories.txt @@ -0,0 +1,196 @@ +Array +( + [Container%s/proxy-classes.php] => targetDir.''.'/Fixtures/includes/foo.php'; + +class FooClassGhost1728205 extends \Bar\FooClass implements \Symfony\Component\VarExporter\LazyObjectInterface +%A + +if (!\class_exists('FooClassGhost1728205', false)) { + \class_alias(__NAMESPACE__.'\\FooClassGhost1728205', 'FooClassGhost1728205', false); +} + + [Container%s/ProjectServiceContainer.php] => targetDir = \dirname($containerDir); + $this->parameters = $this->getDefaultParameters(); + + $this->services = $this->privates = []; + $this->methodMap = [ + 'lazy_foo' => 'getLazyFooService', + ]; + + $this->aliases = []; + + $this->privates['service_container'] = static function ($container) { + include_once __DIR__.'/proxy-classes.php'; + }; + } + + public function compile(): void + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled(): bool + { + return true; + } + + protected function createProxy($class, \Closure $factory) + { + return $factory(); + } + + /** + * Gets the public 'lazy_foo' shared service. + * + * @return \Bar\FooClass + */ + protected static function getLazyFooService($container, $lazyLoad = true) + { + if (true === $lazyLoad) { + return $container->services['lazy_foo'] = $container->createProxy('FooClassGhost1728205', static fn () => \FooClassGhost1728205::createLazyGhost(static fn ($proxy) => self::getLazyFooService($container, $proxy))); + } + + include_once $container->targetDir.''.'/Fixtures/includes/foo_lazy.php'; + + return ($lazyLoad->__construct(new \Bar\FooLazyClass()) && false ?: $lazyLoad); + } + + public function getParameter(string $name): array|bool|string|int|float|\UnitEnum|null + { + if (isset($this->buildParameters[$name])) { + return $this->buildParameters[$name]; + } + + if (!(isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters))) { + throw new ParameterNotFoundException($name); + } + + if (isset($this->loadedDynamicParameters[$name])) { + $value = $this->loadedDynamicParameters[$name] ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name); + } else { + $value = $this->parameters[$name]; + } + + return $value; + } + + public function hasParameter(string $name): bool + { + if (isset($this->buildParameters[$name])) { + return true; + } + + return isset($this->parameters[$name]) || isset($this->loadedDynamicParameters[$name]) || \array_key_exists($name, $this->parameters); + } + + public function setParameter(string $name, $value): void + { + throw new LogicException('Impossible to call set() on a frozen ParameterBag.'); + } + + public function getParameterBag(): ParameterBagInterface + { + if (!isset($this->parameterBag)) { + $parameters = $this->parameters; + foreach ($this->loadedDynamicParameters as $name => $loaded) { + $parameters[$name] = $loaded ? $this->dynamicParameters[$name] : $this->getDynamicParameter($name); + } + foreach ($this->buildParameters as $name => $value) { + $parameters[$name] = $value; + } + $this->parameterBag = new FrozenParameterBag($parameters, []); + } + + return $this->parameterBag; + } + + private $loadedDynamicParameters = []; + private $dynamicParameters = []; + + private function getDynamicParameter(string $name) + { + throw new ParameterNotFoundException($name); + } + + protected function getDefaultParameters(): array + { + return [ + 'lazy_foo_class' => 'Bar\\FooClass', + 'container.dumper.inline_factories' => true, + 'container.dumper.inline_class_loader' => true, + ]; + } +} + + [ProjectServiceContainer.preload.php] => = 7.4 when preloading is desired + +use Symfony\Component\DependencyInjection\Dumper\Preloader; + +if (in_array(PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) { + return; +} + +require dirname(__DIR__, %d).'%svendor/autoload.php'; +(require __DIR__.'/ProjectServiceContainer.php')->set(\Container%s\ProjectServiceContainer::class, null); + +$classes = []; +$classes[] = 'Bar\FooClass'; +$classes[] = 'Bar\FooLazyClass'; +$classes[] = 'Symfony\Component\DependencyInjection\ContainerInterface'; + +$preloaded = Preloader::preload($classes); + + [ProjectServiceContainer.php] => '%s', + 'container.build_id' => '%s', + 'container.build_time' => 1563381341, + 'container.runtime_mode' => \in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? 'web=0' : 'web=1', +], __DIR__.\DIRECTORY_SEPARATOR.'Container%s'); + +) diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_dedup_lazy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_dedup_lazy.php new file mode 100644 index 0000000000000..60add492ba1cd --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_dedup_lazy.php @@ -0,0 +1,126 @@ +services = $this->privates = []; + $this->methodMap = [ + 'bar' => 'getBarService', + 'baz' => 'getBazService', + 'buz' => 'getBuzService', + 'foo' => 'getFooService', + ]; + + $this->aliases = []; + } + + public function compile(): void + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled(): bool + { + return true; + } + + protected function createProxy($class, \Closure $factory) + { + return $factory(); + } + + /** + * Gets the public 'bar' shared service. + * + * @return \stdClass + */ + protected static function getBarService($container, $lazyLoad = true) + { + if (true === $lazyLoad) { + return $container->services['bar'] = $container->createProxy('stdClassGhostAa01f12', static fn () => \stdClassGhostAa01f12::createLazyGhost(static fn ($proxy) => self::getBarService($container, $proxy))); + } + + return $lazyLoad; + } + + /** + * Gets the public 'baz' shared service. + * + * @return \stdClass + */ + protected static function getBazService($container, $lazyLoad = true) + { + if (true === $lazyLoad) { + return $container->services['baz'] = $container->createProxy('stdClassProxyAa01f12', static fn () => \stdClassProxyAa01f12::createLazyProxy(static fn () => self::getBazService($container, false))); + } + + return \foo_bar(); + } + + /** + * Gets the public 'buz' shared service. + * + * @return \stdClass + */ + protected static function getBuzService($container, $lazyLoad = true) + { + if (true === $lazyLoad) { + return $container->services['buz'] = $container->createProxy('stdClassProxyAa01f12', static fn () => \stdClassProxyAa01f12::createLazyProxy(static fn () => self::getBuzService($container, false))); + } + + return \foo_bar(); + } + + /** + * Gets the public 'foo' shared service. + * + * @return \stdClass + */ + protected static function getFooService($container, $lazyLoad = true) + { + if (true === $lazyLoad) { + return $container->services['foo'] = $container->createProxy('stdClassGhostAa01f12', static fn () => \stdClassGhostAa01f12::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy))); + } + + return $lazyLoad; + } +} + +class stdClassGhostAa01f12 extends \stdClass implements \Symfony\Component\VarExporter\LazyObjectInterface +{ + use \Symfony\Component\VarExporter\LazyGhostTrait; + + private const LAZY_OBJECT_PROPERTY_SCOPES = []; +} + +// Help opcache.preload discover always-needed symbols +class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); + +class stdClassProxyAa01f12 extends \stdClass implements \Symfony\Component\VarExporter\LazyObjectInterface +{ + use \Symfony\Component\VarExporter\LazyProxyTrait; + + private const LAZY_OBJECT_PROPERTY_SCOPES = []; +} + +// Help opcache.preload discover always-needed symbols +class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy.php new file mode 100644 index 0000000000000..f584bef6b97cc --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy.php @@ -0,0 +1,76 @@ +services = $this->privates = []; + $this->methodMap = [ + 'bar' => 'getBarService', + ]; + + $this->aliases = []; + } + + public function compile(): void + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled(): bool + { + return true; + } + + public function getRemovedIds(): array + { + return [ + 'foo' => true, + ]; + } + + protected function createProxy($class, \Closure $factory) + { + return $factory(); + } + + /** + * Gets the public 'bar' shared service. + * + * @return \stdClass + */ + protected static function getBarService($container) + { + return $container->services['bar'] = new \stdClass((isset($container->factories['service_container']['foo']) ? $container->factories['service_container']['foo']($container) : self::getFooService($container))); + } + + /** + * Gets the private 'foo' service. + * + * @return \stdClass + */ + protected static function getFooService($container, $lazyLoad = true) + { + $container->factories['service_container']['foo'] ??= self::getFooService(...); + + // lazy factory for stdClass + + return new \stdClass(); + } +} + +// proxy code for stdClass diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy_as_files.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy_as_files.txt new file mode 100644 index 0000000000000..d52dd5a7b82ac --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy_as_files.txt @@ -0,0 +1,173 @@ +Array +( + [Container%s/getNonSharedFooService.php] => factories['non_shared_foo'] ??= fn () => self::do($container); + + if (true === $lazyLoad) { + return $container->createProxy('FooLazyClassGhost%s', static fn () => \FooLazyClassGhost%s::createLazyGhost(static fn ($proxy) => self::do($container, $proxy))); + } + + static $include = true; + + if ($include) { + include_once '%sfoo_lazy.php'; + + $include = false; + } + + return $lazyLoad; + } +} + + [Container%s/FooLazyClassGhost%s.php] => [parent::class, 'foo', null, 4], + ]; +} + +// Help opcache.preload discover always-needed symbols +class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); + +if (!\class_exists('FooLazyClassGhost%s', false)) { + \class_alias(__NAMESPACE__.'\\FooLazyClassGhost%s', 'FooLazyClassGhost%s', false); +} + + [Container%s/Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File.php] => services = $this->privates = []; + $this->fileMap = [ + 'non_shared_foo' => 'getNonSharedFooService', + ]; + + $this->aliases = []; + } + + public function compile(): void + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled(): bool + { + return true; + } + + protected function load($file, $lazyLoad = true): mixed + { + if (class_exists($class = __NAMESPACE__.'\\'.$file, false)) { + return $class::do($this, $lazyLoad); + } + + if ('.' === $file[-4]) { + $class = substr($class, 0, -4); + } else { + $file .= '.php'; + } + + $service = require $this->containerDir.\DIRECTORY_SEPARATOR.$file; + + return class_exists($class, false) ? $class::do($this, $lazyLoad) : $service; + } + + protected function createProxy($class, \Closure $factory) + { + class_exists($class, false) || require __DIR__.'/'.$class.'.php'; + + return $factory(); + } +} + + [Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File.preload.php] => = 7.4 when preloading is desired + +use Symfony\Component\DependencyInjection\Dumper\Preloader; + +if (in_array(PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) { + return; +} + +require '%svendor/autoload.php'; +(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); +require __DIR__.'/Container%s/FooLazyClassGhost%s.php'; +require __DIR__.'/Container%s/getNonSharedFooService.php'; + +$classes = []; +$classes[] = 'Bar\FooLazyClass'; +$classes[] = 'Symfony\Component\DependencyInjection\ContainerInterface'; + +$preloaded = Preloader::preload($classes); + + [Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File.php] => '%s', + 'container.build_id' => '%s', + 'container.build_time' => %d, + 'container.runtime_mode' => \in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? 'web=0' : 'web=1', +], __DIR__.\DIRECTORY_SEPARATOR.'Container%s'); + +) diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy_ghost.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy_ghost.php new file mode 100644 index 0000000000000..b03463295309e --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy_ghost.php @@ -0,0 +1,88 @@ +services = $this->privates = []; + $this->methodMap = [ + 'bar' => 'getBarService', + ]; + + $this->aliases = []; + } + + public function compile(): void + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled(): bool + { + return true; + } + + public function getRemovedIds(): array + { + return [ + 'foo' => true, + ]; + } + + protected function createProxy($class, \Closure $factory) + { + return $factory(); + } + + /** + * Gets the public 'bar' shared service. + * + * @return \stdClass + */ + protected static function getBarService($container) + { + return $container->services['bar'] = new \stdClass((isset($container->factories['service_container']['foo']) ? $container->factories['service_container']['foo']($container) : self::getFooService($container))); + } + + /** + * Gets the private 'foo' service. + * + * @return \stdClass + */ + protected static function getFooService($container, $lazyLoad = true) + { + $container->factories['service_container']['foo'] ??= self::getFooService(...); + + if (true === $lazyLoad) { + return $container->createProxy('stdClassGhostAa01f12', static fn () => \stdClassGhostAa01f12::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy))); + } + + return $lazyLoad; + } +} + +class stdClassGhostAa01f12 extends \stdClass implements \Symfony\Component\VarExporter\LazyObjectInterface +{ + use \Symfony\Component\VarExporter\LazyGhostTrait; + + private const LAZY_OBJECT_PROPERTY_SCOPES = []; +} + +// Help opcache.preload discover always-needed symbols +class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy_public.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy_public.php new file mode 100644 index 0000000000000..0841cf192ef59 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_non_shared_lazy_public.php @@ -0,0 +1,81 @@ +services = $this->privates = []; + $this->methodMap = [ + 'foo' => 'getFooService', + ]; + + $this->aliases = []; + } + + public function compile(): void + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled(): bool + { + return true; + } + + protected function createProxy($class, \Closure $factory) + { + return $factory(); + } + + /** + * Gets the public 'foo' service. + * + * @return \Bar\FooLazyClass + */ + protected static function getFooService($container, $lazyLoad = true) + { + $container->factories['foo'] ??= fn () => self::getFooService($container); + + if (true === $lazyLoad) { + return $container->createProxy('FooLazyClassGhost82ad1a4', static fn () => \FooLazyClassGhost82ad1a4::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy))); + } + + static $include = true; + + if ($include) { + include_once __DIR__.'/Fixtures/includes/foo_lazy.php'; + + $include = false; + } + + return $lazyLoad; + } +} + +class FooLazyClassGhost82ad1a4 extends \Bar\FooLazyClass implements \Symfony\Component\VarExporter\LazyObjectInterface +{ + use \Symfony\Component\VarExporter\LazyGhostTrait; + + private const LAZY_OBJECT_PROPERTY_SCOPES = [ + 'foo' => [parent::class, 'foo', null, 4], + ]; +} + +// Help opcache.preload discover always-needed symbols +class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_wither_lazy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_wither_lazy.php new file mode 100644 index 0000000000000..b9e9164573672 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_wither_lazy.php @@ -0,0 +1,86 @@ +services = $this->privates = []; + $this->methodMap = [ + 'wither' => 'getWitherService', + ]; + + $this->aliases = []; + } + + public function compile(): void + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled(): bool + { + return true; + } + + public function getRemovedIds(): array + { + return [ + 'Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo' => true, + ]; + } + + protected function createProxy($class, \Closure $factory) + { + return $factory(); + } + + /** + * Gets the public 'wither' shared autowired service. + * + * @return \Symfony\Component\DependencyInjection\Tests\Compiler\Wither + */ + protected static function getWitherService($container, $lazyLoad = true) + { + if (true === $lazyLoad) { + return $container->services['wither'] = $container->createProxy('WitherProxy1991f2a', static fn () => \WitherProxy1991f2a::createLazyProxy(static fn () => self::getWitherService($container, false))); + } + + $instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither(); + + $a = ($container->privates['Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo()); + + $instance = $instance->withFoo1($a); + $instance = $instance->withFoo2($a); + $instance->setFoo($a); + + return $instance; + } +} + +class WitherProxy1991f2a extends \Symfony\Component\DependencyInjection\Tests\Compiler\Wither implements \Symfony\Component\VarExporter\LazyObjectInterface +{ + use \Symfony\Component\VarExporter\LazyProxyTrait; + + private const LAZY_OBJECT_PROPERTY_SCOPES = [ + 'foo' => [parent::class, 'foo', null, 4], + ]; +} + +// Help opcache.preload discover always-needed symbols +class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_wither_lazy_non_shared.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_wither_lazy_non_shared.php new file mode 100644 index 0000000000000..d70588f655329 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/legacy_services_wither_lazy_non_shared.php @@ -0,0 +1,88 @@ +services = $this->privates = []; + $this->methodMap = [ + 'wither' => 'getWitherService', + ]; + + $this->aliases = []; + } + + public function compile(): void + { + throw new LogicException('You cannot compile a dumped container that was already compiled.'); + } + + public function isCompiled(): bool + { + return true; + } + + public function getRemovedIds(): array + { + return [ + 'Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo' => true, + ]; + } + + protected function createProxy($class, \Closure $factory) + { + return $factory(); + } + + /** + * Gets the public 'wither' autowired service. + * + * @return \Symfony\Component\DependencyInjection\Tests\Compiler\Wither + */ + protected static function getWitherService($container, $lazyLoad = true) + { + $container->factories['wither'] ??= fn () => self::getWitherService($container); + + if (true === $lazyLoad) { + return $container->createProxy('WitherProxyE94fdba', static fn () => \WitherProxyE94fdba::createLazyProxy(static fn () => self::getWitherService($container, false))); + } + + $instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither(); + + $a = ($container->privates['Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo()); + + $instance = $instance->withFoo1($a); + $instance = $instance->withFoo2($a); + $instance->setFoo($a); + + return $instance; + } +} + +class WitherProxyE94fdba extends \Symfony\Component\DependencyInjection\Tests\Compiler\Wither implements \Symfony\Component\VarExporter\LazyObjectInterface +{ + use \Symfony\Component\VarExporter\LazyProxyTrait; + + private const LAZY_OBJECT_PROPERTY_SCOPES = [ + 'foo' => [parent::class, 'foo', null, 4], + ]; +} + +// Help opcache.preload discover always-needed symbols +class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); +class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_lazy_inlined_factories.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_lazy_inlined_factories.txt index f945fdd50069b..9e6a3865f3605 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_lazy_inlined_factories.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_lazy_inlined_factories.txt @@ -1,18 +1,5 @@ Array ( - [Container%s/proxy-classes.php] => targetDir.''.'/Fixtures/includes/foo.php'; - -class FooClassGhost1728205 extends \Bar\FooClass implements \Symfony\Component\VarExporter\LazyObjectInterface -%A - -if (!\class_exists('FooClassGhost1728205', false)) { - \class_alias(__NAMESPACE__.'\\FooClassGhost1728205', 'FooClassGhost1728205', false); -} - [Container%s/ProjectServiceContainer.php] => aliases = []; - - $this->privates['service_container'] = static function ($container) { - include_once __DIR__.'/proxy-classes.php'; - }; } public function compile(): void @@ -74,9 +57,10 @@ class ProjectServiceContainer extends Container protected static function getLazyFooService($container, $lazyLoad = true) { if (true === $lazyLoad) { - return $container->services['lazy_foo'] = $container->createProxy('FooClassGhost1728205', static fn () => \FooClassGhost1728205::createLazyGhost(static fn ($proxy) => self::getLazyFooService($container, $proxy))); + return $container->services['lazy_foo'] = new \ReflectionClass('Bar\FooClass')->newLazyGhost(static function ($proxy) use ($container) { self::getLazyFooService($container, $proxy); }); } + include_once $container->targetDir.''.'/Fixtures/includes/foo.php'; include_once $container->targetDir.''.'/Fixtures/includes/foo_lazy.php'; return ($lazyLoad->__construct(new \Bar\FooLazyClass()) && false ?: $lazyLoad); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_dedup_lazy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_dedup_lazy.php index 60add492ba1cd..413a883e5c644 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_dedup_lazy.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_dedup_lazy.php @@ -52,7 +52,7 @@ protected function createProxy($class, \Closure $factory) protected static function getBarService($container, $lazyLoad = true) { if (true === $lazyLoad) { - return $container->services['bar'] = $container->createProxy('stdClassGhostAa01f12', static fn () => \stdClassGhostAa01f12::createLazyGhost(static fn ($proxy) => self::getBarService($container, $proxy))); + return $container->services['bar'] = new \ReflectionClass('stdClass')->newLazyGhost(static function ($proxy) use ($container) { self::getBarService($container, $proxy); }); } return $lazyLoad; @@ -66,7 +66,7 @@ protected static function getBarService($container, $lazyLoad = true) protected static function getBazService($container, $lazyLoad = true) { if (true === $lazyLoad) { - return $container->services['baz'] = $container->createProxy('stdClassProxyAa01f12', static fn () => \stdClassProxyAa01f12::createLazyProxy(static fn () => self::getBazService($container, false))); + return $container->services['baz'] = new \ReflectionClass('stdClass')->newLazyProxy(static fn () => self::getBazService($container, false)); } return \foo_bar(); @@ -80,7 +80,7 @@ protected static function getBazService($container, $lazyLoad = true) protected static function getBuzService($container, $lazyLoad = true) { if (true === $lazyLoad) { - return $container->services['buz'] = $container->createProxy('stdClassProxyAa01f12', static fn () => \stdClassProxyAa01f12::createLazyProxy(static fn () => self::getBuzService($container, false))); + return $container->services['buz'] = new \ReflectionClass('stdClass')->newLazyProxy(static fn () => self::getBuzService($container, false)); } return \foo_bar(); @@ -94,33 +94,9 @@ protected static function getBuzService($container, $lazyLoad = true) protected static function getFooService($container, $lazyLoad = true) { if (true === $lazyLoad) { - return $container->services['foo'] = $container->createProxy('stdClassGhostAa01f12', static fn () => \stdClassGhostAa01f12::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy))); + return $container->services['foo'] = new \ReflectionClass('stdClass')->newLazyGhost(static function ($proxy) use ($container) { self::getFooService($container, $proxy); }); } return $lazyLoad; } } - -class stdClassGhostAa01f12 extends \stdClass implements \Symfony\Component\VarExporter\LazyObjectInterface -{ - use \Symfony\Component\VarExporter\LazyGhostTrait; - - private const LAZY_OBJECT_PROPERTY_SCOPES = []; -} - -// Help opcache.preload discover always-needed symbols -class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); - -class stdClassProxyAa01f12 extends \stdClass implements \Symfony\Component\VarExporter\LazyObjectInterface -{ - use \Symfony\Component\VarExporter\LazyProxyTrait; - - private const LAZY_OBJECT_PROPERTY_SCOPES = []; -} - -// Help opcache.preload discover always-needed symbols -class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt index 488895d7c1b6e..69a47220cba88 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_as_files.txt @@ -23,7 +23,7 @@ class getNonSharedFooService extends Symfony_DI_PhpDumper_Service_Non_Shared_Laz $container->factories['non_shared_foo'] ??= fn () => self::do($container); if (true === $lazyLoad) { - return $container->createProxy('FooLazyClassGhost%s', static fn () => \FooLazyClassGhost%s::createLazyGhost(static fn ($proxy) => self::do($container, $proxy))); + return new \ReflectionClass('Bar\FooLazyClass')->newLazyGhost(static function ($proxy) use ($container) { self::do($container, $proxy); }); } static $include = true; @@ -38,26 +38,6 @@ class getNonSharedFooService extends Symfony_DI_PhpDumper_Service_Non_Shared_Laz } } - [Container%s/FooLazyClassGhost%s.php] => set(\Container%s\Symfony_DI_PhpDumper_Service_Non_Shared_Lazy_As_File::class, null); -require __DIR__.'/Container%s/FooLazyClassGhost%s.php'; require __DIR__.'/Container%s/getNonSharedFooService.php'; $classes = []; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_ghost.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_ghost.php index b03463295309e..281baf61514b2 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_ghost.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_ghost.php @@ -68,21 +68,9 @@ protected static function getFooService($container, $lazyLoad = true) $container->factories['service_container']['foo'] ??= self::getFooService(...); if (true === $lazyLoad) { - return $container->createProxy('stdClassGhostAa01f12', static fn () => \stdClassGhostAa01f12::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy))); + return new \ReflectionClass('stdClass')->newLazyGhost(static function ($proxy) use ($container) { self::getFooService($container, $proxy); }); } return $lazyLoad; } } - -class stdClassGhostAa01f12 extends \stdClass implements \Symfony\Component\VarExporter\LazyObjectInterface -{ - use \Symfony\Component\VarExporter\LazyGhostTrait; - - private const LAZY_OBJECT_PROPERTY_SCOPES = []; -} - -// Help opcache.preload discover always-needed symbols -class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_public.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_public.php index 7f870f886abcb..93b7ee3ed685b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_public.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy_public.php @@ -51,7 +51,7 @@ protected static function getFooService($container, $lazyLoad = true) $container->factories['foo'] ??= fn () => self::getFooService($container); if (true === $lazyLoad) { - return $container->createProxy('FooLazyClassGhost82ad1a4', static fn () => \FooLazyClassGhost82ad1a4::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy))); + return new \ReflectionClass('Bar\FooLazyClass')->newLazyGhost(static function ($proxy) use ($container) { self::getFooService($container, $proxy); }); } static $include = true; @@ -65,15 +65,3 @@ protected static function getFooService($container, $lazyLoad = true) return $lazyLoad; } } - -class FooLazyClassGhost82ad1a4 extends \Bar\FooLazyClass implements \Symfony\Component\VarExporter\LazyObjectInterface -{ - use \Symfony\Component\VarExporter\LazyGhostTrait; - - private const LAZY_OBJECT_PROPERTY_SCOPES = []; -} - -// Help opcache.preload discover always-needed symbols -class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy.php index b9e9164573672..76031f1cae33a 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy.php @@ -56,7 +56,7 @@ protected function createProxy($class, \Closure $factory) protected static function getWitherService($container, $lazyLoad = true) { if (true === $lazyLoad) { - return $container->services['wither'] = $container->createProxy('WitherProxy1991f2a', static fn () => \WitherProxy1991f2a::createLazyProxy(static fn () => self::getWitherService($container, false))); + return $container->services['wither'] = new \ReflectionClass('Symfony\Component\DependencyInjection\Tests\Compiler\Wither')->newLazyProxy(static fn () => self::getWitherService($container, false)); } $instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither(); @@ -70,17 +70,3 @@ protected static function getWitherService($container, $lazyLoad = true) return $instance; } } - -class WitherProxy1991f2a extends \Symfony\Component\DependencyInjection\Tests\Compiler\Wither implements \Symfony\Component\VarExporter\LazyObjectInterface -{ - use \Symfony\Component\VarExporter\LazyProxyTrait; - - private const LAZY_OBJECT_PROPERTY_SCOPES = [ - 'foo' => [parent::class, 'foo', null, 4], - ]; -} - -// Help opcache.preload discover always-needed symbols -class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy_non_shared.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy_non_shared.php index d70588f655329..640955a7e555b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy_non_shared.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_wither_lazy_non_shared.php @@ -58,7 +58,7 @@ protected static function getWitherService($container, $lazyLoad = true) $container->factories['wither'] ??= fn () => self::getWitherService($container); if (true === $lazyLoad) { - return $container->createProxy('WitherProxyE94fdba', static fn () => \WitherProxyE94fdba::createLazyProxy(static fn () => self::getWitherService($container, false))); + return new \ReflectionClass('Symfony\Component\DependencyInjection\Tests\Compiler\Wither')->newLazyProxy(static fn () => self::getWitherService($container, false)); } $instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither(); @@ -72,17 +72,3 @@ protected static function getWitherService($container, $lazyLoad = true) return $instance; } } - -class WitherProxyE94fdba extends \Symfony\Component\DependencyInjection\Tests\Compiler\Wither implements \Symfony\Component\VarExporter\LazyObjectInterface -{ - use \Symfony\Component\VarExporter\LazyProxyTrait; - - private const LAZY_OBJECT_PROPERTY_SCOPES = [ - 'foo' => [parent::class, 'foo', null, 4], - ]; -} - -// Help opcache.preload discover always-needed symbols -class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class); -class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class); diff --git a/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/LazyServiceDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/LazyServiceDumperTest.php index 467972a882c78..14d2f81b673ec 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/LazyServiceDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/LazyServiceDumperTest.php @@ -63,7 +63,7 @@ public function testReadonlyClass() $definition = (new Definition(ReadOnlyClass::class))->setLazy(true); $this->assertTrue($dumper->isProxyCandidate($definition)); - $this->assertStringContainsString('readonly class ReadOnlyClassGhost', $dumper->getProxyCode($definition)); + $this->assertStringContainsString(\PHP_VERSION_ID >= 80400 ? '' : 'readonly class ReadOnlyClassGhost', $dumper->getProxyCode($definition)); } }