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

Skip to content

[VarExporter] Remove LazyGhostTrait and LazyProxyTrait in favor of native lazy objects #60716

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
Jun 6, 2025
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
7 changes: 7 additions & 0 deletions UPGRADE-8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ TwigBridge
----------

* Remove `text` format from the `debug:twig` command, use the `txt` format instead

VarExporter
-----------

* Restrict `ProxyHelper::generateLazyProxy()` to generating abstraction-based lazy decorators; use native lazy proxies otherwise
* Remove `LazyGhostTrait` and `LazyProxyTrait`, use native lazy objects instead
* Remove `ProxyHelper::generateLazyGhost()`, use native lazy objects instead
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\JsonStreamer\CacheWarmer\LazyGhostCacheWarmer;
use Symfony\Component\JsonStreamer\CacheWarmer\StreamerCacheWarmer;
use Symfony\Component\JsonStreamer\JsonStreamReader;
use Symfony\Component\JsonStreamer\JsonStreamWriter;
Expand Down

This file was deleted.

This file was deleted.

7 changes: 7 additions & 0 deletions src/Symfony/Component/VarExporter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
CHANGELOG
=========

8.0
---

* Restrict `ProxyHelper::generateLazyProxy()` to generating abstraction-based lazy decorators; use native lazy proxies otherwise
* Remove `LazyGhostTrait` and `LazyProxyTrait`, use native lazy objects instead
* Remove `ProxyHelper::generateLazyGhost()`, use native lazy objects instead

7.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function &__get($name): mixed
$notByRef = $access & Hydrator::PROPERTY_NOT_BY_REF || ($access >> 2) & \ReflectionProperty::IS_PRIVATE_SET;
}

if ($notByRef || 2 !== ((Registry::$parentMethods[$class] ??= Registry::getParentMethods($class))['get'] ?: 2)) {
if ($notByRef || 2 !== ((Registry::$parentGet[$class] ??= Registry::getParentGet($class)) ?: 2)) {
$value = $instance->$name;

return $value;
Expand Down
80 changes: 7 additions & 73 deletions src/Symfony/Component/VarExporter/Internal/LazyObjectRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,9 @@ class LazyObjectRegistry
public static array $classAccessors = [];

/**
* @var array<class-string, array{set: bool, isset: bool, unset: bool, clone: bool, serialize: bool, unserialize: bool, sleep: bool, wakeup: bool, destruct: bool, get: int}>
* @var array<class-string, int}>
*/
public static array $parentMethods = [];

public static ?\Closure $noInitializerState = null;
public static array $parentGet = [];

public static function getClassResetters($class)
{
Expand Down Expand Up @@ -86,80 +84,16 @@ public static function getClassResetters($class)
return $resetters;
}

public static function getClassAccessors($class)
{
return \Closure::bind(static fn () => [
'get' => static function &($instance, $name, $notByRef) {
if (!$notByRef) {
return $instance->$name;
}
$value = $instance->$name;

return $value;
},
'set' => static function ($instance, $name, $value) {
$instance->$name = $value;
},
'isset' => static fn ($instance, $name) => isset($instance->$name),
'unset' => static function ($instance, $name) {
unset($instance->$name);
},
], null, \Closure::class === $class ? null : $class)();
}

public static function getParentMethods($class)
public static function getParentGet($class): int
{
$parent = get_parent_class($class);
$methods = [];

foreach (['set', 'isset', 'unset', 'clone', 'serialize', 'unserialize', 'sleep', 'wakeup', 'destruct', 'get'] as $method) {
if (!$parent || !method_exists($parent, '__'.$method)) {
$methods[$method] = false;
} else {
$m = new \ReflectionMethod($parent, '__'.$method);
$methods[$method] = !$m->isAbstract() && !$m->isPrivate();
}
}

$methods['get'] = $methods['get'] ? ($m->returnsReference() ? 2 : 1) : 0;

return $methods;
}

public static function getScopeForRead($propertyScopes, $class, $property)
{
if (!isset($propertyScopes[$k = "\0$class\0$property"]) && !isset($propertyScopes[$k = "\0*\0$property"])) {
return null;
}
$frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];

if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
$scope = $frame['object']->class;
}
if ('*' === $k[1] && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
return null;
}

return $scope;
}

