Data.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 A 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 scalar|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 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 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 ($v instanceof Stub && Stub::TYPE_REF === $v->type && $v->value instanceof Stub) {
  82. $recursive = (array) $recursive;
  83. if (isset($recursive[$v->value->position])) {
  84. continue;
  85. }
  86. $recursive[$v->value->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 = $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 version 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 of 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 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. case Stub::TYPE_ARRAY:
  214. case Stub::TYPE_RESOURCE:
  215. break;
  216. default:
  217. return;
  218. }
  219. $data = null;
  220. $children = $this->data[$item->position];
  221. foreach ($keys as $key) {
  222. if (isset($children[$key]) || array_key_exists($key, $children)) {
  223. $data = clone $this;
  224. $data->key = $key;
  225. $data->position = $item->position;
  226. break;
  227. }
  228. }
  229. return $data;
  230. }
  231. /**
  232. * Dumps data with a DumperInterface dumper.
  233. */
  234. public function dump(DumperInterface $dumper)
  235. {
  236. $refs = array(0);
  237. $this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]);
  238. }
  239. /**
  240. * Depth-first dumping of items.
  241. *
  242. * @param DumperInterface $dumper The dumper being used for dumping
  243. * @param Cursor $cursor A cursor used for tracking dumper state position
  244. * @param array &$refs A map of all references discovered while dumping
  245. * @param mixed $item A Stub object or the original value being dumped
  246. */
  247. private function dumpItem($dumper, $cursor, &$refs, $item)
  248. {
  249. $cursor->refIndex = 0;
  250. $cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0;
  251. $cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0;
  252. $firstSeen = true;
  253. if (!$item instanceof Stub) {
  254. $cursor->attr = array();
  255. $type = gettype($item);
  256. } elseif (Stub::TYPE_REF === $item->type) {
  257. if ($item->handle) {
  258. if (!isset($refs[$r = $item->handle - (PHP_INT_MAX >> 1)])) {
  259. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  260. } else {
  261. $firstSeen = false;
  262. }
  263. $cursor->hardRefTo = $refs[$r];
  264. $cursor->hardRefHandle = $this->useRefHandles & $item->handle;
  265. $cursor->hardRefCount = $item->refCount;
  266. }
  267. $cursor->attr = $item->attr;
  268. $type = $item->class ?: gettype($item->value);
  269. $item = $item->value;
  270. }
  271. if ($item instanceof Stub) {
  272. if ($item->refCount) {
  273. if (!isset($refs[$r = $item->handle])) {
  274. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  275. } else {
  276. $firstSeen = false;
  277. }
  278. $cursor->softRefTo = $refs[$r];
  279. }
  280. $cursor->softRefHandle = $this->useRefHandles & $item->handle;
  281. $cursor->softRefCount = $item->refCount;
  282. $cursor->attr = $item->attr;
  283. $cut = $item->cut;
  284. if ($item->position && $firstSeen) {
  285. $children = $this->data[$item->position];
  286. if ($cursor->stop) {
  287. if ($cut >= 0) {
  288. $cut += count($children);
  289. }
  290. $children = array();
  291. }
  292. } else {
  293. $children = array();
  294. }
  295. switch ($item->type) {
  296. case Stub::TYPE_STRING:
  297. $dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut);
  298. break;
  299. case Stub::TYPE_ARRAY:
  300. $item = clone $item;
  301. $item->type = $item->class;
  302. $item->class = $item->value;
  303. // No break;
  304. case Stub::TYPE_OBJECT:
  305. case Stub::TYPE_RESOURCE:
  306. $withChildren = $children && $cursor->depth !== $this->maxDepth && $this->maxItemsPerDepth;
  307. $dumper->enterHash($cursor, $item->type, $item->class, $withChildren);
  308. if ($withChildren) {
  309. $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type, null !== $item->class);
  310. } elseif ($children && 0 <= $cut) {
  311. $cut += count($children);
  312. }
  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. }