You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
7.6 KiB

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