Data.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 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. public function __get(string $key)
  107. {
  108. if (null !== $data = $this->seek($key)) {
  109. $item = $this->getStub($data->data[$data->position][$data->key]);
  110. return $item instanceof Stub || [] === $item ? $data : $item;
  111. }
  112. return null;
  113. }
  114. public function __isset(string $key): bool
  115. {
  116. return null !== $this->seek($key);
  117. }
  118. public function offsetExists(mixed $key): bool
  119. {
  120. return $this->__isset($key);
  121. }
  122. public function offsetGet(mixed $key): mixed
  123. {
  124. return $this->__get($key);
  125. }
  126. public function offsetSet(mixed $key, mixed $value): void
  127. {
  128. throw new \BadMethodCallException(self::class.' objects are immutable.');
  129. }
  130. public function offsetUnset(mixed $key): void
  131. {
  132. throw new \BadMethodCallException(self::class.' objects are immutable.');
  133. }
  134. public function __toString(): string
  135. {
  136. $value = $this->getValue();
  137. if (!\is_array($value)) {
  138. return (string) $value;
  139. }
  140. return sprintf('%s (count=%d)', $this->getType(), \count($value));
  141. }
  142. /**
  143. * Returns a depth limited clone of $this.
  144. */
  145. public function withMaxDepth(int $maxDepth): static
  146. {
  147. $data = clone $this;
  148. $data->maxDepth = $maxDepth;
  149. return $data;
  150. }
  151. /**
  152. * Limits the number of elements per depth level.
  153. */
  154. public function withMaxItemsPerDepth(int $maxItemsPerDepth): static
  155. {
  156. $data = clone $this;
  157. $data->maxItemsPerDepth = $maxItemsPerDepth;
  158. return $data;
  159. }
  160. /**
  161. * Enables/disables objects' identifiers tracking.
  162. *
  163. * @param bool $useRefHandles False to hide global ref. handles
  164. */
  165. public function withRefHandles(bool $useRefHandles): static
  166. {
  167. $data = clone $this;
  168. $data->useRefHandles = $useRefHandles ? -1 : 0;
  169. return $data;
  170. }
  171. public function withContext(array $context): static
  172. {
  173. $data = clone $this;
  174. $data->context = $context;
  175. return $data;
  176. }
  177. /**
  178. * Seeks to a specific key in nested data structures.
  179. */
  180. public function seek(string|int $key): ?static
  181. {
  182. $item = $this->data[$this->position][$this->key];
  183. if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {
  184. $item = $item->value;
  185. }
  186. if (!($item = $this->getStub($item)) instanceof Stub || !$item->position) {
  187. return null;
  188. }
  189. $keys = [$key];
  190. switch ($item->type) {
  191. case Stub::TYPE_OBJECT:
  192. $keys[] = Caster::PREFIX_DYNAMIC.$key;
  193. $keys[] = Caster::PREFIX_PROTECTED.$key;
  194. $keys[] = Caster::PREFIX_VIRTUAL.$key;
  195. $keys[] = "\0$item->class\0$key";
  196. // no break
  197. case Stub::TYPE_ARRAY:
  198. case Stub::TYPE_RESOURCE:
  199. break;
  200. default:
  201. return null;
  202. }
  203. $data = null;
  204. $children = $this->data[$item->position];
  205. foreach ($keys as $key) {
  206. if (isset($children[$key]) || \array_key_exists($key, $children)) {
  207. $data = clone $this;
  208. $data->key = $key;
  209. $data->position = $item->position;
  210. break;
  211. }
  212. }
  213. return $data;
  214. }
  215. /**
  216. * Dumps data with a DumperInterface dumper.
  217. */
  218. public function dump(DumperInterface $dumper)
  219. {
  220. $refs = [0];
  221. $cursor = new Cursor();
  222. if ($cursor->attr = $this->context[SourceContextProvider::class] ?? []) {
  223. $cursor->attr['if_links'] = true;
  224. $cursor->hashType = -1;
  225. $dumper->dumpScalar($cursor, 'default', '^');
  226. $cursor->attr = ['if_links' => true];
  227. $dumper->dumpScalar($cursor, 'default', ' ');
  228. $cursor->hashType = 0;
  229. }
  230. $this->dumpItem($dumper, $cursor, $refs, $this->data[$this->position][$this->key]);
  231. }
  232. /**
  233. * Depth-first dumping of items.
  234. *
  235. * @param mixed $item A Stub object or the original value being dumped
  236. */
  237. private function dumpItem(DumperInterface $dumper, Cursor $cursor, array &$refs, mixed $item)
  238. {
  239. $cursor->refIndex = 0;
  240. $cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0;
  241. $cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0;
  242. $firstSeen = true;
  243. if (!$item instanceof Stub) {
  244. $cursor->attr = [];
  245. $type = \gettype($item);
  246. if ($item && 'array' === $type) {
  247. $item = $this->getStub($item);
  248. }
  249. } elseif (Stub::TYPE_REF === $item->type) {
  250. if ($item->handle) {
  251. if (!isset($refs[$r = $item->handle - (\PHP_INT_MAX >> 1)])) {
  252. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  253. } else {
  254. $firstSeen = false;
  255. }
  256. $cursor->hardRefTo = $refs[$r];
  257. $cursor->hardRefHandle = $this->useRefHandles & $item->handle;
  258. $cursor->hardRefCount = 0 < $item->handle ? $item->refCount : 0;
  259. }
  260. $cursor->attr = $item->attr;
  261. $type = $item->class ?: \gettype($item->value);
  262. $item = $this->getStub($item->value);
  263. }
  264. if ($item instanceof Stub) {
  265. if ($item->refCount) {
  266. if (!isset($refs[$r = $item->handle])) {
  267. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  268. } else {
  269. $firstSeen = false;
  270. }
  271. $cursor->softRefTo = $refs[$r];
  272. }
  273. $cursor->softRefHandle = $this->useRefHandles & $item->handle;
  274. $cursor->softRefCount = $item->refCount;
  275. $cursor->attr = $item->attr;
  276. $cut = $item->cut;
  277. if ($item->position && $firstSeen) {
  278. $children = $this->data[$item->position];
  279. if ($cursor->stop) {
  280. if ($cut >= 0) {
  281. $cut += \count($children);
  282. }
  283. $children = [];
  284. }
  285. } else {
  286. $children = [];
  287. }
  288. switch ($item->type) {
  289. case Stub::TYPE_STRING:
  290. $dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut);
  291. break;
  292. case Stub::TYPE_ARRAY:
  293. $item = clone $item;
  294. $item->type = $item->class;
  295. $item->class = $item->value;
  296. // no break
  297. case Stub::TYPE_OBJECT:
  298. case Stub::TYPE_RESOURCE:
  299. $withChildren = $children && $cursor->depth !== $this->maxDepth && $this->maxItemsPerDepth;
  300. $dumper->enterHash($cursor, $item->type, $item->class, $withChildren);
  301. if ($withChildren) {
  302. if ($cursor->skipChildren) {
  303. $withChildren = false;
  304. $cut = -1;
  305. } else {
  306. $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type, null !== $item->class);
  307. }
  308. } elseif ($children && 0 <= $cut) {
  309. $cut += \count($children);
  310. }
  311. $cursor->skipChildren = false;
  312. $dumper->leaveHash($cursor, $item->type, $item->class, $withChildren, $cut);
  313. break;
  314. default:
  315. throw new \RuntimeException(sprintf('Unexpected Stub type: "%s".', $item->type));
  316. }
  317. } elseif ('array' === $type) {
  318. $dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false);
  319. $dumper->leaveHash($cursor, Cursor::HASH_INDEXED, 0, false, 0);
  320. } elseif ('string' === $type) {
  321. $dumper->dumpString($cursor, $item, false, 0);
  322. } else {
  323. $dumper->dumpScalar($cursor, $type, $item);
  324. }
  325. }
  326. /**
  327. * Dumps children of hash structures.
  328. *
  329. * @return int The final number of removed items
  330. */
  331. private function dumpChildren(DumperInterface $dumper, Cursor $parentCursor, array &$refs, array $children, int $hashCut, int $hashType, bool $dumpKeys): int
  332. {
  333. $cursor = clone $parentCursor;
  334. ++$cursor->depth;
  335. $cursor->hashType = $hashType;
  336. $cursor->hashIndex = 0;
  337. $cursor->hashLength = \count($children);
  338. $cursor->hashCut = $hashCut;
  339. foreach ($children as $key => $child) {
  340. $cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
  341. $cursor->hashKey = $dumpKeys ? $key : null;
  342. $this->dumpItem($dumper, $cursor, $refs, $child);
  343. if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
  344. $parentCursor->stop = true;
  345. return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;
  346. }
  347. }
  348. return $hashCut;
  349. }
  350. private function getStub(mixed $item)
  351. {
  352. if (!$item || !\is_array($item)) {
  353. return $item;
  354. }
  355. $stub = new Stub();
  356. $stub->type = Stub::TYPE_ARRAY;
  357. foreach ($item as $stub->class => $stub->position) {
  358. }
  359. if (isset($item[0])) {
  360. $stub->cut = $item[0];
  361. }
  362. $stub->value = $stub->cut + ($stub->position ? \count($this->data[$stub->position]) : 0);
  363. return $stub;
  364. }
  365. }