Skip to content

Commit

Permalink
Merge pull request #26 from rekalogika:fix/disable-proxy-for-stdclass
Browse files Browse the repository at this point in the history
fix: Disallow proxy for objects with dynamic properties, including `stdClass`.
  • Loading branch information
priyadi committed Feb 20, 2024
2 parents 4a80abf + cd99e23 commit cd18fc7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* feat(`Context`): `with()` not accepts multiple argument.
* build: Deinternalize `ObjectCacheFactory`
* fix(`PresetMapping`): Support proxied classes, add tests.
* fix: Disallow proxy for objects with dynamic properties, including `stdClass`.

## 1.0.0

Expand Down
8 changes: 8 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Rekalogika\Mapper\Mapping\MappingFactoryInterface;
use Rekalogika\Mapper\ObjectCache\Implementation\ObjectCacheFactory;
use Rekalogika\Mapper\Proxy\Implementation\DoctrineProxyGenerator;
use Rekalogika\Mapper\Proxy\Implementation\DynamicPropertiesProxyGenerator;
use Rekalogika\Mapper\Proxy\Implementation\ProxyFactory;
use Rekalogika\Mapper\Proxy\Implementation\ProxyGenerator;
use Rekalogika\Mapper\Proxy\Implementation\ProxyRegistry;
Expand Down Expand Up @@ -406,6 +407,13 @@
service('doctrine'),
]);

$services
->set('rekalogika.mapper.proxy.generator.dynamic_properties', DynamicPropertiesProxyGenerator::class)
->decorate('rekalogika.mapper.proxy.generator')
->args([
service('.inner'),
]);

# proxy registry

$services
Expand Down
44 changes: 44 additions & 0 deletions src/Proxy/Implementation/DynamicPropertiesProxyGenerator.php
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);
}
}

0 comments on commit cd18fc7

Please sign in to comment.