SplCaster.php 6.4 KB

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