4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Caster;
14 use Symfony\Component\VarDumper\Cloner\Stub;
17 * Casts SPL related classes to array representation.
19 * @author Nicolas Grekas <p@tchwork.com>
23 private static $splFileObjectFlags = array(
24 \SplFileObject::DROP_NEW_LINE => 'DROP_NEW_LINE',
25 \SplFileObject::READ_AHEAD => 'READ_AHEAD',
26 \SplFileObject::SKIP_EMPTY => 'SKIP_EMPTY',
27 \SplFileObject::READ_CSV => 'READ_CSV',
30 public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, $isNested)
32 $prefix = Caster::PREFIX_VIRTUAL;
33 $class = $stub->class;
34 $flags = $c->getFlags();
37 $prefix.'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
38 $prefix.'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
39 $prefix.'iteratorClass' => new ClassStub($c->getIteratorClass()),
40 $prefix.'storage' => $c->getArrayCopy(),
43 if ('ArrayObject' === $class) {
46 if (!($flags & \ArrayObject::STD_PROP_LIST)) {
47 $c->setFlags(\ArrayObject::STD_PROP_LIST);
48 $a = Caster::castObject($c, $class);
58 public static function castHeap(\Iterator $c, array $a, Stub $stub, $isNested)
61 Caster::PREFIX_VIRTUAL.'heap' => iterator_to_array(clone $c),
67 public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, Stub $stub, $isNested)
69 $prefix = Caster::PREFIX_VIRTUAL;
70 $mode = $c->getIteratorMode();
71 $c->setIteratorMode(\SplDoublyLinkedList::IT_MODE_KEEP | $mode & ~\SplDoublyLinkedList::IT_MODE_DELETE);
74 $prefix.'mode' => new ConstStub((($mode & \SplDoublyLinkedList::IT_MODE_LIFO) ? 'IT_MODE_LIFO' : 'IT_MODE_FIFO').' | '.(($mode & \SplDoublyLinkedList::IT_MODE_DELETE) ? 'IT_MODE_DELETE' : 'IT_MODE_KEEP'), $mode),
75 $prefix.'dllist' => iterator_to_array($c),
77 $c->setIteratorMode($mode);
82 public static function castFileInfo(\SplFileInfo $c, array $a, Stub $stub, $isNested)
86 'filename' => 'getFilename',
87 'basename' => 'getBasename',
88 'pathname' => 'getPathname',
89 'extension' => 'getExtension',
90 'realPath' => 'getRealPath',
91 'aTime' => 'getATime',
92 'mTime' => 'getMTime',
93 'cTime' => 'getCTime',
94 'inode' => 'getInode',
96 'perms' => 'getPerms',
97 'owner' => 'getOwner',
98 'group' => 'getGroup',
100 'writable' => 'isWritable',
101 'readable' => 'isReadable',
102 'executable' => 'isExecutable',
106 'linkTarget' => 'getLinkTarget',
109 $prefix = Caster::PREFIX_VIRTUAL;
111 foreach ($map as $key => $accessor) {
113 $a[$prefix.$key] = $c->$accessor();
114 } catch (\Exception $e) {
118 if (isset($a[$prefix.'realPath'])) {
119 $a[$prefix.'realPath'] = new LinkStub($a[$prefix.'realPath']);
122 if (isset($a[$prefix.'perms'])) {
123 $a[$prefix.'perms'] = new ConstStub(sprintf('0%o', $a[$prefix.'perms']), $a[$prefix.'perms']);
126 static $mapDate = array('aTime', 'mTime', 'cTime');
127 foreach ($mapDate as $key) {
128 if (isset($a[$prefix.$key])) {
129 $a[$prefix.$key] = new ConstStub(date('Y-m-d H:i:s', $a[$prefix.$key]), $a[$prefix.$key]);
136 public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, $isNested)
139 'csvControl' => 'getCsvControl',
140 'flags' => 'getFlags',
141 'maxLineLen' => 'getMaxLineLen',
147 $prefix = Caster::PREFIX_VIRTUAL;
149 foreach ($map as $key => $accessor) {
151 $a[$prefix.$key] = $c->$accessor();
152 } catch (\Exception $e) {
156 if (isset($a[$prefix.'flags'])) {
157 $flagsArray = array();
158 foreach (self::$splFileObjectFlags as $value => $name) {
159 if ($a[$prefix.'flags'] & $value) {
160 $flagsArray[] = $name;
163 $a[$prefix.'flags'] = new ConstStub(implode('|', $flagsArray), $a[$prefix.'flags']);
166 if (isset($a[$prefix.'fstat'])) {
167 $a[$prefix.'fstat'] = new CutArrayStub($a[$prefix.'fstat'], array('dev', 'ino', 'nlink', 'rdev', 'blksize', 'blocks'));
173 public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
176 Caster::PREFIX_VIRTUAL.'storage' => $c->toArray(),
182 public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, $isNested)
185 unset($a[Caster::PREFIX_DYNAMIC."\0gcdata"]); // Don't hit https://bugs.php.net/65967
188 foreach ($clone as $obj) {
189 $storage[spl_object_hash($obj)] = array(
191 'info' => $clone->getInfo(),
196 Caster::PREFIX_VIRTUAL.'storage' => $storage,
202 public static function castOuterIterator(\OuterIterator $c, array $a, Stub $stub, $isNested)
204 $a[Caster::PREFIX_VIRTUAL.'innerIterator'] = $c->getInnerIterator();