diff --git a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php index 55db19552568e..adb05e76465da 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php @@ -609,7 +609,9 @@ private function generateProxyClasses(): array $proxyCode = substr(self::stripComments($proxyCode), 5); } - $proxyClass = explode(' ', $this->inlineRequires ? substr($proxyCode, \strlen($code)) : $proxyCode, 3)[1]; + $proxyClass = $this->inlineRequires ? substr($proxyCode, \strlen($code)) : $proxyCode; + $i = strpos($proxyClass, 'class'); + $proxyClass = substr($proxyClass, 6 + $i, strpos($proxyClass, ' ', 7 + $i) - $i - 6); if ($this->asFiles || $this->namespace) { $proxyCode .= "\nif (!\\class_exists('$proxyClass', false)) {\n \\class_alias(__NAMESPACE__.'\\\\$proxyClass', '$proxyClass', false);\n}\n"; diff --git a/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php b/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php index 7e8d7db84c041..251819a978098 100644 --- a/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php +++ b/src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php @@ -105,7 +105,7 @@ public function getProxyCode(Definition $definition, ?string $id = null): string if ($asGhostObject) { try { - return 'class '.$proxyClass.ProxyHelper::generateLazyGhost($class); + 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); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/LazyServiceDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/LazyServiceDumperTest.php index 064bfc3cc82aa..2b2a3f2853e2f 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/LazyServiceDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/LazyServiceDumperTest.php @@ -52,6 +52,15 @@ public function testInvalidClass() $this->expectExceptionMessage('Invalid "proxy" tag for service "stdClass": class "stdClass" doesn\'t implement "Psr\Container\ContainerInterface".'); $dumper->getProxyCode($definition); } + + public function testReadonlyClass() + { + $dumper = new LazyServiceDumper(); + $definition = (new Definition(ReadonlyTest::class))->setLazy(true); + + $this->assertTrue($dumper->isProxyCandidate($definition)); + $this->assertStringContainsString('readonly class ReadonlyTestGhost', $dumper->getProxyCode($definition)); + } } final class TestContainer implements ContainerInterface @@ -66,3 +75,11 @@ public function get(string $key): string return $key; } } + +readonly class ReadonlyTest +{ + public function say(): string + { + return 'hello'; + } +}