Skip to content

Commit dfb6950

Browse files
committed
[ObjectMapper] lazy map relations
1 parent 9b32764 commit dfb6950

File tree

6 files changed

+93
-18
lines changed

6 files changed

+93
-18
lines changed

src/Symfony/Component/ObjectMapper/ObjectMapper.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class ObjectMapper implements ObjectMapperInterface, ObjectMapperAwareInte
3232
/**
3333
* Tracks recursive references.
3434
*/
35-
private ?\SplObjectStorage $objectMap = null;
35+
private ?\WeakMap $objectMap = null;
3636

3737
public function __construct(
3838
private readonly ObjectMapperMetadataFactoryInterface $metadataFactory = new ReflectionObjectMapperMetadataFactory(),
@@ -41,16 +41,11 @@ public function __construct(
4141
private readonly ?ContainerInterface $conditionCallableLocator = null,
4242
private ?ObjectMapperInterface $objectMapper = null,
4343
) {
44+
$this->objectMap = new \WeakMap();
4445
}
4546

4647
public function map(object $source, object|string|null $target = null): object
4748
{
48-
$objectMapInitialized = false;
49-
if (null === $this->objectMap) {
50-
$this->objectMap = new \SplObjectStorage();
51-
$objectMapInitialized = true;
52-
}
53-
5449
$metadata = $this->metadataFactory->create($source);
5550
$map = $this->getMapTarget($metadata, null, $source, null);
5651
$target ??= $map?->target;
@@ -181,10 +176,6 @@ public function map(object $source, object|string|null $target = null): object
181176
$this->propertyAccessor ? $this->propertyAccessor->setValue($mappedTarget, $property, $value) : ($mappedTarget->{$property} = $value);
182177
}
183178

184-
if ($objectMapInitialized) {
185-
$this->objectMap = null;
186-
}
187-
188179
return $mappedTarget;
189180
}
190181

@@ -218,7 +209,7 @@ private function getRawValue(object $source, string $propertyName): mixed
218209
return $source->{$propertyName};
219210
}
220211

