17
17
18
18
trait LazyGhostTrait
19
19
{
20
- private int $ lazyObjectId ;
20
+ private LazyObjectState $ lazyObjectState ;
21
21
22
22
/**
23
23
* Creates a lazy-loading ghost instance.
@@ -47,15 +47,14 @@ public static function createLazyGhost(\Closure|array $initializer, array $skipp
47
47
$ onlyProperties = null === $ skippedProperties && \is_array ($ initializer ) ? $ initializer : null ;
48
48
49
49
if (self ::class !== $ class = $ instance ? $ instance ::class : static ::class) {
50
- $ skippedProperties ["\0" .self ::class."\0 lazyObjectId " ] = true ;
50
+ $ skippedProperties ["\0" .self ::class."\0 lazyObjectState " ] = true ;
51
51
} elseif (\defined ($ class .'::LAZY_OBJECT_PROPERTY_SCOPES ' )) {
52
52
Hydrator::$ propertyScopes [$ class ] ??= $ class ::LAZY_OBJECT_PROPERTY_SCOPES ;
53
53
}
54
54
55
55
$ instance ??= (Registry::$ classReflectors [$ class ] ??= new \ReflectionClass ($ class ))->newInstanceWithoutConstructor ();
56
56
Registry::$ defaultProperties [$ class ] ??= (array ) $ instance ;
57
- $ instance ->lazyObjectId = $ id = spl_object_id ($ instance );
58
- Registry::$ states [$ id ] = new LazyObjectState ($ initializer , $ skippedProperties ??= []);
57
+ $ instance ->lazyObjectState = new LazyObjectState ($ initializer , $ skippedProperties ??= []);
59
58
60
59
foreach (Registry::$ classResetters [$ class ] ??= Registry::getClassResetters ($ class ) as $ reset ) {
61
60
$ reset ($ instance , $ skippedProperties , $ onlyProperties );
@@ -71,7 +70,7 @@ public static function createLazyGhost(\Closure|array $initializer, array $skipp
71
70
*/
72
71
public function isLazyObjectInitialized (bool $ partial = false ): bool
73
72
{
74
- if (!$ state = Registry:: $ states [ $ this ->lazyObjectId ?? '' ] ?? null ) {
73
+ if (!$ state = $ this ->lazyObjectState ?? null ) {
75
74
return true ;
76
75
}
77
76
@@ -101,7 +100,7 @@ public function isLazyObjectInitialized(bool $partial = false): bool
101
100
*/
102
101
public function initializeLazyObject (): static
103
102
{
104
- if (!$ state = Registry:: $ states [ $ this ->lazyObjectId ?? '' ] ?? null ) {
103
+ if (!$ state = $ this ->lazyObjectState ?? null ) {
105
104
return $ this ;
106
105
}
107
106
@@ -151,7 +150,7 @@ public function initializeLazyObject(): static
151
150
*/
152
151
public function resetLazyObject (): bool
153
152
{
154
- if (!$ state = Registry:: $ states [ $ this ->lazyObjectId ?? '' ] ?? null ) {
153
+ if (!$ state = $ this ->lazyObjectState ?? null ) {
155
154
return false ;
156
155
}
157
156
@@ -169,7 +168,7 @@ public function &__get($name): mixed
169
168
170
169
if ([$ class , , $ readonlyScope ] = $ propertyScopes [$ name ] ?? null ) {
171
170
$ scope = Registry::getScope ($ propertyScopes , $ class , $ name );
172
- $ state = Registry:: $ states [ $ this ->lazyObjectId ?? '' ] ?? null ;
171
+ $ state = $ this ->lazyObjectState ?? null ;
173
172
174
173
if ($ state && (null === $ scope || isset ($ propertyScopes ["\0$ scope \0$ name " ]))
175
174
&& LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $ state ->initialize ($ this , $ name , $ readonlyScope ?? $ scope )
@@ -215,7 +214,7 @@ public function __set($name, $value): void
215
214
216
215
if ([$ class , , $ readonlyScope ] = $ propertyScopes [$ name ] ?? null ) {
217
216
$ scope = Registry::getScope ($ propertyScopes , $ class , $ name , $ readonlyScope );
218
- $ state = Registry:: $ states [ $ this ->lazyObjectId ?? '' ] ?? null ;
217
+ $ state = $ this ->lazyObjectState ?? null ;
219
218
220
219
if ($ state && ($ readonlyScope === $ scope || isset ($ propertyScopes ["\0$ scope \0$ name " ]))) {
221
220
if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $ state ->status ) {
@@ -248,7 +247,7 @@ public function __isset($name): bool
248
247
249
248
if ([$ class , , $ readonlyScope ] = $ propertyScopes [$ name ] ?? null ) {
250
249
$ scope = Registry::getScope ($ propertyScopes , $ class , $ name );
251
- $ state = Registry:: $ states [ $ this ->lazyObjectId ?? '' ] ?? null ;
250
+ $ state = $ this ->lazyObjectState ?? null ;
252
251
253
252
if ($ state && (null === $ scope || isset ($ propertyScopes ["\0$ scope \0$ name " ]))
254
253
&& LazyObjectState::STATUS_UNINITIALIZED_PARTIAL !== $ state ->initialize ($ this , $ name , $ readonlyScope ?? $ scope )
@@ -278,7 +277,7 @@ public function __unset($name): void
278
277
279
278
if ([$ class , , $ readonlyScope ] = $ propertyScopes [$ name ] ?? null ) {
280
279
$ scope = Registry::getScope ($ propertyScopes , $ class , $ name , $ readonlyScope );
281
- $ state = Registry:: $ states [ $ this ->lazyObjectId ?? '' ] ?? null ;
280
+ $ state = $ this ->lazyObjectState ?? null ;
282
281
283
282
if ($ state && ($ readonlyScope === $ scope || isset ($ propertyScopes ["\0$ scope \0$ name " ]))) {
284
283
if (LazyObjectState::STATUS_UNINITIALIZED_FULL === $ state ->status ) {
@@ -306,8 +305,8 @@ public function __unset($name): void
306
305
307
306
public function __clone (): void
308
307
{
309
- if ($ state = Registry:: $ states [ $ this ->lazyObjectId ?? '' ] ?? null ) {
310
- Registry:: $ states [ $ this ->lazyObjectId = spl_object_id ( $ this )] = clone $ state ;
308
+ if ($ state = $ this ->lazyObjectState ?? null ) {
309
+ $ this ->lazyObjectState = clone $ state ;
311
310
}
312
311
313
312
if ((Registry::$ parentMethods [self ::class] ??= Registry::getParentMethods (self ::class))['clone ' ]) {
@@ -325,7 +324,7 @@ public function __serialize(): array
325
324
$ this ->initializeLazyObject ();
326
325
$ properties = (array ) $ this ;
327
326
}
328
- unset($ properties ["\0$ class \0 lazyObjectId " ]);
327
+ unset($ properties ["\0$ class \0 lazyObjectState " ]);
329
328
330
329
if (Registry::$ parentMethods [$ class ]['serialize ' ] || !Registry::$ parentMethods [$ class ]['sleep ' ]) {
331
330
return $ properties ;
@@ -349,26 +348,20 @@ public function __serialize(): array
349
348
350
349
public function __destruct ()
351
350
{
352
- $ state = Registry:: $ states [ $ this ->lazyObjectId ?? '' ] ?? null ;
351
+ $ state = $ this ->lazyObjectState ?? null ;
353
352
354
- try {
355
- if ($ state && \in_array ($ state ->status , [LazyObjectState::STATUS_UNINITIALIZED_FULL , LazyObjectState::STATUS_UNINITIALIZED_PARTIAL ], true )) {
356
- return ;
357
- }
353
+ if ($ state && \in_array ($ state ->status , [LazyObjectState::STATUS_UNINITIALIZED_FULL , LazyObjectState::STATUS_UNINITIALIZED_PARTIAL ], true )) {
354
+ return ;
355
+ }
358
356
359
- if ((Registry::$ parentMethods [self ::class] ??= Registry::getParentMethods (self ::class))['destruct ' ]) {
360
- parent ::__destruct ();
361
- }
362
- } finally {
363
- if ($ state ) {
364
- unset(Registry::$ states [$ this ->lazyObjectId ]);
365
- }
357
+ if ((Registry::$ parentMethods [self ::class] ??= Registry::getParentMethods (self ::class))['destruct ' ]) {
358
+ parent ::__destruct ();
366
359
}
367
360
}
368
361
369
362
private function setLazyObjectAsInitialized (bool $ initialized ): void
370
363
{
371
- $ state = Registry:: $ states [ $ this ->lazyObjectId ?? '' ] ;
364
+ $ state = $ this ->lazyObjectState ?? null ;
372
365
373
366
if ($ state && !\is_array ($ state ->initializer )) {
374
367
$ state ->status = $ initialized ? LazyObjectState::STATUS_INITIALIZED_FULL : LazyObjectState::STATUS_UNINITIALIZED_FULL ;
0 commit comments