Data.php 14 KB

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