SplCaster.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. * @final since Symfony 4.4
  18. */
  19. class SplCaster
  20. {
  21. private static $splFileObjectFlags = [
  22. \SplFileObject::DROP_NEW_LINE => 'DROP_NEW_LINE',
  23. \SplFileObject::READ_AHEAD => 'READ_AHEAD',
  24. \SplFileObject::SKIP_EMPTY => 'SKIP_EMPTY',
  25. \SplFileObject::READ_CSV => 'READ_CSV',
  26. ];
  27. public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, $isNested)
  28. {
  29. return self::castSplArray($c, $a, $stub, $isNested);
  30. }
  31. public static function castArrayIterator(\ArrayIterator $c, array $a, Stub $stub, $isNested)
  32. {
  33. return self::castSplArray($c, $a, $stub, $isNested);
  34. }
  35. public static function castHeap(\Iterator $c, array $a, Stub $stub, $isNested)
  36. {
  37. $a += [
  38. Caster::PREFIX_VIRTUAL.'heap' => iterator_to_array(clone $c),
  39. ];
  40. return $a;
  41. }
  42. public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, Stub $stub, $isNested)
  43. {
  44. $prefix = Caster::PREFIX_VIRTUAL;
  45. $mode = $c->getIteratorMode();
  46. $c->setIteratorMode(\SplDoublyLinkedList::IT_MODE_KEEP | $mode & ~\SplDoublyLinkedList::IT_MODE_DELETE);
  47. $a += [
  48. $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),
  49. $prefix.'dllist' => iterator_to_array($c),
  50. ];
  51. $c->setIteratorMode($mode);
  52. return $a;
  53. }
  54. public static function castFileInfo(\SplFileInfo $c, array $a, Stub $stub, $isNested)
  55. {
  56. static $map = [
  57. 'path' => 'getPath',
  58. 'filename' => 'getFilename',
  59. 'basename' => 'getBasename',
  60. 'pathname' => 'getPathname',
  61. 'extension' => 'getExtension',
  62. 'realPath' => 'getRealPath',
  63. 'aTime' => 'getATime',
  64. 'mTime' => 'getMTime',
  65. 'cTime' => 'getCTime',
  66. 'inode' => 'getInode',
  67. 'size' => 'getSize',
  68. 'perms' => 'getPerms',
  69. 'owner' => 'getOwner',
  70. 'group' => 'getGroup',
  71. 'type' => 'getType',
  72. 'writable' => 'isWritable',
  73. 'readable' => 'isReadable',
  74. 'executable' => 'isExecutable',
  75. 'file' => 'isFile',
  76. 'dir' => 'isDir',
  77. 'link' => 'isLink',
  78. 'linkTarget' => 'getLinkTarget',
  79. ];
  80. $prefix = Caster::PREFIX_VIRTUAL;
  81. if (false === $c->getPathname()) {
  82. $a[$prefix.'⚠'] = 'The parent constructor was not called: the object is in an invalid state';
  83. return $a;
  84. }
  85. foreach ($map as $key => $accessor) {
  86. try {
  87. $a[$prefix.$key] = $c->$accessor();
  88. } catch (\Exception $e) {
  89. }
  90. }
  91. if (isset($a[$prefix.'realPath'])) {
  92. $a[$prefix.'realPath'] = new LinkStub($a[$prefix.'realPath']);
  93. }
  94. if (isset($a[$prefix.'perms'])) {
  95. $a[$prefix.'perms'] = new ConstStub(sprintf('0%o', $a[$prefix.'perms']), $a[$prefix.'perms']);
  96. }
  97. static $mapDate = ['aTime', 'mTime', 'cTime'];
  98. foreach ($mapDate as $key) {
  99. if (isset($a[$prefix.$key])) {
  100. $a[$prefix.$key] = new ConstStub(date('Y-m-d H:i:s', $a[$prefix.$key]), $a[$prefix.$key]);
  101. }
  102. }
  103. return $a;
  104. }
  105. public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, $isNested)
  106. {
  107. static $map = [
  108. 'csvControl' => 'getCsvControl',
  109. 'flags' => 'getFlags',
  110. 'maxLineLen' => 'getMaxLineLen',
  111. 'fstat' => 'fstat',
  112. 'eof' => 'eof',
  113. 'key' => 'key',
  114. ];
  115. $prefix = Caster::PREFIX_VIRTUAL;
  116. foreach ($map as $key => $accessor) {
  117. try {
  118. $a[$prefix.$key] = $c->$accessor();
  119. } catch (\Exception $e) {
  120. }
  121. }
  122. if (isset($a[$prefix.'flags'])) {
  123. $flagsArray = [];
  124. foreach (self::$splFileObjectFlags as $value => $name) {
  125. if ($a[$prefix.'flags'] & $value) {
  126. $flagsArray[] = $name;
  127. }
  128. }
  129. $a[$prefix.'flags'] = new ConstStub(implode('|', $flagsArray), $a[$prefix.'flags']);
  130. }
  131. if (isset($a[$prefix.'fstat'])) {
  132. $a[$prefix.'fstat'] = new CutArrayStub($a[$prefix.'fstat'], ['dev', 'ino', 'nlink', 'rdev', 'blksize', 'blocks']);
  133. }
  134. return $a;
  135. }
  136. public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
  137. {
  138. $a += [
  139. Caster::PREFIX_VIRTUAL.'storage' => $c->toArray(),
  140. ];
  141. return $a;
  142. }
  143. public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, $isNested)
  144. {
  145. $storage = [];
  146. unset($a[Caster::PREFIX_DYNAMIC."\0gcdata"]); // Don't hit https://bugs.php.net/65967
  147. $clone = clone $c;
  148. foreach ($clone as $obj) {
  149. $storage[] = [
  150. 'object' => $obj,
  151. 'info' => $clone->getInfo(),
  152. ];
  153. }
  154. $a += [
  155. Caster::PREFIX_VIRTUAL.'storage' => $storage,
  156. ];
  157. return $a;
  158. }
  159. public static function castOuterIterator(\OuterIterator $c, array $a, Stub $stub, $isNested)
  160. {
  161. $a[Caster::PREFIX_VIRTUAL.'innerIterator'] = $c->getInnerIterator();
  162. return $a;
  163. }
  164. public static function castWeakReference(\WeakReference $c, array $a, Stub $stub, $isNested)
  165. {
  166. $a[Caster::PREFIX_VIRTUAL.'object'] = $c->get();
  167. return $a;
  168. }
  169. private static function castSplArray($c, array $a, Stub $stub, bool $isNested): array
  170. {
  171. $prefix = Caster::PREFIX_VIRTUAL;
  172. $class = $stub->class;
  173. $flags = $c->getFlags();
  174. if (!($flags & \ArrayObject::STD_PROP_LIST)) {
  175. $c->setFlags(\ArrayObject::STD_PROP_LIST);
  176. $a = Caster::castObject($c, $class);
  177. $c->setFlags($flags);
  178. }
  179. $a += [
  180. $prefix.'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
  181. $prefix.'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
  182. ];
  183. if ($c instanceof \ArrayObject) {
  184. $a[$prefix.'iteratorClass'] = new ClassStub($c->getIteratorClass());
  185. }
  186. $a[$prefix.'storage'] = $c->getArrayCopy();
  187. return $a;
  188. }
  189. }