10000 [VarDumper] casters for SPL data structures · symfony/symfony@da3e50a · GitHub
[go: up one dir, main page]

Skip to content

Commit da3e50a

Browse files
[VarDumper] casters for SPL data structures
1 parent c91bc83 commit da3e50a

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
/**
15+
* Casts SPL related classes to array representation.
16+
*
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
*/
19+
class SplCaster
20+
{
21+
public static function castArrayObject(\ArrayObject $c, array $a)
22+
{
23+
$class = get_class($c);
24+
$flags = $c->getFlags();
25+
26+
$b = array(
27+
"\0~\0flag::STD_PROP_LIST" => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
28+
"\0~\0flag::ARRAY_AS_PROPS" => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
29+
"\0~\0iteratorClass" => $c->getIteratorClass(),
30+
"\0~\0storage" => $c->getArrayCopy(),
31+
);
32+
33+
if ($class === 'ArrayObject') {
34+
$a = $b;
35+
} else {
36+
if (!($flags & \ArrayObject::STD_PROP_LIST)) {
37+
$c->setFlags(\ArrayObject::STD_PROP_LIST);
38+
39+
if ($a = (array) $c) {
40+
$class = new \ReflectionClass($class);
41+
foreach ($a as $k => $p) {
42+
if (!isset($k[0]) || ("\0" !== $k[0] && !$class->hasProperty($k))) {
43+
unset($a[$k]);
44+
$a["\0+\0".$k] = $p;
45+
}
46+
}
47+
}
48+
49+
$c->setFlags($flags);
50+
}
51+
52+
$a += $b;
53+
}
54+
55+
return $a;
56+
}
57+
58+
public static function castHeap(\Iterator $c, array $a)
59+
{
60+
$a += array(
61+
"\0~\0heap" => iterator_to_array(clone $c),
62+
);
63+
64+
return $a;
65+
}
66+
67+
public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a)
68+
{
69+
$mode = $c->getIteratorMode();
70+
$c->setIteratorMode(\SplDoublyLinkedList::IT_MODE_KEEP | $mode & ~\SplDoublyLinkedList::IT_MODE_DELETE);
71+
72+
$a += array(
73+
"\0~\0mode" => (($mode & \SplDoublyLinkedList::IT_MODE_LIFO) ? 'IT_MODE_LIFO' : 'IT_MODE_FIFO').' | '.(($mode & \SplDoublyLinkedList::IT_MODE_KEEP) ? 'IT_MODE_KEEP' : 'IT_MODE_DELETE'),
74+
"\0~\0dllist" => iterator_to_array($c),
75+
);
76+
$c->setIteratorMode($mode);
77+
78+
return $a;
79+
}
80+
81+
public static function castFixedArray(\SplFixedArray $c, array $a)
82+
{
83+
$a += array(
84+
"\0~\0storage" => $c->toArray(),
85+
);
86+
87+
return $a;
88+
}
89+
90+
public static function castObjectStorage(\SplObjectStorage $c, array $a)
91+
{
92+
$storage = array();
93+
unset($a["\0+\0\0gcdata"]); // Don't hit https://bugs.php.net/65967
94+
95+
foreach ($c as $obj) {
96+
$storage[spl_object_hash($obj)] = array(
97+
'object' => $obj,
98+
'info' => $c->getInfo(),
99+
);
100+
}
101+
102+
$a += array(
103+
"\0~\0storage" => $storage,
104+
);
105+
106+
return $a;
107+
}
108+
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ abstract class AbstractCloner implements ClonerInterface
2929
'o:Symfony\Component\VarDumper\Exception\ThrowingCasterException'
3030
=> 'Symfony\Component\VarDumper\Caster\ExceptionCaster::castThrowingCasterException',
3131

32+
'o:ArrayObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castArrayObject',
33+
'o:SplDoublyLinkedList' => 'Symfony\Component\VarDumper\Caster\SplCaster::castDoublyLinkedList',
34+
'o:SplFixedArray' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFixedArray',
35+
'o:SplHeap' => 'Symfony\Component\VarDumper\Caster\SplCaster::castHeap',
36+
'o:SplObjectStorage' => 'Symfony\Component\VarDumper\Caster\SplCaster::castObjectStorage',
37+
'o:SplPriorityQueue' => 'Symfony\Component\VarDumper\Caster\SplCaster::castHeap',
38+
3239
'r:curl' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castCurl',
3340
'r:dba' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',
3441
'r:dba persistent' => 'Symfony\Component\VarDumper\Caster\ResourceCaster::castDba',

0 commit comments

Comments
 (0)
0