Skip to content

Commit

Permalink
fix: Proxy generation under opcache and/or classmap-authoritative
Browse files Browse the repository at this point in the history
* fix: Proxy generation under `opcache` and/or `classmap-authoritative`
  • Loading branch information
priyadi committed Mar 4, 2024
1 parent c1c3bf0 commit 6f87e4a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 5 additions & 1 deletion src/Proxy/Implementation/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 1 addition & 5 deletions src/Proxy/Implementation/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand All @@ -62,10 +62,6 @@ private function generateProxySourceCode(string $realClass, string $proxyClass):
private function getClassHeader(): string
{
return <<<'PHP'
<?php
declare(strict_types=1);
/*
* This is a proxy class automatically generated by the rekalogika/mapper
* package. Do not edit it manually.
Expand Down
5 changes: 5 additions & 0 deletions src/Proxy/Implementation/ProxyRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function registerProxy(string $class, string $sourceCode): void
self::getProxyFileName($class)
);

$sourceCode = sprintf(
'<?php declare(strict_types=1);' . "\n\n" . '%s',
$sourceCode
);

file_put_contents($proxyFile, $sourceCode);
}

Expand Down

0 comments on commit 6f87e4a

Please sign in to comment.