SplCaster.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. class SplCaster
  18. {
  19. private static $splFileObjectFlags = array(
  20. \SplFileObject::DROP_NEW_LINE => 'DROP_NEW_LINE',
  21. \SplFileObject::READ_AHEAD => 'READ_AHEAD',
  22. \SplFileObject::SKIP_EMPTY => 'SKIP_EMPTY',
  23. \SplFileObject::READ_CSV => 'READ_CSV',
  24. );
  25. public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, $isNested)
  26. {
  27. return self::castSplArray($c, $a, $stub, $isNested);
  28. }
  29. public static function castArrayIterator(\ArrayIterator $c, array $a, Stub $stub, $isNested)
  30. {
  31. return self::castSplArray($c, $a, $stub, $isNested);
  32. }
  33. public static function castHeap(\Iterator $c, array $a, Stub $stub, $isNested)
  34. {
  35. $a += array(
  36. Caster::PREFIX_VIRTUAL.'heap' => iterator_to_array(clone $c),
  37. );
  38. return $a;
  39. }
  40. public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, Stub $stub, $isNested)
  41. {
  42. $prefix = Caster::PREFIX_VIRTUAL;
  43. $mode = $c->getIteratorMode();
  44. $c->setIteratorMode(\SplDoublyLinkedList::IT_MODE_KEEP | $mode & ~\SplDoublyLinkedList::IT_MODE_DELETE);
  45. $a += array(
  46. $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),
  47. $prefix.'dllist' => iterator_to_array($c),
  48. );
  49. $c->setIteratorMode($mode);
  50. return $a;
  51. }
  52. public static function castFileInfo(\SplFileInfo $c, array $a, Stub $stub, $isNested)
  53. {
  54. static $map = array(
  55. 'path' => 'getPath',
  56. 'filename' => 'getFilename',
  57. 'basename' => 'getBasename',
  58. 'pathname' => 'getPathname',
  59. 'extension' => 'getExtension',
  60. 'realPath' => 'getRealPath',
  61. 'aTime' => 'getATime',
  62. 'mTime' => 'getMTime',
  63. 'cTime' => 'getCTime',
  64. 'inode' => 'getInode',
  65. 'size' => 'getSize',
  66. 'perms' => 'getPerms',
  67. 'owner' => 'getOwner',
  68. 'group' => 'getGroup',
  69. 'type' => 'getType',
  70. 'writable' => 'isWritable',
  71. 'readable' => 'isReadable',
  72. 'executable' => 'isExecutable',
  73. 'file' => 'isFile',
  74. 'dir' => 'isDir',
  75. 'link' => 'isLink',
  76. 'linkTarget' => 'getLinkTarget',
  77. );
  78. $prefix = Caster::PREFIX_VIRTUAL;
  79. foreach ($map as $key => $accessor) {
  80. try {
  81. $a[$prefix.$key] = $c->$accessor();
  82. } catch (\Exception $e) {
  83. }
  84. }
  85. if (isset($a[$prefix.'realPath'])) {
  86. $a[$prefix.'realPath'] = new LinkStub($a[$prefix.'realPath']);
  87. }
  88. if (isset($a[$prefix.'perms'])) {
  89. $a[$prefix.'perms'] = new ConstStub(sprintf('0%o', $a[$prefix.'perms']), $a[$prefix.'perms']);
  90. }
  91. static $mapDate = array('aTime', 'mTime', 'cTime');
  92. foreach ($mapDate as $key) {
  93. if (isset($a[$prefix.$key])) {
  94. $a[$prefix.$key] = new ConstStub(date('Y-m-d H:i:s', $a[$prefix.$key]), $a[$prefix.$key]);
  95. }
  96. }
  97. return $a;
  98. }
  99. public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, $isNested)
  100. {
  101. static $map = array(
  102. 'csvControl' => 'getCsvControl',
  103. 'flags' => 'getFlags',
  104. 'maxLineLen' => 'getMaxLineLen',
  105. 'fstat' => 'fstat',
  106. 'eof' => 'eof',
  107. 'key' => 'key',
  108. );
  109. $prefix = Caster::PREFIX_VIRTUAL;
  110. foreach ($map as $key => $accessor) {
  111. try {
  112. $a[$prefix.$key] = $c->$accessor();
  113. } catch (\Exception $e) {
  114. }
  115. }
  116. if (isset($a[$prefix.'flags'])) {
  117. $flagsArray = array();
  118. foreach (self::$splFileObjectFlags as $value => $name) {
  119. if ($a[$prefix.'flags'] & $value) {
  120. $flagsArray[] = $name;
  121. }
  122. }
  123. $a[$prefix.'flags'] = new ConstStub(implode('|', $flagsArray), $a[$prefix.'flags']);
  124. }
  125. if (isset($a[$prefix.'fstat'])) {
  126. $a[$prefix.'fstat'] = new CutArrayStub($a[$prefix.'fstat'], array('dev', 'ino', 'nlink', 'rdev', 'blksize', 'blocks'));
  127. }
  128. return $a;
  129. }
  130. public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
  131. {
  132. $a += array(
  133. Caster::PREFIX_VIRTUAL.'storage' => $c->toArray(),
  134. );
  135. return $a;
  136. }
  137. public static function castObjectStorage(\SplObjectStorage $c, array $a, Stub $stub, $isNested)
  138. {
  139. $storage = array();
  140. unset($a[Caster::PREFIX_DYNAMIC."\0gcdata"]); // Don't hit https://bugs.php.net/65967
  141. $clone = clone $c;
  142. foreach ($clone as $obj) {
  143. $storage[] = array(
  144. 'object' => $obj,
  145. 'info' => $clone->getInfo(),
  146. );
  147. }
  148. $a += array(
  149. Caster::PREFIX_VIRTUAL.'storage' => $storage,
  150. );
  151. return $a;
  152. }
  153. public static function castOuterIterator(\OuterIterator $c, array $a, Stub $stub, $isNested)
  154. {
  155. $a[Caster::PREFIX_VIRTUAL.'innerIterator'] = $c->getInnerIterator();
  156. return $a;
  157. }
  158. private static function castSplArray($c, array $a, Stub $stub, $isNested)
  159. {
  160. $prefix = Caster::PREFIX_VIRTUAL;
  161. $class = $stub->class;
  162. $flags = $c->getFlags();
  163. if (!($flags & \ArrayObject::STD_PROP_LIST)) {
  164. $c->setFlags(\ArrayObject::STD_PROP_LIST);
  165. $a = Caster::castObject($c, $class);
  166. $c->setFlags($flags);
  167. }
  168. $a += array(
  169. $prefix.'flag::STD_PROP_LIST' => (bool) ($flags & \ArrayObject::STD_PROP_LIST),
  170. $prefix.'flag::ARRAY_AS_PROPS' => (bool) ($flags & \ArrayObject::ARRAY_AS_PROPS),
  171. );
  172. if ($c instanceof \ArrayObject) {
  173. $a[$prefix.'iteratorClass'] = new ClassStub($c->getIteratorClass());
  174. }
  175. $a[$prefix.'storage'] = $c->getArrayCopy();
  176. return $a;
  177. }
  178. }