Skip to content
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
47 changes: 32 additions & 15 deletions src/Symfony/Component/ObjectMapper/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class ObjectMapper implements ObjectMapperInterface, ObjectMapperAwareInte
/**
* Tracks recursive references.
*/
private ?\SplObjectStorage $objectMap = null;
private ?\WeakMap $objectMap = null;

public function __construct(
private readonly ObjectMapperMetadataFactoryInterface $metadataFactory = new ReflectionObjectMapperMetadataFactory(),
Expand All @@ -45,12 +45,20 @@ public function __construct(

public function map(object $source, object|string|null $target = null): object
{
$objectMapInitialized = false;
if (null === $this->objectMap) {
$this->objectMap = new \SplObjectStorage();
$objectMapInitialized = true;
if ($this->objectMap) {
return $this->doMap($source, $target, $this->objectMap, false);
}

$this->objectMap = new \WeakMap();
try {
return $this->doMap($source, $target, $this->objectMap, true);
} finally {
$this->objectMap = null;
}
}

private function doMap(object $source, object|string|null $target, \WeakMap $objectMap, bool $rootCall): object
{
$metadata = $this->metadataFactory->create($source);
$map = $this->getMapTarget($metadata, null, $source, null);
$target ??= $map?->target;
Expand Down Expand Up @@ -89,7 +97,7 @@ public function map(object $source, object|string|null $target = null): object
throw new MappingException(\sprintf('Expected the mapped object to be an instance of "%s" but got "%s".', $targetRefl->getName(), get_debug_type($mappedTarget)));
}

$this->objectMap[$source] = $mappedTarget;
$objectMap[$source] = $mappedTarget;
$ctorArguments = [];
$targetConstructor = $targetRefl->getConstructor();
foreach ($targetConstructor?->getParameters() ?? [] as $parameter) {
Expand Down Expand Up @@ -146,7 +154,7 @@ public function map(object $source, object|string|null $target = null): object
continue;
}

$value = $this->getSourceValue($source, $mappedTarget, $value, $this->objectMap, $mapping);
$value = $this->getSourceValue($source, $mappedTarget, $value, $objectMap, $mapping);
$this->storeValue($targetPropertyName, $mapToProperties, $ctorArguments, $value);
}

Expand All @@ -156,12 +164,12 @@ public function map(object $source, object|string|null $target = null): object
continue;
}

$value = $this->getSourceValue($source, $mappedTarget, $this->getRawValue($source, $propertyName), $this->objectMap);
$value = $this->getSourceValue($source, $mappedTarget, $this->getRawValue($source, $propertyName), $objectMap);
$this->storeValue($propertyName, $mapToProperties, $ctorArguments, $value);
}
}

if (!$mappingToObject && !$map?->transform && $targetConstructor) {
if ((!$mappingToObject || !$rootCall) && !$map?->transform && $targetConstructor) {
try {
$mappedTarget->__construct(...$ctorArguments);
} catch (\ReflectionException $e) {
Expand All @@ -181,10 +189,6 @@ public function map(object $source, object|string|null $target = null): object
$this->propertyAccessor ? $this->propertyAccessor->setValue($mappedTarget, $property, $value) : ($mappedTarget->{$property} = $value);
}

if ($objectMapInitialized) {
$this->objectMap = null;
}

return $mappedTarget;
}

Expand Down Expand Up @@ -218,7 +222,7 @@ private function getRawValue(object $source, string $propertyName): mixed
return $source->{$propertyName};
}

