Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[VarExporter] Allow reinitializing lazy objects with a new initializer #58129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2024
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/VarExporter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Allow reinitializing lazy objects with a new initializer

6.4
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class LazyObjectState
* @param array<string, true> $skippedProperties
*/
public function __construct(
public readonly \Closure $initializer,
public readonly array $skippedProperties = [],
public \Closure $initializer,
public array $skippedProperties = [],
) {
}

Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/VarExporter/LazyGhostTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public static function createLazyGhost(\Closure $initializer, ?array $skippedPro
$instance ??= Registry::$classReflectors[$class]->newInstanceWithoutConstructor();
}

if (isset($instance->lazyObjectState)) {
$instance->lazyObjectState->initializer = $initializer;
$instance->lazyObjectState->skippedProperties = $skippedProperties ??= [];

if (LazyObjectState::STATUS_UNINITIALIZED_FULL !== $instance->lazyObjectState->status) {
$instance->lazyObjectState->reset($instance);
}

return $instance;
}

$instance->lazyObjectState = new LazyObjectState($initializer, $skippedProperties ??= []);

foreach (Registry::$classResetters[$class] as $reset) {
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Component/VarExporter/LazyProxyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public static function createLazyProxy(\Closure $initializer, ?object $instance
$instance ??= Registry::$classReflectors[$class]->newInstanceWithoutConstructor();
}

if (isset($instance->lazyObjectState)) {
$instance->lazyObjectState->initializer = $initializer;
unset($instance->lazyObjectState->realInstance);

return $instance;
}

$instance->lazyObjectState = new LazyObjectState($initializer);

foreach (Registry::$classResetters[$class] as $reset) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost;

class RegularClass
{
public function __construct(
public int $foo
) {
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/LazyGhostTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ public function testNormalization()
$this->assertSame(['property' => 'property', 'method' => 'method'], $output);
}

public function testReinitLazyGhost()
{
$object = TestClass::createLazyGhost(function ($p) { $p->public = 2; });

$this->assertSame(2, $object->public);

TestClass::createLazyGhost(function ($p) { $p->public = 3; }, null, $object);

$this->assertSame(3, $object->public);
}

/**
* @template T
*
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/LazyProxyTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\VarExporter\Exception\LogicException;
use Symfony\Component\VarExporter\LazyProxyTrait;
use Symfony\Component\VarExporter\ProxyHelper;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\RegularClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\FinalPublicClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\ReadOnlyClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyProxy\StringMagicGetClass;
Expand Down Expand Up @@ -295,6 +296,31 @@ public function testNormalization()
$this->assertSame(['property' => 'property', 'method' => 'method'], $output);
}

public function testReinitRegularLazyProxy()
{
$object = $this->createLazyProxy(RegularClass::class, fn () => new RegularClass(123));

$this->assertSame(123, $object->foo);

$object::createLazyProxy(fn () => new RegularClass(234), $object);

$this->assertSame(234, $object->foo);
}

/**
* @requires PHP 8.3
*/
public function testReinitReadonlyLazyProxy()
{
$object = $this->createLazyProxy(ReadOnlyClass::class, fn () => new ReadOnlyClass(123));

$this->assertSame(123, $object->foo);

$object::createLazyProxy(fn () => new ReadOnlyClass(234), $object);

$this->assertSame(234, $object->foo);
}

/**
* @template T
*
Expand Down