Data.php 14 KB

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