8000 feature #58129 [VarExporter] Allow reinitializing lazy objects with a… · symfony/symfony@b455a5e · GitHub
[go: up one dir, main page]

Skip to content

Commit b455a5e

Browse files
feature #58129 [VarExporter] Allow reinitializing lazy objects with a new initializer (nicolas-grekas)
This PR was merged into the 7.2 branch. Discussion ---------- [VarExporter] Allow reinitializing lazy objects with a new initializer | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT Being able to call createLazyGhost/Proxy on an existing lazy object is a capability that PHP 8.4 will provide thanks to https://wiki.php.net/rfc/lazy-objects, and that zenstruck/foundry desires also to implement refreshable fixtures. /cc `@kbond` `@nikophil` Commits ------- 4c1b320 [VarExporter] Allow reinitializing lazy objects with a new initializer
2 parents 76b7a7e + 4c1b320 commit b455a5e

File tree

7 files changed

+82
-2
lines changed

7 files changed

+82
-2
lines changed

src/Symfony/Component/VarExporter/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.2
5+
---
6+
7+
* Allow reinitializing lazy objects with a new initializer
8+
49
6.4
510
---
611

src/Symfony/Component/VarExporter/Internal/LazyObjectState.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class LazyObjectState
3838
* @param array<string, true> $skippedProperties
3939
*/
4040
public function __construct(
41-
public readonly \Closure $initializer,
42-
public readonly array $skippedProperties = [],
41+
public \Closure $initializer,
42+
public array $skippedProperties = [],
4343
) {
4444
}
4545

src/Symfony/Component/VarExporter/LazyGhostTrait.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public static function createLazyGhost(\Closure $initializer, ?array $skippedPro
5151
$instance ??= Registry::$classReflectors[$class]->newInstanceWithoutConstructor();
5252
}
5353

54+
if (isset($instance->lazyObjectState)) {
55+
$instance->lazyObjectState->initializer = $initializer;
56+
$instance->lazyObjectState->skippedProperties = $skippedProperties ??= [];
57+
58+
if (LazyObjectState::STATUS_UNINITIALIZED_FULL !== $instance->lazyObjectState->status) {
59+
$instance->lazyObjectState->reset($instance);
60+
}
61+
62+
return $instance;
63+
}
64+
5465
$instance->lazyObjectState = new LazyObjectState($initializer, $skippedProperties ??= []);
5566

5667
foreach (Registry::$classResetters[$class] as $reset) {

src/Symfony/Component/VarExporter/LazyProxyTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public static function createLazyProxy(\Closure $initializer, ?object $instance
4747
$instance ??= Registry::$classReflectors[$class]->newInstanceWithoutConstructor();
4848
}
4949

50+
if (isset($instance->lazyObjectState)) {
51+
$instance->lazyObjectState->initializer = $initializer;
52+
unset($instance->lazyObjectState->realInstance);
53+
54+
return $instance;
55+
}
56+
5057
$instance->lazyObjectState = new LazyObjectState($initializer);
5158

5259
foreach (Registry::$classResetters[$class] as $reset) {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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\VarExporter\Tests\Fixtures\LazyGhost;
13+
14+
class RegularClass
15+
{
16+
public function __construct(
17+
public int $foo
18+
) {
19+
}
20+
}

src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,17 @@ public function testNormalization()
280280
$this->assertSame(['property' => 'property', 'method' => 'method'], $output);
281281
}
282282

283+
public function testReinitLazyGhost()
284+
{
285+
$object = TestClass::createLazyGhost(function ($p) { $p->public = 2; });
286+
287+
$this->assertSame(2, $object->public);
288+
289+
TestClass::createLazyGhost(function ($p) { $p->public = 3; }, null, $object);
290+
291+
$this->assertSame(3, $object->public);
292+
}
293+
283294
/**
284295
* @template T
285296
*

src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\VarExporter\Exception\LogicException;
1919
use Symfony\Component\VarExporter\LazyProxyTrait;
2020
use Symfony\Component\VarExporter\ProxyHelper;
21+
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\RegularClass;
2122
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\FinalPublicClass;
2223
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\ReadOnlyClass;
2324
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\StringMagicGetClass;
@@ -295,6 +296,31 @@ public function testNormalization()
295296
$this->assertSame(['property' => 'property', 'method' => 'method'], $output);
296297
}
297298

299+
public function testReinitRegularLazyProxy()
300+
{
301+
$object = $this->createLazyProxy(RegularClass::class, fn () => new RegularClass(123));
302+
303+
$this->assertSame(123, $object->foo);
304+
305+
$object::createLazyProxy(fn () => new RegularClass(234), $object);
306+
307+
$this->assertSame(234, $object->foo);
308+
}
309+
310+
/**
311+
* @requires PHP 8.3
312+
*/
313+
public function testReinitReadonlyLazyProxy()
314+
{
315+
$object = $this->createLazyProxy(ReadOnlyClass::class, fn () => new ReadOnlyClass(123));
316+
317+
$this->assertSame(123, $object->foo);
318+
319+
$object::createLazyProxy(fn () => new ReadOnlyClass(234), $object);
320+
321+
$this->assertSame(234, $object->foo);
322+
}
323+
298324
/**
299325
* @template T
300326
*

0 commit comments

Comments
 (0)
0