@@ -23,22 +23,16 @@ trait LazyGhostTrait
23
23
/**
24
24
* Creates a lazy-loading ghost instance.
25
25
*
26
- * Skipped properties should be indexed by their array-cast identifier , see
26
+ * Skipped properties should be indexed by their array-cast name , see
27
27
* https://php.net/manual/language.types.array#language.types.array.casting
28
28
*
29
29
* @param (\Closure(static):void $initializer The closure should initialize the object it receives as argument
30
30
* @param array<string, true>|null $skippedProperties An array indexed by the properties to skip, a.k.a. the ones
31
31
* that the initializer doesn't initialize, if any
32
32
* @param static|null $instance
33
33
*/
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
35
35
{
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
-
42
36
if (self ::class !== $ class = $ instance ? $ instance ::class : static ::class) {
43
37
$ skippedProperties ["\0" .self ::class."\0lazyObjectState " ] = true ;
44
38
} elseif (\defined ($ class .'::LAZY_OBJECT_PROPERTY_SCOPES ' )) {
@@ -50,7 +44,7 @@ public static function createLazyGhost(\Closure|array $initializer, array $skipp
50
44
$ instance ->lazyObjectState = new LazyObjectState ($ initializer , $ skippedProperties ??= []);
51
45
52
46
foreach (Registry::$ classResetters [$ class ] ??= Registry::getClassResetters ($ class ) as $ reset ) {
53
- $ reset ($ instance , $ skippedProperties, $ onlyProperties );
47
+ $ reset ($ instance , $ skippedProperties );
54
48
}
55
49
56
50
return $ instance ;
@@ -67,25 +61,7 @@ public function isLazyObjectInitialized(bool $partial = false): bool
67
61
return true ;
68
62
}
69
63
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 ;
89
65
}
90
66
91
67
/**
@@ -97,42 +73,8 @@ public function initializeLazyObject(): static
97
73
return $ this ;
98
74
}
99
75
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 );
136
78
}
137
79
138
80
return $ this ;
@@ -163,9 +105,8 @@ public function &__get($name): mixed
163
105
$ scope = Registry::getScope ($ propertyScopes , $ class , $ name );
164
106
$ state = $ this ->lazyObjectState ?? null ;
165
107
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 );
169
110
goto get_in_scope;
170
111
}
171
112
}
@@ -261,9 +202,8 @@ public function __isset($name): bool
261
202
$ scope = Registry::getScope ($ propertyScopes , $ class , $ name );
262
203
$ state = $ this ->lazyObjectState ?? null ;
263
204
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 );
267
207
goto isset_in_scope;
268
208
}
269
209
}
@@ -362,7 +302,7 @@ public function __destruct()
362
302
{
363
303
$ state = $ this ->lazyObjectState ?? null ;
364
304
365
- if ($ state && \in_array ( $ state ->status , [LazyObjectState:: STATUS_UNINITIALIZED_FULL , LazyObjectState:: STATUS_UNINITIALIZED_PARTIAL ], true ) ) {
305
+ if (LazyObjectState:: STATUS_UNINITIALIZED_FULL === $ state? ->status) {
366
306
return ;
367
307
}
368
308
@@ -373,9 +313,7 @@ public function __destruct()
373
313
374
314
private function setLazyObjectAsInitialized (bool $ initialized ): void
375
315
{
376
- $ state = $ this ->lazyObjectState ?? null ;
377
-
378
- if ($ state && !\is_array ($ state ->initializer )) {
316
+ if ($ state = $ this ->lazyObjectState ?? null ) {
379
317
$ state ->status = $ initialized ? LazyObjectState::STATUS_INITIALIZED_FULL : LazyObjectState::STATUS_UNINITIALIZED_FULL ;
380
318
}
381
319
}
0 commit comments