8000 bug #53876 [DependencyInjection] fix unable to make lazy service from… · symfony/symfony@84c231e · GitHub
[go: up one dir, main page]

Skip to content

Commit 84c231e

Browse files
bug #53876 [DependencyInjection] fix unable to make lazy service from readonly class (kor3k)
This PR was merged into the 6.4 branch. Discussion ---------- [DependencyInjection] fix unable to make lazy service from readonly class [DependencyInjection] unable to make lazy service from readonly class #53843 | Q | A | ------------- | --- | Branch? | 6.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #53843 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Commits ------- 5197b47 [DependencyInjection] fix unable to make lazy service from readonly class
2 parents cf78ca0 + 5197b47 commit 84c231e

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

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

+3-1
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

+1-1
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
}
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

+13
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