From db793d4ac580f1750209f25904ab540d28616d97 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Mon, 4 Mar 2024 11:37:56 +0700 Subject: [PATCH] fix: Proxy generation under `opcache` and/or `classmap-authoritative` --- CHANGELOG.md | 1 + composer.json | 1 + src/Proxy/Implementation/ProxyFactory.php | 6 +++++- src/Proxy/Implementation/ProxyGenerator.php | 6 +----- src/Proxy/Implementation/ProxyRegistry.php | 5 +++++ 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8cceb18..a62b3d3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 1.1.1 * fix: Fix missing `kernel.reset` tags. +* fix: Proxy generation under `opcache` and/or `classmap-authoritative` ## 1.1.0 diff --git a/composer.json b/composer.json index fffa9489..11a972c1 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "symfony/property-access": "^6.4 || ^7.0", "symfony/property-info": "^6.4 || ^7.0", "symfony/serializer": "^6.4 || ^7.0", + "symfony/service-contracts": "^3.0", "symfony/var-exporter": "^6.4.1 || ^6.5 || ^7.0.1 || ^7.1" }, "require-dev": { diff --git a/src/Proxy/Implementation/ProxyFactory.php b/src/Proxy/Implementation/ProxyFactory.php index 6b0add38..2d7db932 100644 --- a/src/Proxy/Implementation/ProxyFactory.php +++ b/src/Proxy/Implementation/ProxyFactory.php @@ -44,11 +44,15 @@ public function createProxy( ): object { $targetProxyClass = ProxyNamer::generateProxyClassName($class); - if (!class_exists($targetProxyClass, false)) { + if (!class_exists($targetProxyClass)) { $sourceCode = $this->proxyGenerator ->generateProxyCode($class, $targetProxyClass); $this->proxyRegistry->registerProxy($targetProxyClass, $sourceCode); + // @phpstan-ignore-next-line + eval($sourceCode); + + // @phpstan-ignore-next-line if (!class_exists($targetProxyClass)) { throw new LogicException( sprintf('Unable to find target proxy class "%s".', $targetProxyClass), diff --git a/src/Proxy/Implementation/ProxyGenerator.php b/src/Proxy/Implementation/ProxyGenerator.php index d1539748..66735ec0 100644 --- a/src/Proxy/Implementation/ProxyGenerator.php +++ b/src/Proxy/Implementation/ProxyGenerator.php @@ -53,7 +53,7 @@ private function generateProxySourceCode(string $realClass, string $proxyClass): sprintf('namespace %s;', $namespace) . "\n\n" . sprintf( 'final %sclass %s%s', - $targetReflection->isReadOnly() ? 'readonly ' : ' ', + $targetReflection->isReadOnly() ? 'readonly ' : '', $shortName, ProxyHelper::generateLazyGhost($targetReflection) ); @@ -62,10 +62,6 @@ private function generateProxySourceCode(string $realClass, string $proxyClass): private function getClassHeader(): string { return <<<'PHP' -