private function getSourceValue(object $source, object $target, mixed $value, \SplObjectStorage $objectMap, ?Mapping $mapping = null): mixed
private function getSourceValue(object $source, object $target, mixed $value, \WeakMap $objectMap, ?Mapping $mapping = null): mixed
{
if ($mapping?->transform) {
$value = $this->applyTransforms($mapping, $value, $source, $target);
Expand All @@ -236,8 +240,21 @@ private function getSourceValue(object $source, object $target, mixed $value, \S
$value = $target;
} elseif ($objectMap->offsetExists($value)) {
$value = $objectMap[$value];
} elseif (\PHP_VERSION_ID < 80400) {
return ($this->objectMapper ?? $this)->map($value, $mapTo->target);
} else {
$value = ($this->objectMapper ?? $this)->map($value, $mapTo->target);
$refl = new \ReflectionClass($mapTo->target);
$mapper = $this->objectMapper ?? $this;

return $refl->newLazyGhost(function ($target) use ($mapper, $value, $objectMap) {
$previousMap = $this->objectMap;
$this->objectMap = $objectMap;
try {
$objectMap[$value] = $mapper->map($value, $target);
} finally {
$this->objectMap = $previousMap;
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy;

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: OrderTarget::class)]
class OrderSource
{
public ?int $id = null;
public ?UserSource $user = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy;

class OrderTarget
{
public ?int $id = null;
public ?UserTarget $user = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy;

use Symfony\Component\ObjectMapper\Attribute\Map;

#[Map(target: UserTarget::class)]
class UserSource
{
public ?string $name = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy;

class UserTarget
{
public ?string $name = null;
}
52 changes: 38 additions & 14 deletions src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\RecursiveDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\Relation;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\RelationDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy\OrderSource;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy\OrderTarget;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy\UserSource;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy\UserTarget;
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultValueStdClass\TargetDto;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\TargetUser;
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\User;
Expand Down Expand Up @@ -84,7 +88,13 @@ final class ObjectMapperTest extends TestCase
public function testMap($expect, $args, array $deps = [])
{
$mapper = new ObjectMapper(...$deps);
$this->assertEquals($expect, $mapper->map(...$args));
$mapped = $mapper->map(...$args);

if (\PHP_VERSION_ID >= 80400 && isset($mapped->relation) && $mapped->relation instanceof D ) {
$mapped->relation->baz;
}

$this->assertEquals($expect, $mapped);
}

/**
Expand Down Expand Up @@ -457,31 +467,27 @@ public function testDecorateObjectMapper()
{
$mapper = new ObjectMapper();
$myMapper = new class($mapper) implements ObjectMapperInterface {
private ?\SplObjectStorage $embededMap = null;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test got transformed and now only tests that an embeded relation does call the decorated mapper.
The previous test (based on api-platform/core@d06b1a0) was based on a leaking object map which isn't great (thanks @nicolas-grekas for the fix). The new non-leaky behavior will need an update for our particular use case inside API Platform and that's fine.


public function __construct(private readonly ObjectMapperInterface $mapper)
public function __construct(private ObjectMapperInterface $mapper)
{
$this->embededMap = new \SplObjectStorage();
$this->mapper = $mapper->withObjectMapper($this);
}

public function map(object $source, object|string|null $target = null): object
{
if (isset($this->embededMap[$source])) {
$target = $this->embededMap[$source];
}

$mapped = $this->mapper->map($source, $target);
$this->embededMap[$source] = $mapped;

if ($source instanceof C) {
$mapped->baz = 'got decorated';
}

return $mapped;
}
};

$mapper = $mapper->withObjectMapper($myMapper);

$d = new D(baz: 'foo', bat: 'bar');
$c = new C(foo: 'foo', bar: 'bar');
$myNewD = $myMapper->map($c);
$this->assertSame('got decorated', $myNewD->baz);

$a = new A();
$a->foo = 'test';
Expand All @@ -491,8 +497,8 @@ public function map(object $source, object|string|null $target = null): object
$a->relation = $c;
$a->relationNotMapped = $d;

$b = $mapper->map($a);
$this->assertSame($myNewD, $b->relation);
$b = $myMapper->map($a);
$this->assertSame('got decorated', $b->relation->baz);
}

#[DataProvider('validPartialInputProvider')]
Expand Down Expand Up @@ -559,4 +565,22 @@ public function testTransformCollection()

$this->assertEquals([new TransformCollectionD('a'), new TransformCollectionD('b')], $transformed->foo);
}

#[RequiresPhp('>=8.4')]
public function testEmbedsAreLazyLoadedByDefault()
{
$mapper = new ObjectMapper();
$source = new OrderSource();
$source->id = 123;
$source->user = new UserSource();
$source->user->name = 'Test User';
$target = $mapper->map($source, OrderTarget::class);
$this->assertInstanceOf(OrderTarget::class, $target);
$this->assertSame(123, $target->id);
$this->assertInstanceOf(UserTarget::class, $target->user);
$refl = new \ReflectionClass(UserTarget::class);
$this->assertTrue($refl->isUninitializedLazyObject($target->user));
$this->assertSame('Test User', $target->user->name);
$this->assertFalse($refl->isUninitializedLazyObject($target->user));
}
}
Loading