🌐 AI搜索 & 代理 主页
Skip to content

Commit 33217e5

Browse files
committed
fixes
1 parent 7513d65 commit 33217e5

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Symfony\Component\ObjectMapper;
4+
5+
/*
6+
* This file is part of the Symfony package.
7+
*
8+
* (c) Fabien Potencier <fabien@symfony.com>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
/**
15+
* @internal
16+
*/
17+
interface ClearObjectMapInterface
18+
{
19+
/**
20+
* Clear object map to free memory.
21+
*/
22+
public function clearObjectMap(): void;
23+
}

src/Symfony/Component/ObjectMapper/ObjectMapper.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* @author Antoine Bluchet <soyuka@gmail.com>
2929
*/
30-
final class ObjectMapper implements ObjectMapperInterface, ObjectMapperAwareInterface
30+
final class ObjectMapper implements ObjectMapperInterface, ObjectMapperAwareInterface, ClearObjectMapInterface
3131
{
3232
/**
3333
* Tracks recursive references.
@@ -45,10 +45,8 @@ public function __construct(
4545

4646
public function map(object $source, object|string|null $target = null): object
4747
{
48-
$objectMapInitialized = false;
4948
if (null === $this->objectMap) {
5049
$this->objectMap = new \SplObjectStorage();
51-
$objectMapInitialized = true;
5250
}
5351

5452
$metadata = $this->metadataFactory->create($source);
@@ -161,7 +159,7 @@ public function map(object $source, object|string|null $target = null): object
161159
}
162160
}
163161

164-
if (!$map?->transform && $targetConstructor) {
162+
if (!$mappingToObject && !$map?->transform && $targetConstructor) {
165163
try {
166164
$mappedTarget->__construct(...$ctorArguments);
167165
} catch (\ReflectionException $e) {
@@ -181,10 +179,6 @@ public function map(object $source, object|string|null $target = null): object
181179
$this->propertyAccessor ? $this->propertyAccessor->setValue($mappedTarget, $property, $value) : ($mappedTarget->{$property} = $value);
182180
}
183181

184-
if ($objectMapInitialized) {
185-
// $this->objectMap = null;
186-
}
187-
188182
return $mappedTarget;
189183
}
190184

@@ -379,5 +373,10 @@ public function withObjectMapper(ObjectMapperInterface $objectMapper): static
379373

380374
return $clone;
381375
}
376+
377+
public function clearObjectMap(): void
378+
{
379+
$this->objectMap = null;
380+
}
382381
}
383382

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,17 @@ final class ObjectMapperTest extends TestCase
8888
public function testMap($expect, $args, array $deps = [])
8989
{
9090
$mapper = new ObjectMapper(...$deps);
91-
$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 \Symfony\Component\ObjectMapper\Tests\Fixtures\D
97+
) {
98+
$mapped->relation->baz;
99+
}
100+
101+
$this->assertEquals($expect, $mapped);
92102
}
93103

94104
/**
@@ -463,9 +473,10 @@ public function testDecorateObjectMapper()
463473
$myMapper = new class($mapper) implements ObjectMapperInterface {
464474
private ?\SplObjectStorage $embededMap = null;
465475

466-
public function __construct(private readonly ObjectMapperInterface $mapper)
476+
public function __construct(private ObjectMapperInterface $mapper)
467477
{
468478
$this->embededMap = new \SplObjectStorage();
479+
$this->mapper = $mapper->withObjectMapper($this);
469480
}
470481

471482
public function map(object $source, object|string|null $target = null): object
@@ -481,8 +492,6 @@ public function map(object $source, object|string|null $target = null): object
481492
}
482493
};
483494

484-
$mapper = $mapper->withObjectMapper($myMapper);
485-
486495
$d = new D(baz: 'foo', bat: 'bar');
487496
$c = new C(foo: 'foo', bar: 'bar');
488497
$myNewD = $myMapper->map($c);
@@ -495,7 +504,7 @@ public function map(object $source, object|string|null $target = null): object
495504
$a->relation = $c;
496505
$a->relationNotMapped = $d;
497506

498-
$b = $mapper->map($a);
507+
$b = $myMapper->map($a);
499508
$this->assertSame($myNewD, $b->relation);
500509
}
501510

0 commit comments

Comments
 (0)