SplCaster.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. class SplCaster
  20. {
  21. private const SPL_FILE_OBJECT_FLAGS = [
  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, bool $isNested)
  28. {
  29. return self::castSplArray($c, $a, $stub, $isNested);
  30. }
  31. public static function castArrayIterator(\ArrayIterator $c, array $a, Stub $stub, bool $isNested)
  32. {
  33. return self::castSplArray($c, $a, $stub, $isNested);
  34. }
  35. public static function castHeap(\Iterator $c, array $a, Stub $stub, bool $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, bool $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, bool $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. unset($a["\0SplFileInfo\0fileName"]);
  82. unset($a["\0SplFileInfo\0pathName"]);
  83. try {
  84. $c->isReadable();
  85. } catch (\RuntimeException $e) {
  86. if ('Object not initialized' !== $e->getMessage()) {
  87. throw $e;
  88. }
  89. $a[$prefix.'⚠'] = 'The parent constructor was not called: the object is in an invalid state';
  90. return $a;
  91. } catch (\Error $e) {
  92. if ('Object not initialized' !== $e->getMessage()) {
  93. throw $e;
  94. }
  95. $a[$prefix.'⚠'] = 'The parent constructor was not called: the object is in an invalid state';
  96. return $a;
  97. }
  98. foreach ($map as $key => $accessor) {
  99. try {
  100. $a[$prefix.$key] = $c->$accessor();
  101. } catch (\Exception $e) {
  102. }
  103. }
  104. if ($a[$prefix.'realPath'] ?? false) {
  105. $a[$prefix.'realPath'] = new LinkStub($a[$prefix.'realPath']);
  106. }
  107. if (isset($a[$prefix.'perms'])) {
  108. $a[$prefix.'perms'] = new ConstStub(sprintf('0%o', $a[$prefix.'perms']), $a[$prefix.'perms']);
  109. }
  110. static $mapDate = ['aTime', 'mTime', 'cTime'];
  111. foreach ($mapDate as $key) {
  112. if (isset($a[$prefix.$key])) {
  113. $a[$prefix.$key] = new ConstStub(date('Y-m-d H:i:s', $a[$prefix.$key]), $a[$prefix.$key]);
  114. }
  115. }
  116. return $a;
  117. }
  118. public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, bool $isNested)
  119. {
  120. static $map = [
  121. 'csvControl' => 'getCsvControl',
  122. 'flags' => 'getFlags',
  123. 'maxLineLen' => 'getMaxLineLen',
  124. 'fstat' => 'fstat',
  125. 'eof' => 'eof',
  126. 'key' => 'key',
  127. ];
  128. $prefix = Caster::PREFIX_VIRTUAL;
  129. foreach ($map as $key => $accessor) {
  130. try {
  131. $a[$prefix.$key] = $c->$accessor();
  132. } catch (\Exception $e) {
  133. }
  134. }
  135. if (isset($a[$prefix.'flags'])) {
  136. $flagsArray = [];
  137. foreach (self::SPL_FILE_OBJECT_FLAGS as $value => $name) {
  138. if ($a[$prefix.'flags'] & $value) {
  139. $flagsArray[] = $name;
  140. }
  141. }
  142. $a[$prefix.'flags'] = new ConstStub(implode('|', $flagsArray), $a[$prefix.'flags']);
  143. }
  144. if (isset($a[$prefix.'fstat'])) {
  145. $a[$prefix.'fstat'] = new CutArrayStub($a[$prefix.'fstat'], ['dev', 'ino', 'nlink', 'rdev', 'blksize', 'blocks']);
  146. }
  147. return $a;
  148. }
  149. public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, bool $isNested)
  150. {
  151. $storage = [];
  152. unset($a[Caster::PREFIX_DYNAMIC."\0gcdata"]); // Don't hit https://bugs.php.net/65967
  153. unset($a["\0SplObjectStorage\0storage"]);
  154. $clone = clone $c;
  155. foreach ($clone as $obj) {
  156. $storage[] = [
  157. 'object' => $obj,
  158. 'info' => $clone->getInfo(),
  159. ];
  160. }
  161. $a += [
  162. Caster::PREFIX_VIRTUAL.'storage' => $storage,
  163. ];
  164. return $a;
  165. }
  166. public static function castOuterIterator(\OuterIterator $c, array $a, Stub $stub, bool $isNested)
  167. {
  168. $a[Caster::PREFIX_VIRTUAL.'innerIterator'] = $c->getInnerIterator();
  169. return $a;
  170. }
  171. public static function castWeakReference(\WeakReference $c, array $a, Stub $stub, bool $isNested)
  172. {
  173. $a[Caster::PREFIX_VIRTUAL.'object'] = $c->get();
  174. return $a;
  175. }
  176. private static function castSplArray(\ArrayObject|\ArrayIterator $c, array $a, Stub $stub, bool $isNested): array
  177. {
  178. $prefix = Caster::PREFIX_VIRTUAL;
  179. $flags = $c->getFlags();
  180. if (!($flags & \ArrayObject::STD_PROP_LIST)) {
  181. $c->setFlags(\ArrayObject::STD_PROP_LIST);
  182. $a = Caster::castObject($c, \get_class($c), method_exists($c, '__debugInfo'), $stub->class);
  183. $c->setFlags($flags);
  184. }
  185. $a += [
  186. $prefix.'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
  187. $prefix.'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
  188. ];
  189. if ($c instanceof \ArrayObject) {
  190. $a[$prefix.'iteratorClass'] = new ClassStub($c->getIteratorClass());
  191. }
  192. return $a;
  193. }
  194. }