8000 [DependencyInjection] fix unable to make lazy service from readonly c… · symfony/symfony@5197b47 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5197b47

Browse files
kor3knicolas-grekas
authored andcommitted
[DependencyInjection] fix unable to make lazy service from readonly class
1 parent fa843ce commit 5197b47

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,9 @@ private function generateProxyClasses(): array
624624
$proxyCode = substr(self::stripComments($proxyCode), 5);
625625
}
626626

627-
$proxyClass = explode(' ', $this->inlineRequires ? substr($proxyCode, \strlen($code)) : $proxyCode, 3)[1];
627+
$proxyClass = $this->inlineRequires ? substr($proxyCode, \strlen($code)) : $proxyCode;
628+
$i = strpos($proxyClass, 'class');
629+
$proxyClass = substr($proxyClass, 6 + $i, strpos($proxyClass, ' ', 7 + $i) - $i - 6);
628630

629631
if ($this->asFiles || $this->namespace) {
630632
$proxyCode .= "\nif (!\\class_exists('$proxyClass', false)) {\n \\class_alias(__NAMESPACE__.'\\\\$proxyClass', '$proxyClass', false);\n}\n";

src/Symfony/Component/DependencyInjection/LazyProxy/PhpDumper/LazyServiceDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function getProxyCode(Definition $definition, ?string $id = null): string
105105

106106
if ($asGhostObject) {
107107
try {
108-
return 'class '.$proxyClass.ProxyHelper::generateLazyGhost($class);
108+
return (\PHP_VERSION_ID >= 80200 && $class?->isReadOnly() ? 'readonly ' : '').'class '.$proxyClass.ProxyHelper::generateLazyGhost($class);
109109
} catch (LogicException $e) {
110110
throw new InvalidArgumentException(sprintf('Cannot generate lazy ghost for service "%s".', $id ?? $definition->getClass()), 0, $e);
111111
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
readonly class ReadonlyTest
15+
{
16+
public function say(): string
17+
{
18+
return 'hello';
19+
}
20+
}

src/Symfony/Component/DependencyInjection/Tests/LazyProxy/PhpDumper/LazyServiceDumperTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\DependencyInjection\Definition;
1717
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1818
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\LazyServiceDumper;
19+
use Symfony\Component\DependencyInjection\Tests\Fixtures\ReadonlyTest;
1920

2021
class LazyServiceDumperTest extends TestCase
2122
{
@@ -52,6 +53,18 @@ public function testInvalidClass()
5253
$this->expectExceptionMessage('Invalid "proxy" tag for service "stdClass": class "stdClass" doesn\'t implement "Psr\Container\ContainerInterface".');
5354
$dumper->getProxyCode($definition);
5455
}
56+
57+
/**
58+
* @requires PHP 8.2
59+
*/
60+
public function testReadonlyClass()
61+
{
62+
$dumper = new LazyServiceDumper();
63+
$definition = (new Definition(ReadonlyTest::class))->setLazy(true);
64+
65+
$this->assertTrue($dumper->isProxyCandidate($definition));
66+
$this->assertStringContainsString('readonly class ReadonlyTestGhost', $dumper->getProxyCode($definition));
67+
}
5568
}
5669

5770
final class TestContainer implements ContainerInterface

0 commit comments

Comments
 (0)
0