8000 [VarExporter] Drop support for partially initialized lazy object · symfony/symfony@27650ef · GitHub
[go: up one dir, main page]

Skip to content

Commit 27650ef

Browse files
[VarExporter] Drop support for partially initialized lazy object
1 parent f581151 commit 27650ef

File tree

6 files changed

+27
-319
lines changed

6 files changed

+27
-319
lines changed

UPGRADE-7.0.md

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Components
4646
* [Translation](#Translation)
4747
* [Validator](#Validator)
4848
* [VarDumper](#VarDumper)
49+
* [VarExporter](#VarExporter)
4950
* [Workflow](#Workflow)
5051
* [Yaml](#Yaml)
5152

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

612+
VarExporter
613+
-----------
614+
615+
* Remove support for per-property lazy-initializers
616+
611617
Workflow
612618
--------
613619

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,18 @@ public static function getClassResetters($class)
6767

6868
$resetters = [];
6969
foreach ($classProperties as $scope => $properties) {
70-
$resetters[] = \Closure::bind(static function ($instance, $skippedProperties, $onlyProperties = null) use ($properties) {
70+
$resetters[] = \Closure::bind(static function ($instance, $skippedProperties) use ($properties) {
7171
foreach ($properties as $name => $key) {
72-
if (!\array_key_exists($key, $skippedProperties) && (null === $onlyProperties || \array_key_exists($key, $onlyProperties))) {
72+
if (!\array_key_exists($key, $skippedProperties)) {
7373
unset($instance->$name);
7474
}
7575
}
7676
}, null, $scope);
7777
}
7878

79-
$resetters[] = static function ($instance, $skippedProperties, $onlyProperties = null) {
79+
$resetters[] = static function ($instance, $skippedProperties) {
8080
foreach ((array) $instance as $name => $value) {
81-
if ("\0" !== ($name[0] ?? '') && !\array_key_exists($name, $skippedProperties) && (null === $onlyProperties || \array_key_exists($name, $onlyProperties))) {
81+
if ("\0" !== ($name[0] ?? '') && !\array_key_exists($name, $skippedProperties)) {
8282
unset($instance->$name);
8383
}
8484
}

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

+4-41
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class LazyObjectState
3939

4040
public object $realInstance;
4141

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

4848
public function initialize($instance, $propertyName, $propertyScope)
@@ -51,42 +51,6 @@ public function initialize($instance, $propertyName, $propertyScope)
5151
return self::STATUS_INITIALIZED_FULL;
5252
}
5353

54-
if (\is_array($this->initializer)) {
55-
$class = $instance::class;
56-
$propertyScope ??= $class;
57-
$propertyScopes = Hydrator::$propertyScopes[$class];
58-
$propertyScopes[$k = "\0$propertyScope\0$propertyName"] ?? $propertyScopes[$k = "\0*\0$propertyName"] ?? $k = $propertyName;
59-
60-
if ($initializer = $this->initializer[$k] ?? null) {
61-
$value = $initializer(...[$instance, $propertyName, $propertyScope, LazyObjectRegistry::$defaultProperties[$class][$k] ?? null]);
62-
$accessor = LazyObjectRegistry::$classAccessors[$propertyScope] ??= LazyObjectRegistry::getClassAccessors($propertyScope);
63-
$accessor['set']($instance, $propertyName, $value);
64-
65-
return $this->status = self::STATUS_INITIALIZED_PARTIAL;
66-
}
67-
68-
$status = self::STATUS_UNINITIALIZED_PARTIAL;
69-
70-
if ($initializer = $this->initializer["\0"] ?? null) {
71-
if (!\is_array($values = $initializer($instance, LazyObjectRegistry::$defaultProperties[$class]))) {
72-
throw new \TypeError(sprintf('The lazy-initializer defined for instance of "%s" must return an array, got "%s".', $class, get_debug_type($values)));
73-
}
74-
$properties = (array) $instance;
75-
foreach ($values as $key => $value) {
76-
if ($k === $key) {
77-
$status = self::STATUS_INITIALIZED_PARTIAL;
78-
}
79-
if (!\array_key_exists($key, $properties) && [$scope, $name, $readonlyScope] = $propertyScopes[$key] ?? null) {
80-
$scope = $readonlyScope ?? ('*' !== $scope ? $scope : $class);
81-
$accessor = LazyObjectRegistry::$classAccessors[$scope] ??= LazyObjectRegistry::getClassAccessors($scope);
82-
$accessor['set']($instance, $name, $value);
83-
}
84-
}
85-
}
86-
87-
return $status;
88-
}
89-
9054
$this->status = self::STATUS_INITIALIZED_FULL;
9155

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

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

12487
foreach (LazyObjectRegistry::$classResetters[$class] as $reset) {
125-
$reset($instance, $skippedProperties, $onlyProperties);
88+
$reset($instance, $skippedProperties);
12689
}
12790

128-
$this->status = self::STATUS_INITIALIZED_FULL === $this->status ? self::STATUS_UNINITIALIZED_FULL : self::STATUS_UNINITIALIZED_PARTIAL;
91+
$this->status = self::STATUS_UNINITIALIZED_FULL;
12992
}
13093
}

src/Symfony/Component/VarExporter/LazyGhostTrait.php

+11-73
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,8 @@ trait LazyGhostTrait
3131
* that the initializer doesn't initialize, if any
3232
* @param static|null $instance
3333
*/
34-
public static function createLazyGhost(\Closure|array $initializer, array $skippedProperties = null, object $instance = null): static
34+
public static function createLazyGhost(\Closure $initializer, array $skippedProperties = null, object $instance = null): static
3535
{
36-
if (\is_array($initializer)) {
37-
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.');
38-
}
39-
40-
$onlyProperties = null === $skippedProperties && \is_array($initializer) ? $initializer : null;
41-
4236
if (self::class !== $class = $instance ? $instance::class : static::class) {
4337
$skippedProperties["\0".self::class."\0lazyObjectState"] = true;
4438
} elseif (\defined($class.'::LAZY_OBJECT_PROPERTY_SCOPES')) {
@@ -50,7 +44,7 @@ public static function createLazyGhost(\Closure|array $initializer, array $skipp
5044
$instance->lazyObjectState = new LazyObjectState($initializer, $skippedProperties ??= []);
5145

5246
foreach (Registry::$classResetters[$class] ??= Registry::getClassResetters($class) as $reset) {
53-
$reset($instance, $skippedProperties, $onlyProperties);
47+
$reset($instance, $skippedProperties);
5448
}
5549

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

70-
if (!\is_array($state->initializer)) {
71-
return LazyObjectState::STATUS_INITIALIZED_FULL === $state->status;
72-
}
73-
74-
$class = $this::class;
75-
$properties = (array) $this;
76-
77-
if ($partial) {
78-
return (bool) array_intersect_key($state->initializer, $properties);
79-
}
80-
81-
$propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
82-
foreach ($state->initializer as $key => $initializer) {
83-
if (!\array_key_exists($key, $properties) && isset($propertyScopes[$key])) {
84-
return false;
85-
}
86-
}
87-
88-
return true;
64+
return LazyObjectState::STATUS_INITIALIZED_FULL === $state->status;
8965
}
9066

9167
/**
@@ -97,42 +73,8 @@ public function initializeLazyObject(): static
9773
return $this;
9874
}
9975

100-
if (!\is_array($state->initializer)) {
101-
if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
102-
$state->initialize($this, '', null);
103-
}
104-
105-
return $this;
106-
}
107-
108-
$values = isset($state->initializer["\0"]) ? null : [];
109-
110-
$class = $this::class;
111-
$properties = (array) $this;
112-
$propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class);
113-
foreach ($state->initializer as $key => $initializer) {
114-
if (\array_key_exists($key, $properties) || ![$scope, $name, $readonlyScope] = $propertyScopes[$key] ?? null) {
115-
continue;
116-
}
117-
$scope = $readonlyScope ?? ('*' !== $scope ? $scope : $class);
118-
119-
if (null === $values) {
120-
if (!\is_array($values = ($state->initializer["\0"])($this, Registry::$defaultProperties[$class]))) {
121-
throw new \TypeError(sprintf('The lazy-initializer defined for instance of "%s" must return an array, got "%s".', $class, get_debug_type($values)));
122-
}
123-
124-
if (\array_key_exists($key, $properties = (array) $this)) {
125-
continue;
126-
}
127-
}
128-
129-
if (\array_key_exists($key, $values)) {
130-
$accessor = Registry::$classAccessors[$scope] ??= Registry::getClassAccessors($scope);
131-
$accessor['set']($this, $name, $properties[$key] = $values[$key]);
132-
} else {
133-
$state->initialize($this, $name, $scope);
134-
$properties = (array) $this;
135-
}
76+
if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $state->status) {
77+
$state->initialize($this, '', null);
13678
}
13779

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

166-
if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
167-
&& LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $readonlyScope ?? $scope)
168-
) {
108+
if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
109+
$state->initialize($this, $name, $readonlyScope ?? $scope);
169110
goto get_in_scope;
170111
}
171112
}
@@ -261,9 +202,8 @@ public function __isset($name): bool
261202
$scope = Registry::getScope($propertyScopes, $class, $name);
262203
$state = $this->lazyObjectState ?? null;
263204

264-
if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))
265-
&& LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $state->initialize($this, $name, $readonlyScope ?? $scope)
266-
) {
205+
if ($state && (null === $scope || isset($propertyScopes["\0$scope\0$name"]))) {
206+
$state->initialize($this, $name, $readonlyScope ?? $scope);
267207
goto isset_in_scope;
268208
}
269209
}
@@ -362,7 +302,7 @@ public function __destruct()
362302
{
363303
$state = $this->lazyObjectState ?? null;
364304

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

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

374314
private function setLazyObjectAsInitialized(bool $initialized): void
375315
{
376-
$state = $this->lazyObjectState ?? null;
377-
378-
if ($state && !\is_array($state->initializer)) {
316+
if ($state = $this->lazyObjectState ?? null) {
379317
$state->status = $initialized ? LazyObjectState::STATUS_INITIALIZED_FULL : LazyObjectState::STATUS_UNINITIALIZED_FULL;
380318
}
381319
}

0 commit comments

Comments
 (0)
0