Data.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\Cloner;
  11. use Symfony\Component\VarDumper\Caster\Caster;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. */
  15. class Data
  16. {
  17. private $data;
  18. private $position = 0;
  19. private $key = 0;
  20. private $maxDepth = 20;
  21. private $maxItemsPerDepth = -1;
  22. private $useRefHandles = -1;
  23. /**
  24. * @param array $data A array as returned by ClonerInterface::cloneVar()
  25. */
  26. public function __construct(array $data)
  27. {
  28. $this->data = $data;
  29. }
  30. /**
  31. * @return array The raw data structure
  32. */
  33. public function getRawData()
  34. {
  35. return $this->data;
  36. }
  37. /**
  38. * Returns a depth limited clone of $this.
  39. *
  40. * @param int $maxDepth The max dumped depth level
  41. *
  42. * @return self A clone of $this
  43. */
  44. public function withMaxDepth($maxDepth)
  45. {
  46. $data = clone $this;
  47. $data->maxDepth = (int) $maxDepth;
  48. return $data;
  49. }
  50. /**
  51. * Limits the number of elements per depth level.
  52. *
  53. * @param int $maxItemsPerDepth The max number of items dumped per depth level
  54. *
  55. * @return self A clone of $this
  56. */
  57. public function withMaxItemsPerDepth($maxItemsPerDepth)
  58. {
  59. $data = clone $this;
  60. $data->maxItemsPerDepth = (int) $maxItemsPerDepth;
  61. return $data;
  62. }
  63. /**
  64. * Enables/disables objects' identifiers tracking.
  65. *
  66. * @param bool $useRefHandles False to hide global ref. handles
  67. *
  68. * @return self A clone of $this
  69. */
  70. public function withRefHandles($useRefHandles)
  71. {
  72. $data = clone $this;
  73. $data->useRefHandles = $useRefHandles ? -1 : 0;
  74. return $data;
  75. }
  76. /**
  77. * Seeks to a specific key in nested data structures.
  78. *
  79. * @param string|int $key The key to seek to
  80. *
  81. * @return self|null A clone of $this of null if the key is not set
  82. */
  83. public function seek($key)
  84. {
  85. $item = $this->data[$this->position][$this->key];
  86. if (!$item instanceof Stub || !$item->position) {
  87. return;
  88. }
  89. $keys = array($key);
  90. switch ($item->type) {
  91. case Stub::TYPE_OBJECT:
  92. $keys[] = Caster::PREFIX_DYNAMIC.$key;
  93. $keys[] = Caster::PREFIX_PROTECTED.$key;
  94. $keys[] = Caster::PREFIX_VIRTUAL.$key;
  95. $keys[] = "\0$item->class\0$key";
  96. case Stub::TYPE_ARRAY:
  97. case Stub::TYPE_RESOURCE:
  98. break;
  99. default:
  100. return;
  101. }
  102. $data = null;
  103. $children = $this->data[$item->position];
  104. foreach ($keys as $key) {
  105. if (isset($children[$key]) || array_key_exists($key, $children)) {
  106. $data = clone $this;
  107. $data->key = $key;
  108. $data->position = $item->position;
  109. break;
  110. }
  111. }
  112. return $data;
  113. }
  114. /**
  115. * Dumps data with a DumperInterface dumper.
  116. */
  117. public function dump(DumperInterface $dumper)
  118. {
  119. $refs = array(0);
  120. $this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]);
  121. }
  122. /**
  123. * Depth-first dumping of items.
  124. *
  125. * @param DumperInterface $dumper The dumper being used for dumping
  126. * @param Cursor $cursor A cursor used for tracking dumper state position
  127. * @param array &$refs A map of all references discovered while dumping
  128. * @param mixed $item A Stub object or the original value being dumped
  129. */
  130. private function dumpItem($dumper, $cursor, &$refs, $item)
  131. {
  132. $cursor->refIndex = 0;
  133. $cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0;
  134. $cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0;
  135. $firstSeen = true;
  136. if (!$item instanceof Stub) {
  137. $cursor->attr = array();
  138. $type = gettype($item);
  139. } elseif (Stub::TYPE_REF === $item->type) {
  140. if ($item->handle) {
  141. if (!isset($refs[$r = $item->handle - (PHP_INT_MAX >> 1)])) {
  142. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  143. } else {
  144. $firstSeen = false;
  145. }
  146. $cursor->hardRefTo = $refs[$r];
  147. $cursor->hardRefHandle = $this->useRefHandles & $item->handle;
  148. $cursor->hardRefCount = $item->refCount;
  149. }
  150. $cursor->attr = $item->attr;
  151. $type = $item->class ?: gettype($item->value);
  152. $item = $item->value;
  153. }
  154. if ($item instanceof Stub) {
  155. if ($item->refCount) {
  156. if (!isset($refs[$r = $item->handle])) {
  157. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  158. } else {
  159. $firstSeen = false;
  160. }
  161. $cursor->softRefTo = $refs[$r];
  162. }
  163. $cursor->softRefHandle = $this->useRefHandles & $item->handle;
  164. $cursor->softRefCount = $item->refCount;
  165. $cursor->attr = $item->attr;
  166. $cut = $item->cut;
  167. if ($item->position && $firstSeen) {
  168. $children = $this->data[$item->position];
  169. if ($cursor->stop) {
  170. if ($cut >= 0) {
  171. $cut += count($children);
  172. }
  173. $children = array();
  174. }
  175. } else {
  176. $children = array();
  177. }
  178. switch ($item->type) {
  179. case Stub::TYPE_STRING:
  180. $dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut);
  181. break;
  182. case Stub::TYPE_ARRAY:
  183. $item = clone $item;
  184. $item->type = $item->class;
  185. $item->class = $item->value;
  186. // No break;
  187. case Stub::TYPE_OBJECT:
  188. case Stub::TYPE_RESOURCE:
  189. $withChildren = $children && $cursor->depth !== $this->maxDepth && $this->maxItemsPerDepth;
  190. $dumper->enterHash($cursor, $item->type, $item->class, $withChildren);
  191. if ($withChildren) {
  192. $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type, null !== $item->class);
  193. } elseif ($children && 0 <= $cut) {
  194. $cut += count($children);
  195. }
  196. $dumper->leaveHash($cursor, $item->type, $item->class, $withChildren, $cut);
  197. break;
  198. default:
  199. throw new \RuntimeException(sprintf('Unexpected Stub type: %s', $item->type));
  200. }
  201. } elseif ('array' === $type) {
  202. $dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false);
  203. $dumper->leaveHash($cursor, Cursor::HASH_INDEXED, 0, false, 0);
  204. } elseif ('string' === $type) {
  205. $dumper->dumpString($cursor, $item, false, 0);
  206. } else {
  207. $dumper->dumpScalar($cursor, $type, $item);
  208. }
  209. }
  210. /**
  211. * Dumps children of hash structures.
  212. *
  213. * @param DumperInterface $dumper
  214. * @param Cursor $parentCursor The cursor of the parent hash
  215. * @param array &$refs A map of all references discovered while dumping
  216. * @param array $children The children to dump
  217. * @param int $hashCut The number of items removed from the original hash
  218. * @param string $hashType A Cursor::HASH_* const
  219. * @param bool $dumpKeys Whether keys should be dumped or not
  220. *
  221. * @return int The final number of removed items
  222. */
  223. private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType, $dumpKeys)
  224. {
  225. $cursor = clone $parentCursor;
  226. ++$cursor->depth;
  227. $cursor->hashType = $hashType;
  228. $cursor->hashIndex = 0;
  229. $cursor->hashLength = count($children);
  230. $cursor->hashCut = $hashCut;
  231. foreach ($children as $key => $child) {
  232. $cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
  233. $cursor->hashKey = $dumpKeys ? $key : null;
  234. $this->dumpItem($dumper, $cursor, $refs, $child);
  235. if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
  236. $parentCursor->stop = true;
  237. return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;
  238. }
  239. }
  240. return $hashCut;
  241. }
  242. }