SplCaster.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Caster;
  11. use Symfony\Component\VarDumper\Cloner\Stub;
  12. /**
  13. * Casts SPL related classes to array representation.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. */
  17. class SplCaster
  18. {
  19. private static $splFileObjectFlags = array(
  20. \SplFileObject::DROP_NEW_LINE => 'DROP_NEW_LINE',
  21. \SplFileObject::READ_AHEAD => 'READ_AHEAD',
  22. \SplFileObject::SKIP_EMPTY => 'SKIP_EMPTY',
  23. \SplFileObject::READ_CSV => 'READ_CSV',
  24. );
  25. public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, $isNested)
  26. {
  27. $prefix = Caster::PREFIX_VIRTUAL;
  28. $class = $stub->class;
  29. $flags = $c->getFlags();
  30. $b = array(
  31. $prefix.'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
  32. $prefix.'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
  33. $prefix.'iteratorClass' => new ClassStub($c->getIteratorClass()),
  34. $prefix.'storage' => $c->getArrayCopy(),
  35. );
  36. if ($class === 'ArrayObject') {
  37. $a = $b;
  38. } else {
  39. if (!($flags & \ArrayObject::STD_PROP_LIST)) {
  40. $c->setFlags(\ArrayObject::STD_PROP_LIST);
  41. $a = Caster::castObject($c, $class);
  42. $c->setFlags($flags);
  43. }
  44. $a += $b;
  45. }
  46. return $a;
  47. }
  48. public static function castHeap(\Iterator $c, array $a, Stub $stub, $isNested)
  49. {
  50. $a += array(
  51. Caster::PREFIX_VIRTUAL.'heap' => iterator_to_array(clone $c),
  52. );
  53. return $a;
  54. }
  55. public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, Stub $stub, $isNested)
  56. {
  57. $prefix = Caster::PREFIX_VIRTUAL;
  58. $mode = $c->getIteratorMode();
  59. $c->setIteratorMode(\SplDoublyLinkedList::IT_MODE_KEEP | $mode & ~\SplDoublyLinkedList::IT_MODE_DELETE);
  60. $a += array(
  61. $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),
  62. $prefix.'dllist' => iterator_to_array($c),
  63. );
  64. $c->setIteratorMode($mode);
  65. return $a;
  66. }
  67. public static function castFileInfo(\SplFileInfo $c, array $a, Stub $stub, $isNested)
  68. {
  69. static $map = array(
  70. 'path' => 'getPath',
  71. 'filename' => 'getFilename',
  72. 'basename' => 'getBasename',
  73. 'pathname' => 'getPathname',
  74. 'extension' => 'getExtension',
  75. 'realPath' => 'getRealPath',
  76. 'aTime' => 'getATime',
  77. 'mTime' => 'getMTime',
  78. 'cTime' => 'getCTime',
  79. 'inode' => 'getInode',
  80. 'size' => 'getSize',
  81. 'perms' => 'getPerms',
  82. 'owner' => 'getOwner',
  83. 'group' => 'getGroup',
  84. 'type' => 'getType',
  85. 'writable' => 'isWritable',
  86. 'readable' => 'isReadable',
  87. 'executable' => 'isExecutable',
  88. 'file' => 'isFile',
  89. 'dir' => 'isDir',
  90. 'link' => 'isLink',
  91. 'linkTarget' => 'getLinkTarget',
  92. );
  93. $prefix = Caster::PREFIX_VIRTUAL;
  94. foreach ($map as $key => $accessor) {
  95. try {
  96. $a[$prefix.$key] = $c->$accessor();
  97. } catch (\Exception $e) {
  98. }
  99. }
  100. if (isset($a[$prefix.'realPath'])) {
  101. $a[$prefix.'realPath'] = new LinkStub($a[$prefix.'realPath']);
  102. }
  103. if (isset($a[$prefix.'perms'])) {
  104. $a[$prefix.'perms'] = new ConstStub(sprintf('0%o', $a[$prefix.'perms']), $a[$prefix.'perms']);
  105. }
  106. static $mapDate = array('aTime', 'mTime', 'cTime');
  107. foreach ($mapDate as $key) {
  108. if (isset($a[$prefix.$key])) {
  109. $a[$prefix.$key] = new ConstStub(date('Y-m-d H:i:s', $a[$prefix.$key]), $a[$prefix.$key]);
  110. }
  111. }
  112. return $a;
  113. }
  114. public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, $isNested)
  115. {
  116. static $map = array(
  117. 'csvControl' => 'getCsvControl',
  118. 'flags' => 'getFlags',
  119. 'maxLineLen' => 'getMaxLineLen',
  120. 'fstat' => 'fstat',
  121. 'eof' => 'eof',
  122. 'key' => 'key',
  123. );
  124. $prefix = Caster::PREFIX_VIRTUAL;
  125. foreach ($map as $key => $accessor) {
  126. try {
  127. $a[$prefix.$key] = $c->$accessor();
  128. } catch (\Exception $e) {
  129. }
  130. }
  131. if (isset($a[$prefix.'flags'])) {
  132. $flagsArray = array();
  133. foreach (self::$splFileObjectFlags as $value => $name) {
  134. if ($a[$prefix.'flags'] & $value) {
  135. $flagsArray[] = $name;
  136. }
  137. }
  138. $a[$prefix.'flags'] = new ConstStub(implode('|', $flagsArray), $a[$prefix.'flags']);
  139. }
  140. if (isset($a[$prefix.'fstat'])) {
  141. $a[$prefix.'fstat'] = new CutArrayStub($a[$prefix.'fstat'], array('dev', 'ino', 'nlink', 'rdev', 'blksize', 'blocks'));
  142. }
  143. return $a;
  144. }
  145. public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
  146. {
  147. $a += array(
  148. Caster::PREFIX_VIRTUAL.'storage' => $c->toArray(),
  149. );
  150. return $a;
  151. }
  152. public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, $isNested)
  153. {
  154. $storage = array();
  155. unset($a[Caster::PREFIX_DYNAMIC."\0gcdata"]); // Don't hit https://bugs.php.net/65967
  156. foreach ($c as $obj) {
  157. $storage[spl_object_hash($obj)] = array(
  158. 'object' => $obj,
  159. 'info' => $c->getInfo(),
  160. );
  161. }
  162. $a += array(
  163. Caster::PREFIX_VIRTUAL.'storage' => $storage,
  164. );
  165. return $a;
  166. }
  167. public static function castOuterIterator(\OuterIterator $c, array $a, Stub $stub, $isNested)
  168. {
  169. $a[Caster::PREFIX_VIRTUAL.'innerIterator'] = $c->getInnerIterator();
  170. return $a;
  171. }
  172. }