@@ -23,22 +23,16 @@ trait LazyGhostTrait
2323 /**
2424 * Creates a lazy-loading ghost instance.
2525 *
26- * Skipped properties should be indexed by their array-cast identifier , see
26+ * Skipped properties should be indexed by their array-cast name , see
2727 * https://php.net/manual/language.types.array#language.types.array.casting
2828 *
2929 * @param (\Closure(static):void $initializer The closure should initialize the object it receives as argument
3030 * @param array<string, true>|null $skippedProperties An array indexed by the properties to skip, a.k.a. the ones
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