15
15
use Symfony \Component \VarExporter \Internal \Hydrator ;
16
16
use Symfony \Component \VarExporter \Internal \LazyObjectRegistry as Registry ;
17
17
use Symfony \Component \VarExporter \Internal \LazyObjectState ;
18
+ use Symfony \Component \VarExporter \Internal \LazyObjectTrait ;
18
19
19
20
trait LazyProxyTrait
20
21
{
21
- private LazyObjectState $ lazyObjectState ;
22
- private object $ lazyObjectReal ;
22
+ use LazyObjectTrait;
23
23
24
24
/**
25
25
* Creates a lazy-loading virtual proxy.
26
26
*
27
27
* @param \Closure():object $initializer Returns the proxied object
28
- * @param static|null $instance
28
+ * @param static|null $instance
29
29
*/
30
30
public static function createLazyProxy (\Closure $ initializer , object $ instance = null ): static
31
31
{
@@ -52,20 +52,16 @@ public static function createLazyProxy(\Closure $initializer, object $instance =
52
52
*/
53
53
public function isLazyObjectInitialized (bool $ partial = false ): bool
54
54
{
55
- if (!isset ($ this ->lazyObjectState ) || Registry::$ noInitializerState === $ this ->lazyObjectState ) {
56
- return true ;
57
- }
58
-
59
- return \array_key_exists ("\0" .self ::class."\0lazyObjectReal " , (array ) $ this );
55
+ return !isset ($ this ->lazyObjectState ) || isset ($ this ->lazyObjectState ->realInstance ) || Registry::$ noInitializerState === $ this ->lazyObjectState ->initializer ;
60
56
}
61
57
62
58
/**
63
59
* Forces initialization of a lazy object and returns it.
64
60
*/
65
61
public function initializeLazyObject (): parent
66
62
{
67
- if (isset ( $ this ->lazyObjectReal ) ) {
68
- return $ this -> lazyObjectReal ;
63
+ if ($ state = $ this ->lazyObjectState ?? null ) {
64
+ return $ state -> realInstance ??= ( $ state -> initializer )() ;
69
65
}
70
66
71
67
return $ this ;
@@ -76,13 +72,11 @@ public function initializeLazyObject(): parent
76
72
*/
77
73
public function resetLazyObject (): bool
78
74
{
79
- if (!isset ($ this ->lazyObjectState ) || Registry::$ noInitializerState === $ this ->lazyObjectState ) {
75
+ if (!isset ($ this ->lazyObjectState ) || Registry::$ noInitializerState === $ this ->lazyObjectState -> initializer ) {
80
76
return false ;
81
77
}
82
78
83
- if (\array_key_exists ("\0" .self ::class."\0lazyObjectReal " , (array ) $ this )) {
84
- unset($ this ->lazyObjectReal );
85
- }
79
+ unset($ this ->lazyObjectState ->realInstance );
86
80
87
81
return true ;
88
82
}
@@ -98,23 +92,16 @@ public function &__get($name): mixed
98
92
99
93
if (null === $ scope || isset ($ propertyScopes ["\0$ scope \0$ name " ])) {
100
94
if ($ state = $ this ->lazyObjectState ?? null ) {
101
- if ('lazyObjectReal ' === $ name && self ::class === $ scope ) {
102
- $ this ->lazyObjectReal = ($ state ->initializer )();
103
-
104
- return $ this ->lazyObjectReal ;
105
- }
106
- if (isset ($ this ->lazyObjectReal )) {
107
- $ instance = $ this ->lazyObjectReal ;
108
- }
95
+ $ instance = $ state ->realInstance ??= ($ state ->initializer )();
109
96
}
110
97
$ parent = 2 ;
111
98
goto get_in_scope;
112
99
}
113
100
}
114
101
$ parent = (Registry::$ parentMethods [self ::class] ??= Registry::getParentMethods (self ::class))['get ' ];
115
102
116
- if (isset ( $ this ->lazyObjectReal ) ) {
117
- $ instance = $ this -> lazyObjectReal ;
103
+ if ($ state = $ this ->lazyObjectState ?? null ) {
104
+ $ instance = $ state -> realInstance ??= ( $ state -> initializer )() ;
118
105
} else {
119
106
if (2 === $ parent ) {
120
107
return parent ::__get ($ name );
@@ -174,22 +161,15 @@ public function __set($name, $value): void
174
161
$ scope = Registry::getScope ($ propertyScopes , $ class , $ name , $ readonlyScope );
175
162
176
163
if ($ readonlyScope === $ scope || isset ($ propertyScopes ["\0$ scope \0$ name " ])) {
177
- if (isset ($ this ->lazyObjectState )) {
178
- if ('lazyObjectReal ' === $ name && self ::class === $ scope ) {
179
- $ this ->lazyObjectReal = $ value ;
180
-
181
- return ;
182
- }
183
- if (isset ($ this ->lazyObjectReal )) {
184
- $ instance = $ this ->lazyObjectReal ;
185
- }
164
+ if ($ state = $ this ->lazyObjectState ?? null ) {
165
+ $ instance = $ state ->realInstance ??= ($ state ->initializer )();
186
166
}
187
167
goto set_in_scope;
188
168
}
189
169
}
190
170
191
- if (isset ( $ this ->lazyObjectReal ) ) {
192
- $ instance = $ this -> lazyObjectReal ;
171
+ if ($ state = $ this ->lazyObjectState ?? null ) {
172
+ $ instance = $ state -> realInstance ??= ( $ state -> initializer )() ;
193
173
} elseif ((Registry::$ parentMethods [self ::class] ??= Registry::getParentMethods (self ::class))['set ' ]) {
194
174
parent ::__set ($ name , $ value );
195
175
@@ -216,22 +196,15 @@ public function __isset($name): bool
216
196
$ scope = Registry::getScope ($ propertyScopes , $ class , $ name );
217
197
218
198
if (null === $ scope || isset ($ propertyScopes ["\0$ scope \0$ name " ])) {
219
- if (isset ($ this ->lazyObjectState )) {
220
- if ('lazyObjectReal ' === $ name && self ::class === $ scope ) {
221
- $ state = $ this ->lazyObjectState ?? null ;
222
-
223
- return null !== $ this ->lazyObjectReal = $ state ? ($ state ->initializer )() : null ;
224
- }
225
- if (isset ($ this ->lazyObjectReal )) {
226
- $ instance = $ this ->lazyObjectReal ;
227
- }
199
+ if ($ state = $ this ->lazyObjectState ?? null ) {
200
+ $ instance = $ state ->realInstance ??= ($ state ->initializer )();
228
201
}
229
202
goto isset_in_scope;
230
203
}
231
204
}
232
205
233
- if (isset ( $ this ->lazyObjectReal ) ) {
234
- $ instance = $ this -> lazyObjectReal ;
206
+ if ($ state = $ this ->lazyObjectState ?? null ) {
207
+ $ instance = $ state -> realInstance ??= ( $ state -> initializer )() ;
235
208
} elseif ((Registry::$ parentMethods [self ::class] ??= Registry::getParentMethods (self ::class))['isset ' ]) {
236
209
return parent ::__isset ($ name );
237
210
}
@@ -256,22 +229,15 @@ public function __unset($name): void
256
229
$ scope = Registry::getScope ($ propertyScopes , $ class , $ name , $ readonlyScope );
257
230
258
231
if ($ readonlyScope === $ scope || isset ($ propertyScopes ["\0$ scope \0$ name " ])) {
259
- if (isset ($ this ->lazyObjectState )) {
260
- if ('lazyObjectReal ' === $ name && self ::class === $ scope ) {
261
- unset($ this ->lazyObjectReal );
262
-
263
- return ;
264
- }
265
- if (isset ($ this ->lazyObjectReal )) {
266
- $ instance = $ this ->lazyObjectReal ;
267
- }
232
+ if ($ state = $ this ->lazyObjectState ?? null ) {
233
+ $ instance = $ state ->realInstance ??= ($ state ->initializer )();
268
234
}
269
235
goto unset_in_scope;
270
236
}
271
237
}
272
238
273
- if (isset ( $ this ->lazyObjectReal ) ) {
274
- $ instance = $ this -> lazyObjectReal ;
239
+ if ($ state = $ this ->lazyObjectState ?? null ) {
240
+ $ instance = $ state -> realInstance ??= ( $ state -> initializer )() ;
275
241
} elseif ((Registry::$ parentMethods [self ::class] ??= Registry::getParentMethods (self ::class))['unset ' ]) {
276
242
parent ::__unset ($ name );
277
243
@@ -298,26 +264,30 @@ public function __clone(): void
298
264
return ;
299
265
}
300
266
301
- if (\array_key_exists ("\0" .self ::class."\0lazyObjectReal " , (array ) $ this )) {
302
- $ this ->lazyObjectReal = clone $ this ->lazyObjectReal ;
303
- }
304
- if ($ state = $ this ->lazyObjectState ?? null ) {
305
- $ this ->lazyObjectState = clone $ state ;
267
+ $ this ->lazyObjectState = clone $ this ->lazyObjectState ;
268
+
269
+ if (isset ($ this ->lazyObjectState ->realInstance )) {
270
+ $ this ->lazyObjectState ->realInstance = clone $ this ->lazyObjectState ->realInstance ;
306
271
}
307
272
}
308
273
309
274
public function __serialize (): array
310
275
{
311
276
$ class = self ::class;
277
+ $ state = $ this ->lazyObjectState ?? null ;
312
278
313
- if (!isset ( $ this -> lazyObjectReal ) && (Registry::$ parentMethods [$ class ] ??= Registry::getParentMethods ($ class ))['serialize ' ]) {
279
+ if (!$ state && (Registry::$ parentMethods [$ class ] ??= Registry::getParentMethods ($ class ))['serialize ' ]) {
314
280
$ properties = parent ::__serialize ();
315
281
} else {
316
282
$ properties = (array ) $ this ;
283
+
284
+ if ($ state ) {
285
+ unset($ properties ["\0$ class \0lazyObjectState " ]);
286
+ $ properties ["\0$ class \0lazyObjectReal " ] = $ state ->realInstance ??= ($ state ->initializer )();
287
+ }
317
288
}
318
- unset($ properties ["\0$ class \0lazyObjectState " ]);
319
289
320
- if (isset ( $ this -> lazyObjectReal ) || Registry::$ parentMethods [$ class ]['serialize ' ] || !Registry::$ parentMethods [$ class ]['sleep ' ]) {
290
+ if ($ state || Registry::$ parentMethods [$ class ]['serialize ' ] || !Registry::$ parentMethods [$ class ]['sleep ' ]) {
321
291
return $ properties ;
322
292
}
323
293
@@ -341,17 +311,18 @@ public function __unserialize(array $data): void
341
311
{
342
312
$ class = self ::class;
343
313
344
- if (isset ($ data ["\0$ class \0lazyObjectReal " ])) {
314
+ if ($ instance = $ data ["\0$ class \0lazyObjectReal " ] ?? null ) {
315
+ unset($ data ["\0$ class \0lazyObjectReal " ]);
316
+
345
317
foreach (Registry::$ classResetters [$ class ] ??= Registry::getClassResetters ($ class ) as $ reset ) {
346
318
$ reset ($ this , $ data );
347
319
}
348
320
349
- if (1 < \count ( $ data) ) {
321
+ if ($ data ) {
350
322
PublicHydrator::hydrate ($ this , $ data );
351
- } else {
352
- $ this ->lazyObjectReal = $ data ["\0$ class \0lazyObjectReal " ];
353
323
}
354
- $ this ->lazyObjectState = Registry::$ noInitializerState ??= new LazyObjectState (static fn () => throw new \LogicException ('Lazy proxy has no initializer. ' ));
324
+ $ this ->lazyObjectState = new LazyObjectState (Registry::$ noInitializerState ??= static fn () => throw new \LogicException ('Lazy proxy has no initializer. ' ));
325
+ $ this ->lazyObjectState ->realInstance = $ instance ;
355
326
} elseif ((Registry::$ parentMethods [$ class ] ??= Registry::getParentMethods ($ class ))['unserialize ' ]) {
356
327
parent ::__unserialize ($ data );
357
328
} else {
0 commit comments