SplCaster.php 6.9 KB

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