Data.php 13 KB

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