-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Disallow proxy for objects with dynamic properties, including `s…
…tdClass`.
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/Proxy/Implementation/DynamicPropertiesProxyGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of rekalogika/mapper package. | ||
* | ||
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE file | ||
* that was distributed with this source code. | ||
*/ | ||
|
||
namespace Rekalogika\Mapper\Proxy\Implementation; | ||
|
||
use Rekalogika\Mapper\Proxy\Exception\ProxyNotSupportedException; | ||
use Rekalogika\Mapper\Proxy\ProxyGeneratorInterface; | ||
|
||
/** | ||
* Prevent proxy creation for objects with dynamic properties. | ||
* | ||
* @internal | ||
*/ | ||
final readonly class DynamicPropertiesProxyGenerator implements ProxyGeneratorInterface | ||
{ | ||
public function __construct( | ||
private ProxyGeneratorInterface $decorated, | ||
) { | ||
} | ||
|
||
public function generateProxyCode(string $realClass, string $proxyClass): string | ||
{ | ||
$reflection = new \ReflectionClass($realClass); | ||
|
||
do { | ||
if ($reflection->getAttributes(\AllowDynamicProperties::class)) { | ||
throw new ProxyNotSupportedException($realClass, reason: 'Objects with dynamic properties do not support proxying.'); | ||
} | ||
} while ($reflection = $reflection->getParentClass()); | ||
|
||
|
||
return $this->decorated->generateProxyCode($realClass, $proxyClass); | ||
} | ||
} |