8000 [VarExporter] Drop support for partially initialized lazy object by nicolas-grekas · Pull Request #52569 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarExporter] Drop support for partially initialized lazy object #52569

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
Nov 14, 2023
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
6 changes: 6 additions & 0 deletions UPGRADE-7.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Components
* [Translation](#Translation)
* [Validator](#Validator)
* [VarDumper](#VarDumper)
* [VarExporter](#VarExporter)
* [Workflow](#Workflow)
* [Yaml](#Yaml)

Expand Down Expand Up @@ -608,6 +609,11 @@ VarDumper
* Add parameter `string $label = null` to `VarDumper::dump()`
* Require explicit argument when calling `VarDumper::setHandler()`

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

* Remove support for per-property lazy-initializers

Workflow
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ public static function getClassResetters($class)

$resetters = [];
foreach ($classProperties as $scope => $properties) {
$resetters[] = \Closure::bind(static function ($instance, $skippedProperties, $onlyProperties = null) use ($properties) {
$resetters[] = \Closure::bind(static function ($instance, $skippedProperties) use ($properties) {
foreach ($properties as $name => $key) {
if (!\array_key_exists($key, $skippedProperties) && (null === $onlyProperties || \array_key_exists($key, $onlyProperties))) {
if (!\array_key_exists($key, $skippedProperties)) {
unset($instance->$name);
}
}
}, null, $scope);
}

$resetters[] = static function ($instance, $skippedProperties, $onlyProperties = null) {
$resetters[] = static function ($instance, $skippedProperties) {
foreach ((array) $instance as $name => $value) {
if ("\0" !== ($name[0] ?? '') && !\array_key_exists($name, $skippedProperties) && (null === $onlyProperties || \array_key_exists($name, $onlyProperties))) {
if ("\0" !== ($name[0] ?? '') && !\array_key_exists($name, $skippedProperties)) {
unset($instance->$name);
}
}
Expand Down
45 changes: 4 additions & 41 deletions src/Symfony/Component/VarExporter/Internal/LazyObjectState.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class LazyObjectState

public object $realInstance;

public function __construct(public readonly \Closure|array $initializer, $skippedProperties = [])
public function __construct(public readonly \Closure $initializer, $skippedProperties = [])
{
$this->skippedProperties = $skippedProperties;
$this->status = \is_array($initializer) ? self::STATUS_UNINITIALIZED_PARTIAL : self::STATUS_UNINITIALIZED_FULL;
$this->status = self::STATUS_UNINITIALIZED_FULL;
}

public function initialize($instance, $propertyName, $propertyScope)
Expand All @@ -51,42 +51,6 @@ public function initialize($instance, $propertyName, $propertyScope)
return self::STATUS_INITIALIZED_FULL;
}

if (\is_array($this->initializer)) {
$class = $instance::class;
$propertyScope ??= $class;
$propertyScopes = Hydrator::$propertyScopes[$class];
$propertyScopes[$k = "\0$propertyScope\0$propertyName"] ?? $propertyScopes[$k = "\0*\0$propertyName"] ?? $k = $propertyName;

if ($initializer = $this->initializer[$k] ?? null) {
$value = $initializer(...[$instance, $propertyName, $propertyScope, LazyObjectRegistry::$defaultProperties[$class][$k] ?? null]);
$accessor = LazyObjectRegistry::$classAccessors[$propertyScope] ??= LazyObjectRegistry::getClassAccessors($propertyScope);
$accessor['set']($instance, $propertyName, $value);

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

$status = self::STATUS_UNINITIALIZED_PARTIAL;

if ($initializer = $this->initializer["\0"] ?? null) {
if (!\is_array($values = $initializer($instance, LazyObjectRegistry::$defaultProperties[$class]))) {
throw new \TypeError(sprintf('The lazy-initializer defined for instance of "%s" must return an array, got "%s".', $class, get_debug_type($values)));
}
$properties = (array) $instance;
foreach ($values as $key => $value) {
if ($k === $key) {
$status = self::STATUS_INITIALIZED_PARTIAL;
}
if (!\array_key_exists($key, $properties) && [$scope, $name, $readonlyScope] = $propertyScopes[$key] ?? null) {
$scope = $readonlyScope ?? ('*' !== $scope ? $scope : $class);
$accessor = LazyObjectRegistry::$classAccessors[$scope] ??= LazyObjectRegistry::getClassAccessors($scope);
$accessor['set']($instance, $name, $value);
}
}
}

return $status;
}

$this->status = self::STATUS_INITIALIZED_FULL;

try {
Expand All @@ -111,7 +75,6 @@ public function reset($instance): void
$propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
$skippedProperties = $this->skippedProperties;
$properties = (array) $instance;
$onlyProperties = \is_array($this->initializer) ? $this->initializer : null;

foreach ($propertyScopes as $key => [$scope, $name, $readonlyScope]) {
$propertyScopes[$k = "\0$scope\0$name"] ?? $propertyScopes[$k = "\0*\0$name"] ?? $k = $name;
Expand All @@ -122,9 +85,9 @@ public function reset($instance): void
}

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

$this->status = self::STATUS_INITIALIZED_FULL === $this->status ? self::STATUS_UNINITIALIZED_FULL : self::STATUS_UNINITIALIZED_PARTIAL;
$this->status = self::STATUS_UNINITIALIZED_FULL;
}
}
84 changes: 11 additions & 73 deletions src/Symfony/Component/VarExporter/LazyGhostTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@ trait LazyGhostTrait
* that the initializer doesn't initialize, if any
* @param static|null $instance
*/
public static function createLazyGhost(\Closure|array $initializer, array $skippedProperties = null, object $instance = null): static
public static function createLazyGhost(\Closure $initializer, array $skippedProperties = null, object $instance = null): static
{
if (\is_array($initializer)) {
trigger_deprecation('symfony/var-exporter', '6.4', 'Per-property lazy-initializers are deprecated and won\'t be supported anymore in 7.0, use an object initializer instead.');
}

$onlyProperties = null === $skippedProperties && \is_array($initializer) ? $initializer : null;

if (self::class !== $class = $instance ? $instance::class : static::class) {
$skippedProperties["\0".self::class."\0lazyObjectState"] = true;
} elseif (\defined($class.'::LAZY_OBJECT_PROPERTY_SCOPES')) {
Expand All @@ -50,7 +44,7 @@ public static function createLazyGhost(\Closure|array $initializer, array $skipp
$instance->lazyObjectState = new LazyObjectState($initializer, $skippedProperties ??= []);

foreach (Registry::$classResetters[$class] ??= Registry::getClassResetters($class) as $reset) {
$reset($instance, $skippedProperties, $onlyProperties);
$reset($instance, $skippedProperties);
}

return $instance;
Expand All @@ -67,25 +61,7 @@ public function isLazyObjectInitialized(bool $partial = false): bool
return true;
}

if (!\is_array($state->initializer)) {
return LazyObjectState::STATUS_INITIALIZED_FULL === $state->status;
}

$class = $this::class;
$properties = (array) $this;

if ($partial) {
return (bool) array_intersect_key($state->initializer, $properties);
}

$propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
foreach ($state->initializer as $key => $initializer) {
if (!\array_key_exists($key, $properties) && isset($propertyScopes[$key])) {
return false;
}
}

return true;
return LazyObjectState::STATUS_INITIALIZED_FULL === $state->status;
}

/**
Expand All @@ -97,42 +73,8 @@ public function initializeLazyObject(): static
return $this;
}

if (!\is_array($state->initializer)) {
if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
$state->initialize($this, '', null);
}

return $this;
}

$values = isset($state->initializer["\0"]) ? null : [];

$class = $this::class;
$properties = (array) $this;
$propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
foreach ($state->initializer as $key => $initializer) {
if (\array_key_exists($key, $properties) || ![$scope, $name, $readonlyScope] = $propertyScopes[$key] ?? null) {
continue;
}
$scope = $readonlyScope ?? ('*' !== $scope ? $scope : $class);

if (null === $values) {
if (!\is_array($values = ($state->initializer["\0"])($this, Registry::$defaultProperties[$class]))) {
throw new \TypeError(sprintf('The lazy-initializer defined for instance of "%s" must return an array, got "%s".', $class, get_debug_type($values)));
}

if (\array_key_exists($key, $properties = (array) $this)) {
continue;
}
}

if (\array_key_exists($key, $values)) {
$accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
$accessor['set']($this, $name, $properties[$key] = $values[$key]);
} else {
$state->initialize($this, $name, $scope);
$properties = (array) $this;
}
if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
$state->initialize($this, '', null);
}

return $this;
Expand Down Expand Up @@ -163,9 +105,8 @@ public function &__get($name): mixed
$scope = Registry::getScope($propertyScopes, $class, $name);
$state = $this->lazyObjectState ?? null;

if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
&& LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $readonlyScope ?? $scope)
) {
if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
$state->initialize($this, $name, $readonlyScope ?? $scope);
goto get_in_scope;
}
}
Expand Down Expand Up @@ -261,9 +202,8 @@ public function __isset($name): bool
$scope = Registry::getScope($propertyScopes, $class, $name);
$state = $this->lazyObjectState ?? null;

if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
&& LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $readonlyScope ?? $scope)
) {
if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
$state->initialize($this, $name, $readonlyScope ?? $scope);
goto isset_in_scope;
}
}
Expand Down Expand Up @@ -362,7 +302,7 @@ public function __destruct()
{
$state = $this->lazyObjectState ?? null;

if ($state && \in_array($state->status, [LazyObjectState::STATUS_UNINITIALIZED_FULL, LazyObjectState::STATUS_UNINITIALIZED_PARTIAL], true)) {
if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state?->status) {
return;
}

Expand All @@ -373,9 +313,7 @@ public function __destruct()

private function setLazyObjectAsInitialized(bool $initialized): void
{
$state = $this->lazyObjectState ?? null;

if ($state && !\is_array($state->initializer)) {
if ($state = $this->lazyObjectState ?? null) {
$state->status = $initialized ? LazyObjectState::STATUS_INITIALIZED_FULL : LazyObjectState::STATUS_UNINITIALIZED_FULL;
}
}
Expand Down
Loading
0