16
16
/**
17
17
* @author Nicolas Grekas <[email protected] >
18
18
*/
19
- class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializable
19
+ class Data implements \ArrayAccess, \Countable, \IteratorAggregate
20
20
{
21
21
private $ data ;
22
22
private $ position = 0 ;
@@ -72,7 +72,7 @@ public function getValue($recursive = false)
72
72
if ($ item instanceof Stub && Stub::TYPE_REF === $ item ->type && !$ item ->position ) {
73
73
$ item = $ item ->value ;
74
74
}
75
- if (!$ item instanceof Stub) {
75
+ if (!( $ item = $ this -> getStub ( $ item )) instanceof Stub) {
76
76
return $ item ;
77
77
}
78
78
if (Stub::TYPE_STRING === $ item ->type ) {
@@ -82,20 +82,20 @@ public function getValue($recursive = false)
82
82
$ children = $ item ->position ? $ this ->data [$ item ->position ] : array ();
83
83
84
84
foreach ($ children as $ k => $ v ) {
85
- if ($ recursive && !$ v instanceof Stub) {
85
+ if ($ recursive && !( $ v = $ this -> getStub ( $ v )) instanceof Stub) {
86
86
continue ;
87
87
}
88
88
$ children [$ k ] = clone $ this ;
89
89
$ children [$ k ]->key = $ k ;
90
90
$ children [$ k ]->position = $ item ->position ;
91
91
92
92
if ($ recursive ) {
93
- if ($ v instanceof Stub && Stub ::TYPE_REF === $ v ->type && $ v -> value instanceof Stub) {
93
+ if (Stub::TYPE_REF === $ v ->type && ( $ v = $ this -> getStub ( $ v -> value )) instanceof Stub) {
94
94
$ recursive = (array ) $ recursive ;
95
- if (isset ($ recursive [$ v ->value -> position ])) {
95
+ if (isset ($ recursive [$ v ->position ])) {
96
96
continue ;
97
97
}
98
- $ recursive [$ v ->value -> position ] = true ;
98
+ $ recursive [$ v ->position ] = true ;
99
99
}
100
100
$ children [$ k ] = $ children [$ k ]->getValue ($ recursive );
101
101
}
@@ -123,7 +123,7 @@ public function getIterator()
123
123
public function __get ($ key )
124
124
{
125
125
if (null !== $ data = $ this ->seek ($ key )) {
126
- $ item = $ data ->data [$ data ->position ][$ data ->key ];
126
+ $ item = $ this -> getStub ( $ data ->data [$ data ->position ][$ data ->key ]) ;
127
127
128
128
return $ item instanceof Stub || array () === $ item ? $ data : $ item ;
129
129
}
@@ -236,7 +236,7 @@ public function seek($key)
236
236
if ($ item instanceof Stub && Stub::TYPE_REF === $ item ->type && !$ item ->position ) {
237
237
$ item = $ item ->value ;
238
238
}
239
- if (!$ item instanceof Stub || !$ item ->position ) {
239
+ if (!( $ item = $ this -> getStub ( $ item )) instanceof Stub || !$ item ->position ) {
240
240
return ;
241
241
}
242
242
$ keys = array ($ key );
@@ -278,57 +278,6 @@ public function dump(DumperInterface $dumper)
278
278
$ this ->dumpItem ($ dumper , new Cursor (), $ refs , $ this ->data [$ this ->position ][$ this ->key ]);
279
279
}
280
280
281
- /**
282
- * @internal
283
- */
284
- public function serialize ()
285
- {
286
- $ data = $ this ->data ;
287
-
288
- foreach ($ data as $ i => $ values ) {
289
- foreach ($ values as $ k => $ v ) {
290
- if ($ v instanceof Stub) {
291
- if (Stub::TYPE_ARRAY === $ v ->type ) {
292
- $ v = self ::mapStubConsts ($ v , false );
293
- $ data [$ i ][$ k ] = array ($ v ->class , $ v ->position , $ v ->cut );
294
- } else {
295
- $ v = self ::mapStubConsts ($ v , false );
296
- $ data [$ i ][$ k ] = array ($ v ->class , $ v ->position , $ v ->cut , $ v ->type , $ v ->value , $ v ->handle , $ v ->refCount , $ v ->attr );
297
- }
298
- }
299
- }
300
- }
301
-
302
- return serialize (array ($ data , $ this ->position , $ this ->key , $ this ->maxDepth , $ this ->maxItemsPerDepth , $ this ->useRefHandles ));
303
- }
304
-
305
- /**
306
- * @internal
307
- */
308
- public function unserialize ($ serialized )
309
- {
310
- list ($ data , $ this ->position , $ this ->key , $ this ->maxDepth , $ this ->maxItemsPerDepth , $ this ->useRefHandles ) = unserialize ($ serialized );
311
-
312
- foreach ($ data as $ i => $ values ) {
313
- foreach ($ values as $ k => $ v ) {
314
- if ($ v && is_array ($ v )) {
315
- $ s = new Stub ();
316
- if (3 === count ($ v )) {
317
- $ s ->type = Stub::TYPE_ARRAY ;
318
- $ s = self ::mapStubConsts ($ s , false );
319
- list ($ s ->class , $ s ->position , $ s ->cut ) = $ v ;
320
- $ s ->value = $ s ->cut + count ($ data [$ s ->position ]);
321
- } else {
322
- list ($ s ->class , $ s ->position , $ s ->cut , $ s ->type , $ s ->value , $ s ->handle , $ s ->refCount , $ s ->attr ) = $ v ;
323
- }
324
- $ data [$ i ][$ k ] = self ::mapStubConsts ($ s , true );
325
- }
326
- }
327
- }
328
-
329
- $ this ->data = $ data ;
330
- }
331
-
332
281
/**
333
282
* Depth-first dumping of items.
334
283
*
@@ -346,7 +295,10 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
346
295
347
296
if (!$ item instanceof Stub) {
348
297
$ cursor ->attr = array ();
349
- $ type = gettype ($ item );
298
+ $ type = \gettype ($ item );
299
+ if ($ item && 'array ' === $ type ) {
300
+ $ item = $ this ->getStub ($ item );
301
+ }
350
302
} elseif (Stub::TYPE_REF === $ item ->type ) {
351
303
if ($ item ->handle ) {
352
304
if (!isset ($ refs [$ r = $ item ->handle - (PHP_INT_MAX >> 1 )])) {
@@ -360,7 +312,7 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
360
312
}
361
313
$ cursor ->attr = $ item ->attr ;
362
314
$ type = $ item ->class ?: gettype ($ item ->value );
363
- $ item = $ item ->value ;
315
+ $ item = $ this -> getStub ( $ item ->value ) ;
364
316
}
365
317
if ($ item instanceof Stub) {
366
318
if ($ item ->refCount ) {
@@ -458,21 +410,20 @@ private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCu
458
410
return $ hashCut ;
459
411
}
460
412
461
- private static function mapStubConsts ( Stub $ stub , $ resolve )
413
+ private function getStub ( $ item )
462
414
{
463
- static $ stubConstIndexes , $ stubConstValues ;
464
-
465
- if (null === $ stubConstIndexes ) {
466
- $ r = new \ReflectionClass (Stub::class);
467
- $ stubConstIndexes = array_flip (array_values ($ r ->getConstants ()));
468
- $ stubConstValues = array_flip ($ stubConstIndexes );
415
+ if (!$ item || !\is_array ($ item )) {
416
+ return $ item ;
469
417
}
470
418
471
- $ map = $ resolve ? $ stubConstValues : $ stubConstIndexes ;
472
-
473
- $ stub = clone $ stub ;
474
- $ stub ->type = $ map [$ stub ->type ];
475
- $ stub ->class = isset ($ map [$ stub ->class ]) ? $ map [$ stub ->class ] : $ stub ->class ;
419
+ $ stub = new Stub ();
420
+ $ stub ->type = Stub::TYPE_ARRAY ;
421
+ foreach ($ item as $ stub ->class => $ stub ->position ) {
422
+ }
423
+ if (isset ($ item [0 ])) {
424
+ $ stub ->cut = $ item [0 ];
425
+ }
426
+ $ stub ->value = $ stub ->cut + \count ($ this ->data [$ stub ->position ]);
476
427
477
428
return $ stub ;
478
429
}
0 commit comments