Data.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 implements \ArrayAccess, \Countable, \IteratorAggregate
  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 An array as returned by ClonerInterface::cloneVar()
  25. */
  26. public function __construct(array $data)
  27. {
  28. $this->data = $data;
  29. }
  30. /**
  31. * @return string The type of the value
  32. */
  33. public function getType()
  34. {
  35. $item = $this->data[$this->position][$this->key];
  36. if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {
  37. $item = $item->value;
  38. }
  39. if (!$item instanceof Stub) {
  40. return \gettype($item);
  41. }
  42. if (Stub::TYPE_STRING === $item->type) {
  43. return 'string';
  44. }
  45. if (Stub::TYPE_ARRAY === $item->type) {
  46. return 'array';
  47. }
  48. if (Stub::TYPE_OBJECT === $item->type) {
  49. return $item->class;
  50. }
  51. if (Stub::TYPE_RESOURCE === $item->type) {
  52. return $item->class.' resource';
  53. }
  54. }
  55. /**
  56. * @param bool $recursive Whether values should be resolved recursively or not
  57. *
  58. * @return string|int|float|bool|array|Data[]|null A native representation of the original value
  59. */
  60. public function getValue($recursive = false)
  61. {
  62. $item = $this->data[$this->position][$this->key];
  63. if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {
  64. $item = $item->value;
  65. }
  66. if (!($item = $this->getStub($item)) instanceof Stub) {
  67. return $item;
  68. }
  69. if (Stub::TYPE_STRING === $item->type) {
  70. return $item->value;
  71. }
  72. $children = $item->position ? $this->data[$item->position] : [];
  73. foreach ($children as $k => $v) {
  74. if ($recursive && !($v = $this->getStub($v)) instanceof Stub) {
  75. continue;
  76. }
  77. $children[$k] = clone $this;
  78. $children[$k]->key = $k;
  79. $children[$k]->position = $item->position;
  80. if ($recursive) {
  81. if (Stub::TYPE_REF === $v->type && ($v = $this->getStub($v->value)) instanceof Stub) {
  82. $recursive = (array) $recursive;
  83. if (isset($recursive[$v->position])) {
  84. continue;
  85. }
  86. $recursive[$v->position] = true;
  87. }
  88. $children[$k] = $children[$k]->getValue($recursive);
  89. }
  90. }
  91. return $children;
  92. }
  93. public function count()
  94. {
  95. return \count($this->getValue());
  96. }
  97. public function getIterator()
  98. {
  99. if (!\is_array($value = $this->getValue())) {
  100. throw new \LogicException(sprintf('%s object holds non-iterable type "%s".', self::class, \gettype($value)));
  101. }
  102. yield from $value;
  103. }
  104. public function __get($key)
  105. {
  106. if (null !== $data = $this->seek($key)) {
  107. $item = $this->getStub($data->data[$data->position][$data->key]);
  108. return $item instanceof Stub || [] === $item ? $data : $item;
  109. }
  110. }
  111. public function __isset($key)
  112. {
  113. return null !== $this->seek($key);
  114. }
  115. public function offsetExists($key)
  116. {
  117. return $this->__isset($key);
  118. }
  119. public function offsetGet($key)
  120. {
  121. return $this->__get($key);
  122. }
  123. public function offsetSet($key, $value)
  124. {
  125. throw new \BadMethodCallException(self::class.' objects are immutable.');
  126. }
  127. public function offsetUnset($key)
  128. {
  129. throw new \BadMethodCallException(self::class.' objects are immutable.');
  130. }
  131. public function __toString()
  132. {
  133. $value = $this->getValue();
  134. if (!\is_array($value)) {
  135. return (string) $value;
  136. }
  137. return sprintf('%s (count=%d)', $this->getType(), \count($value));
  138. }
  139. /**
  140. * Returns a depth limited clone of $this.
  141. *
  142. * @param int $maxDepth The max dumped depth level
  143. *
  144. * @return self A clone of $this
  145. */
  146. public function withMaxDepth($maxDepth)
  147. {
  148. $data = clone $this;
  149. $data->maxDepth = (int) $maxDepth;
  150. return $data;
  151. }
  152. /**
  153. * Limits the number of elements per depth level.
  154. *
  155. * @param int $maxItemsPerDepth The max number of items dumped per depth level
  156. *
  157. * @return self A clone of $this
  158. */
  159. public function withMaxItemsPerDepth($maxItemsPerDepth)
  160. {
  161. $data = clone $this;
  162. $data->maxItemsPerDepth = (int) $maxItemsPerDepth;
  163. return $data;
  164. }
  165. /**
  166. * Enables/disables objects' identifiers tracking.
  167. *
  168. * @param bool $useRefHandles False to hide global ref. handles
  169. *
  170. * @return self A clone of $this
  171. */
  172. public function withRefHandles($useRefHandles)
  173. {
  174. $data = clone $this;
  175. $data->useRefHandles = $useRefHandles ? -1 : 0;
  176. return $data;
  177. }
  178. /**
  179. * Seeks to a specific key in nested data structures.
  180. *
  181. * @param string|int $key The key to seek to
  182. *
  183. * @return self|null A clone of $this or null if the key is not set
  184. */
  185. public function seek($key)
  186. {
  187. $item = $this->data[$this->position][$this->key];
  188. if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {
  189. $item = $item->value;
  190. }
  191. if (!($item = $this->getStub($item)) instanceof Stub || !$item->position) {
  192. return;
  193. }
  194. $keys = [$key];
  195. switch ($item->type) {
  196. case Stub::TYPE_OBJECT:
  197. $keys[] = Caster::PREFIX_DYNAMIC.$key;
  198. $keys[] = Caster::PREFIX_PROTECTED.$key;
  199. $keys[] = Caster::PREFIX_VIRTUAL.$key;
  200. $keys[] = "\0$item->class\0$key";
  201. // no break
  202. case Stub::TYPE_ARRAY:
  203. case Stub::TYPE_RESOURCE:
  204. break;
  205. default:
  206. return;
  207. }
  208. $data = null;
  209. $children = $this->data[$item->position];
  210. foreach ($keys as $key) {
  211. if (isset($children[$key]) || array_key_exists($key, $children)) {
  212. $data = clone $this;
  213. $data->key = $key;
  214. $data->position = $item->position;
  215. break;
  216. }
  217. }
  218. return $data;
  219. }
  220. /**
  221. * Dumps data with a DumperInterface dumper.
  222. */
  223. public function dump(DumperInterface $dumper)
  224. {
  225. $refs = [0];
  226. $this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]);
  227. }
  228. /**
  229. * Depth-first dumping of items.
  230. *
  231. * @param DumperInterface $dumper The dumper being used for dumping
  232. * @param Cursor $cursor A cursor used for tracking dumper state position
  233. * @param array &$refs A map of all references discovered while dumping
  234. * @param mixed $item A Stub object or the original value being dumped
  235. */
  236. private function dumpItem($dumper, $cursor, &$refs, $item)
  237. {
  238. $cursor->refIndex = 0;
  239. $cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0;
  240. $cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0;
  241. $firstSeen = true;
  242. if (!$item instanceof Stub) {
  243. $cursor->attr = [];
  244. $type = \gettype($item);
  245. if ($item && 'array' === $type) {
  246. $item = $this->getStub($item);
  247. }
  248. } elseif (Stub::TYPE_REF === $item->type) {
  249. if ($item->handle) {
  250. if (!isset($refs[$r = $item->handle - (PHP_INT_MAX >> 1)])) {
  251. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  252. } else {
  253. $firstSeen = false;
  254. }
  255. $cursor->hardRefTo = $refs[$r];
  256. $cursor->hardRefHandle = $this->useRefHandles & $item->handle;
  257. $cursor->hardRefCount = $item->refCount;
  258. }
  259. $cursor->attr = $item->attr;
  260. $type = $item->class ?: \gettype($item->value);
  261. $item = $this->getStub($item->value);
  262. }
  263. if ($item instanceof Stub) {
  264. if ($item->refCount) {
  265. if (!isset($refs[$r = $item->handle])) {
  266. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  267. } else {
  268. $firstSeen = false;
  269. }
  270. $cursor->softRefTo = $refs[$r];
  271. }
  272. $cursor->softRefHandle = $this->useRefHandles & $item->handle;
  273. $cursor->softRefCount = $item->refCount;
  274. $cursor->attr = $item->attr;
  275. $cut = $item->cut;
  276. if ($item->position && $firstSeen) {
  277. $children = $this->data[$item->position];
  278. if ($cursor->stop) {
  279. if ($cut >= 0) {
  280. $cut += \count($children);
  281. }
  282. $children = [];
  283. }
  284. } else {
  285. $children = [];
  286. }
  287. switch ($item->type) {
  288. case Stub::TYPE_STRING:
  289. $dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut);
  290. break;
  291. case Stub::TYPE_ARRAY:
  292. $item = clone $item;
  293. $item->type = $item->class;
  294. $item->class = $item->value;
  295. // no break
  296. case Stub::TYPE_OBJECT:
  297. case Stub::TYPE_RESOURCE:
  298. $withChildren = $children && $cursor->depth !== $this->maxDepth && $this->maxItemsPerDepth;
  299. $dumper->enterHash($cursor, $item->type, $item->class, $withChildren);
  300. if ($withChildren) {
  301. if ($cursor->skipChildren) {
  302. $withChildren = false;
  303. $cut = -1;
  304. } else {
  305. $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type, null !== $item->class);
  306. }
  307. } elseif ($children && 0 <= $cut) {
  308. $cut += \count($children);
  309. }
  310. $cursor->skipChildren = false;
  311. $dumper->leaveHash($cursor, $item->type, $item->class, $withChildren, $cut);
  312. break;
  313. default:
  314. throw new \RuntimeException(sprintf('Unexpected Stub type: %s', $item->type));
  315. }
  316. } elseif ('array' === $type) {
  317. $dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false);
  318. $dumper->leaveHash($cursor, Cursor::HASH_INDEXED, 0, false, 0);
  319. } elseif ('string' === $type) {
  320. $dumper->dumpString($cursor, $item, false, 0);
  321. } else {
  322. $dumper->dumpScalar($cursor, $type, $item);
  323. }
  324. }
  325. /**
  326. * Dumps children of hash structures.
  327. *
  328. * @param DumperInterface $dumper
  329. * @param Cursor $parentCursor The cursor of the parent hash
  330. * @param array &$refs A map of all references discovered while dumping
  331. * @param array $children The children to dump
  332. * @param int $hashCut The number of items removed from the original hash
  333. * @param string $hashType A Cursor::HASH_* const
  334. * @param bool $dumpKeys Whether keys should be dumped or not
  335. *
  336. * @return int The final number of removed items
  337. */
  338. private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType, $dumpKeys)
  339. {
  340. $cursor = clone $parentCursor;
  341. ++$cursor->depth;
  342. $cursor->hashType = $hashType;
  343. $cursor->hashIndex = 0;
  344. $cursor->hashLength = \count($children);
  345. $cursor->hashCut = $hashCut;
  346. foreach ($children as $key => $child) {
  347. $cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
  348. $cursor->hashKey = $dumpKeys ? $key : null;
  349. $this->dumpItem($dumper, $cursor, $refs, $child);
  350. if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
  351. $parentCursor->stop = true;
  352. return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;
  353. }
  354. }
  355. return $hashCut;
  356. }
  357. private function getStub($item)
  358. {
  359. if (!$item || !\is_array($item)) {
  360. return $item;
  361. }
  362. $stub = new Stub();
  363. $stub->type = Stub::TYPE_ARRAY;
  364. foreach ($item as $stub->class => $stub->position) {
  365. }
  366. if (isset($item[0])) {
  367. $stub->cut = $item[0];
  368. }
  369. $stub->value = $stub->cut + ($stub->position ? \count($this->data[$stub->position]) : 0);
  370. return $stub;
  371. }
  372. }