Drupal investigation

SplCaster.php 6.2KB

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