Без опису

SplCaster.php 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 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, $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. 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 ($a[$prefix.'realPath'] ?? false) {
  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, $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, $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, $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, $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. }