Skip to content
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

fix: Disallow proxy for objects with dynamic properties, including stdClass. #26

Merged
merged 1 commit into from
Feb 20, 2024
Merged
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
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);
}
}
Loading