Data.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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|null|Data[] 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. * Returns a depth limited clone of $this.
  143. *
  144. * @param int $maxDepth The max dumped depth level
  145. *
  146. * @return self A clone of $this
  147. */
  148. public function withMaxDepth($maxDepth)
  149. {
  150. $data = clone $this;
  151. $data->maxDepth = (int) $maxDepth;
  152. return $data;
  153. }
  154. /**
  155. * Limits the number of elements per depth level.
  156. *
  157. * @param int $maxItemsPerDepth The max number of items dumped per depth level
  158. *
  159. * @return self A clone of $this
  160. */
  161. public function withMaxItemsPerDepth($maxItemsPerDepth)
  162. {
  163. $data = clone $this;
  164. $data->maxItemsPerDepth = (int) $maxItemsPerDepth;
  165. return $data;
  166. }
  167. /**
  168. * Enables/disables objects' identifiers tracking.
  169. *
  170. * @param bool $useRefHandles False to hide global ref. handles
  171. *
  172. * @return self A clone of $this
  173. */
  174. public function withRefHandles($useRefHandles)
  175. {
  176. $data = clone $this;
  177. $data->useRefHandles = $useRefHandles ? -1 : 0;
  178. return $data;
  179. }
  180. /**
  181. * Seeks to a specific key in nested data structures.
  182. *
  183. * @param string|int $key The key to seek to
  184. *
  185. * @return self|null A clone of $this or null if the key is not set
  186. */
  187. public function seek($key)
  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;
  195. }
  196. $keys = array($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;
  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. public function dump(DumperInterface $dumper)
  226. {
  227. $refs = array(0);
  228. $this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]);
  229. }
  230. /**
  231. * Depth-first dumping of items.
  232. *
  233. * @param DumperInterface $dumper The dumper being used for dumping
  234. * @param Cursor $cursor A cursor used for tracking dumper state position
  235. * @param array &$refs A map of all references discovered while dumping
  236. * @param mixed $item A Stub object or the original value being dumped
  237. */
  238. private function dumpItem($dumper, $cursor, &$refs, $item)
  239. {
  240. $cursor->refIndex = 0;
  241. $cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0;
  242. $cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0;
  243. $firstSeen = true;
  244. if (!$item instanceof Stub) {
  245. $cursor->attr = array();
  246. $type = \gettype($item);
  247. if ($item && 'array' === $type) {
  248. $item = $this->getStub($item);
  249. }
  250. } elseif (Stub::TYPE_REF === $item->type) {
  251. if ($item->handle) {
  252. if (!isset($refs[$r = $item->handle - (PHP_INT_MAX >> 1)])) {
  253. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  254. } else {
  255. $firstSeen = false;
  256. }
  257. $cursor->hardRefTo = $refs[$r];
  258. $cursor->hardRefHandle = $this->useRefHandles & $item->handle;
  259. $cursor->hardRefCount = $item->refCount;
  260. }
  261. $cursor->attr = $item->attr;
  262. $type = $item->class ?: \gettype($item->value);
  263. $item = $this->getStub($item->value);
  264. }
  265. if ($item instanceof Stub) {
  266. if ($item->refCount) {
  267. if (!isset($refs[$r = $item->handle])) {
  268. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  269. } else {
  270. $firstSeen = false;
  271. }
  272. $cursor->softRefTo = $refs[$r];
  273. }
  274. $cursor->softRefHandle = $this->useRefHandles & $item->handle;
  275. $cursor->softRefCount = $item->refCount;
  276. $cursor->attr = $item->attr;
  277. $cut = $item->cut;
  278. if ($item->position && $firstSeen) {
  279. $children = $this->data[$item->position];
  280. if ($cursor->stop) {
  281. if ($cut >= 0) {
  282. $cut += \count($children);
  283. }
  284. $children = array();
  285. }
  286. } else {
  287. $children = array();
  288. }
  289. switch ($item->type) {
  290. case Stub::TYPE_STRING:
  291. $dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut);
  292. break;
  293. case Stub::TYPE_ARRAY:
  294. $item = clone $item;
  295. $item->type = $item->class;
  296. $item->class = $item->value;
  297. // no break
  298. case Stub::TYPE_OBJECT:
  299. case Stub::TYPE_RESOURCE:
  300. $withChildren = $children && $cursor->depth !== $this->maxDepth && $this->maxItemsPerDepth;
  301. $dumper->enterHash($cursor, $item->type, $item->class, $withChildren);
  302. if ($withChildren) {
  303. if ($cursor->skipChildren) {
  304. $withChildren = false;
  305. $cut = -1;
  306. } else {
  307. $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type, null !== $item->class);
  308. }
  309. } elseif ($children && 0 <= $cut) {
  310. $cut += \count($children);
  311. }
  312. $cursor->skipChildren = false;
  313. $dumper->leaveHash($cursor, $item->type, $item->class, $withChildren, $cut);
  314. break;
  315. default:
  316. throw new \RuntimeException(sprintf('Unexpected Stub type: %s', $item->type));
  317. }
  318. } elseif ('array' === $type) {
  319. $dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false);
  320. $dumper->leaveHash($cursor, Cursor::HASH_INDEXED, 0, false, 0);
  321. } elseif ('string' === $type) {
  322. $dumper->dumpString($cursor, $item, false, 0);
  323. } else {
  324. $dumper->dumpScalar($cursor, $type, $item);
  325. }
  326. }
  327. /**
  328. * Dumps children of hash structures.
  329. *
  330. * @param DumperInterface $dumper
  331. * @param Cursor $parentCursor The cursor of the parent hash
  332. * @param array &$refs A map of all references discovered while dumping
  333. * @param array $children The children to dump
  334. * @param int $hashCut The number of items removed from the original hash
  335. * @param string $hashType A Cursor::HASH_* const
  336. * @param bool $dumpKeys Whether keys should be dumped or not
  337. *
  338. * @return int The final number of removed items
  339. */
  340. private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType, $dumpKeys)
  341. {
  342. $cursor = clone $parentCursor;
  343. ++$cursor->depth;
  344. $cursor->hashType = $hashType;
  345. $cursor->hashIndex = 0;
  346. $cursor->hashLength = \count($children);
  347. $cursor->hashCut = $hashCut;
  348. foreach ($children as $key => $child) {
  349. $cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
  350. $cursor->hashKey = $dumpKeys ? $key : null;
  351. $this->dumpItem($dumper, $cursor, $refs, $child);
  352. if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
  353. $parentCursor->stop = true;
  354. return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;
  355. }
  356. }
  357. return $hashCut;
  358. }
  359. private function getStub($item)
  360. {
  361. if (!$item || !\is_array($item)) {
  362. return $item;
  363. }
  364. $stub = new Stub();
  365. $stub->type = Stub::TYPE_ARRAY;
  366. foreach ($item as $stub->class => $stub->position) {
  367. }
  368. if (isset($item[0])) {
  369. $stub->cut = $item[0];
  370. }
  371. $stub->value = $stub->cut + ($stub->position ? \count($this->data[$stub->position]) : 0);
  372. return $stub;
  373. }
  374. }