Нет описания

Data.php 14KB

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