8000 [DependencyInjection] fix unable to make lazy service from readonly class by kor3k · Pull Request #53876 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] fix unable to make lazy service from readonly class #53876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function getProxyCode(Definition $definition, ?string $id = null): string

if ($asGhostObject) {
try {
return 'class '.$proxyClass.ProxyHelper::generateLazyGhost($class);
return (\PHP_VERSION_ID >= 80200 && $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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

readonly class ReadonlyTest
{
public function say(): string
{
return 'hello';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\LazyServiceDumper;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ReadonlyTest;

class LazyServiceDumperTest extends TestCase
{
Expand Down Expand Up @@ -52,6 +53,18 @@ public function testInvalidClass()
$this->expectExceptionMessage('Invalid "proxy" tag for service "stdClass": class "stdClass" doesn\'t implement "Psr\Container\ContainerInterface".');
$dumper->getProxyCode($definition);
}

/**
* @requires PHP 8.2
*/
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
Expand Down
0