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

Skip to content

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

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -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";
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 ($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
Expand Up @@ -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
Expand All @@ -66,3 +75,11 @@ public function get(string $key): string
return $key;
}
}

readonly class ReadonlyTest
{
public function say(): string
{
return 'hello';
}
}
0