SplCaster.php 7.9 KB

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