221-
private function getSourceValue(object $source, object $target, mixed $value, \SplObjectStorage $objectMap, ?Mapping $mapping = null): mixed
212+
private function getSourceValue(object $source, object $target, mixed $value, \WeakMap $objectMap, ?Mapping $mapping = null): mixed
222213
{
223214
if ($mapping?->transform) {
224215
$value = $this->applyTransforms($mapping, $value, $source, $target);
@@ -237,7 +228,16 @@ private function getSourceValue(object $source, object $target, mixed $value, \S
237228
} elseif ($objectMap->offsetExists($value)) {
238229
$value = $objectMap[$value];
239230
} else {
240-
$value = ($this->objectMapper ?? $this)->map($value, $mapTo->target);
231+
if (\PHP_VERSION_ID >= 80400) {
232+
$refl = new \ReflectionClass($mapTo->target);
233+
$mapper = $this->objectMapper ?? $this;
234+
235+
return $refl->newLazyProxy(static function () use ($mapper, $value, $objectMap, $mapTo) {
236+
return $objectMap[$value] = $mapper->map($value, $mapTo->target);
237+
});
238+
}
239+
240+
return ($this->objectMapper ?? $this)->map($value, $mapTo->target);
241241
}
242242
}
243243

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy;
4+
5+
use Symfony\Component\ObjectMapper\Attribute\Map;
6+
7+
#[Map(target: OrderTarget::class)]
8+
class OrderSource
9+
{
10+
public ?int $id = null;
11+
public ?UserSource $user = null;
12+
}
13+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy;
4+
5+
class OrderTarget
6+
{
7+
public ?int $id = null;
8+
public ?UserTarget $user = null;
9+
}
10+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy;
4+
5+
use Symfony\Component\ObjectMapper\Attribute\Map;
6+
7+
#[Map(target: UserTarget::class)]
8+
class UserSource
9+
{
10+
public ?string $name = null;
11+
}
12+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy;
4+
5+
class UserTarget
6+
{
7+
public ?string $name = null;
8+
}
9+

src/Symfony/Component/ObjectMapper/Tests/ObjectMapperTest.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\RecursiveDto;
3333
use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\Relation;
3434
use Symfony\Component\ObjectMapper\Tests\Fixtures\DeeperRecursion\RelationDto;
35+
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy\OrderSource;
36+
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy\OrderTarget;
37+
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy\UserSource;
38+
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultLazy\UserTarget;
3539
use Symfony\Component\ObjectMapper\Tests\Fixtures\DefaultValueStdClass\TargetDto;
3640
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\TargetUser;
3741
use Symfony\Component\ObjectMapper\Tests\Fixtures\Flatten\User;
@@ -84,7 +88,17 @@ final class ObjectMapperTest extends TestCase
8488
public function testMap($expect, $args, array $deps = [])
8589
{
8690
$mapper = new ObjectMapper(...$deps);
87-
$this->assertEquals($expect, $mapper->map(...$args));
91+
$mapped = $mapper->map(...$args);
92+
93+
if (
94+
\PHP_VERSION_ID >= 80400
95+
&& isset($mapped->relation)
96+
&& $mapped->relation instanceof D
97+
) {
98+
$mapped->relation->baz;
99+
}
100+
101+
$this->assertEquals($expect, $mapped);
88102
}
89103

90104
/**
@@ -459,9 +473,10 @@ public function testDecorateObjectMapper()
459473
$myMapper = new class($mapper) implements ObjectMapperInterface {
460474
private ?\SplObjectStorage $embededMap = null;
461475

462-
public function __construct(private readonly ObjectMapperInterface $mapper)
476+
public function __construct(private ObjectMapperInterface $mapper)
463477
{
464478
$this->embededMap = new \SplObjectStorage();
479+
$this->mapper = $mapper->withObjectMapper($this);
465480
}
466481

467482
public function map(object $source, object|string|null $target = null): object
@@ -477,8 +492,6 @@ public function map(object $source, object|string|null $target = null): object
477492
}
478493
};
479494

480-
$mapper = $mapper->withObjectMapper($myMapper);
481-
482495
$d = new D(baz: 'foo', bat: 'bar');
483496
$c = new C(foo: 'foo', bar: 'bar');
484497
$myNewD = $myMapper->map($c);
@@ -491,7 +504,7 @@ public function map(object $source, object|string|null $target = null): object
491504
$a->relation = $c;
492505
$a->relationNotMapped = $d;
493506

494-
$b = $mapper->map($a);
507+
$b = $myMapper->map($a);
495508
$this->assertSame($myNewD, $b->relation);
496509
}
497510

@@ -559,4 +572,22 @@ public function testTransformCollection()
559572

560573
$this->assertEquals([new TransformCollectionD('a'), new TransformCollectionD('b')], $transformed->foo);
561574
}
575+
576+
#[RequiresPhp('>=8.4')]
577+
public function testEmbedsAreLazyLoadedByDefault()
578+
{
579+
$mapper = new ObjectMapper();
580+
$source = new OrderSource();
581+
$source->id = 123;
582+
$source->user = new UserSource();
583+
$source->user->name = 'Test User';
584+
$target = $mapper->map($source, OrderTarget::class);
585+
$this->assertInstanceOf(OrderTarget::class, $target);
586+
$this->assertSame(123, $target->id);
587+
$this->assertInstanceOf(UserTarget::class, $target->user);
588+
$refl = new \ReflectionClass(UserTarget::class);
589+
$this->assertTrue($refl->isUninitializedLazyObject($target->user));
590+
$this->assertSame('Test User', $target->user->name);
591+
$this->assertFalse($refl->isUninitializedLazyObject($target->user));
592+
}
562593
}

0 commit comments

Comments
 (0)