Skip to content

Commit 5cc9cae

Browse files
bug symfony#61146 [ObjectMapper] initialize lazy objects (soyuka)
This PR was merged into the 7.3 branch. Discussion ---------- [ObjectMapper] initialize lazy objects | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | yes | Deprecations? | no | Issues | Fix symfony#61050 | License | MIT Commits ------- 3b15ab7 [ObjectMapper] initialize lazy objects
2 parents 155eacb + 3b15ab7 commit 5cc9cae

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

src/Symfony/Component/ObjectMapper/ObjectMapper.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\ObjectMapper\Metadata\ReflectionObjectMapperMetadataFactory;
2121
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException as PropertyAccessorNoSuchPropertyException;
2222
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
23+
use Symfony\Component\VarExporter\LazyObjectInterface;
2324

2425
/**
2526
* Object to object mapper.
@@ -315,6 +316,12 @@ private function getSourceReflectionClass(object $source, \ReflectionClass $targ
315316
throw new MappingException($e->getMessage(), $e->getCode(), $e);
316317
}
317318

319+
if ($source instanceof LazyObjectInterface) {
320+
$source->initializeLazyObject();
321+
} elseif (\PHP_VERSION_ID >= 80400 && $refl->isUninitializedLazyObject($source)) {
322+
$refl->initializeLazyObject($source);
323+
}
324+
318325
if ($metadata) {
319326
return $refl;
320327
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures;
13+
14+
use Symfony\Component\VarExporter\LazyGhostTrait;
15+
use Symfony\Component\VarExporter\LazyObjectInterface;
16+
17+
class LazyFoo extends \stdClass implements LazyObjectInterface
18+
{
19+
use LazyGhostTrait;
20+
21+
public string $name = 'foo';
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ObjectMapper\Tests\Fixtures;
13+
14+
class MyProxy
15+
{
16+
public string $name;
17+
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallback\B as InstanceCallbackB;
4242
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallbackWithArguments\A as InstanceCallbackWithArgumentsA;
4343
use Symfony\Component\ObjectMapper\Tests\Fixtures\InstanceCallbackWithArguments\B as InstanceCallbackWithArgumentsB;
44+
use Symfony\Component\ObjectMapper\Tests\Fixtures\LazyFoo;
4445
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\AToBMapper;
4546
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\MapStructMapperMetadataFactory;
4647
use Symfony\Component\ObjectMapper\Tests\Fixtures\MapStruct\Source;
@@ -52,6 +53,7 @@
5253
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargetProperty\C as MultipleTargetPropertyC;
5354
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\A as MultipleTargetsA;
5455
use Symfony\Component\ObjectMapper\Tests\Fixtures\MultipleTargets\C as MultipleTargetsC;
56+
use Symfony\Component\ObjectMapper\Tests\Fixtures\MyProxy;
5557
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Source as PromotedConstructorSource;
5658
use Symfony\Component\ObjectMapper\Tests\Fixtures\PromotedConstructor\Target as PromotedConstructorTarget;
5759
use Symfony\Component\ObjectMapper\Tests\Fixtures\Recursion\AB;
@@ -368,4 +370,36 @@ public static function objectMapperProvider(): iterable
368370
yield [new ObjectMapper()];
369371
yield [new ObjectMapper(new ReflectionObjectMapperMetadataFactory(), PropertyAccess::createPropertyAccessor())];
370372
}
373+
374+
public function testMapInitializesLazyObject()
375+
{
376+
$lazy = new LazyFoo();
377+
$mapper = new ObjectMapper();
378+
$mapper->map($lazy, \stdClass::class);
379+
$this->assertTrue($lazy->isLazyObjectInitialized());
380+
}
381+
382+
/**
383+
* @requires PHP 8.4
384+
*/
385+
public function testMapInitializesNativePhp84LazyObject()
386+
{
387+
$initialized = false;
388+
$initializer = function () use (&$initialized) {
389+
$initialized = true;
390+
391+
$p = new MyProxy();
392+
$p->name = 'test';
393+
394+
return $p;
395+
};
396+
397+
$r = new \ReflectionClass(MyProxy::class);
398+
$lazyObj = $r->newLazyProxy($initializer);
399+
$this->assertFalse($initialized);
400+
$mapper = new ObjectMapper();
401+
$d = $mapper->map($lazyObj, MyProxy::class);
402+
$this->assertSame('test', $d->name);
403+
$this->assertTrue($initialized);
404+
}
371405
}

src/Symfony/Component/ObjectMapper/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"psr/container": "^2.0"
2121
},
2222
"require-dev": {
23-
"symfony/property-access": "^7.2"
23+
"symfony/property-access": "^7.2",
24+
"symfony/var-exporter": "^7.2"
2425
},
2526
"autoload": {
2627
"psr-4": {

0 commit comments

Comments
 (0)