public static function getScopeForWrite($propertyScopes, $class, $property, $flags)
{
if (!($flags & (\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_READONLY | \ReflectionProperty::IS_PRIVATE_SET))) {
return null;
if (!$parent || !method_exists($parent, '__get')) {
return 0;
}
$frame = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2];

if (\ReflectionProperty::class === $scope = $frame['class'] ?? \Closure::class) {
$scope = $frame['object']->class;
}
if ($flags & (\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PRIVATE_SET)) {
return $scope;
}
if ($flags & (\ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PROTECTED_SET) && ($class === $scope || (is_subclass_of($class, $scope) && !isset($propertyScopes["\0$scope\0$property"])))) {
return null;
}
$m = new \ReflectionMethod($parent, '__get');

return $scope;
return !$m->isAbstract() && !$m->isPrivate() ? ($m->returnsReference() ? 2 : 1) : 0;
}
}
72 changes: 1 addition & 71 deletions src/Symfony/Component/VarExporter/Internal/LazyObjectState.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,80 +22,10 @@
*/
class LazyObjectState
{
public const STATUS_UNINITIALIZED_FULL = 1;
public const STATUS_UNINITIALIZED_PARTIAL = 2;
public const STATUS_INITIALIZED_FULL = 3;
public const STATUS_INITIALIZED_PARTIAL = 4;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those constants are still used in VarDumper.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #60724


/**
* @var self::STATUS_*
*/
public int $status = self::STATUS_UNINITIALIZED_FULL;

public ?\Closure $initializer = null;
public object $realInstance;
public object $cloneInstance;

/**
* @param array<string, true> $skippedProperties
*/
public function __construct(
public ?\Closure $initializer = null,
public array $skippedProperties = [],
) {
}

public function initialize($instance, $propertyName, $writeScope)
{
if (self::STATUS_UNINITIALIZED_FULL !== $this->status) {
return $this->status;
}

$this->status = self::STATUS_INITIALIZED_PARTIAL;

try {
if ($defaultProperties = array_diff_key(LazyObjectRegistry::$defaultProperties[$instance::class], $this->skippedProperties)) {
PublicHydrator::hydrate($instance, $defaultProperties);
}

($this->initializer)($instance);
} catch (\Throwable $e) {
$this->status = self::STATUS_UNINITIALIZED_FULL;
$this->reset($instance);

throw $e;
}

return $this->status = self::STATUS_INITIALIZED_FULL;
}

public function reset($instance): void
{
$class = $instance::class;
$propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
$skippedProperties = $this->skippedProperties;
$properties = (array) $instance;

foreach ($propertyScopes as $key => [$scope, $name, , $access]) {
$propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name;

if ($k === $key && ($access & Hydrator::PROPERTY_HAS_HOOKS || ($access >> 2) & \ReflectionProperty::IS_READONLY || !\array_key_exists($k, $properties))) {
$skippedProperties[$k] = true;
}
}

foreach (LazyObjectRegistry::$classResetters[$class] as $reset) {
$reset($instance, $skippedProperties);
}

foreach ((array) $instance as $name => $value) {
if ("\0" !== ($name[0] ?? '') && !\array_key_exists($name, $skippedProperties)) {
unset($instance->$name);
}
}

$this->status = self::STATUS_UNINITIALIZED_FULL;
}

public function __clone()
{
if (isset($this->cloneInstance)) {
Expand Down
23 changes: 0 additions & 23 deletions src/Symfony/Component/VarExporter/Internal/LazyObjectTrait.php

This file was deleted.

Loading
Loading