No Description

Data.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /**
  12. * @author Nicolas Grekas <p@tchwork.com>
  13. */
  14. class Data
  15. {
  16. private $data;
  17. private $maxDepth = 20;
  18. private $maxItemsPerDepth = -1;
  19. private $useRefHandles = -1;
  20. /**
  21. * @param array $data A array as returned by ClonerInterface::cloneVar().
  22. */
  23. public function __construct(array $data)
  24. {
  25. $this->data = $data;
  26. }
  27. /**
  28. * @return array The raw data structure.
  29. */
  30. public function getRawData()
  31. {
  32. return $this->data;
  33. }
  34. /**
  35. * Returns a depth limited clone of $this.
  36. *
  37. * @param int $maxDepth The max dumped depth level.
  38. * @param int $maxItemsPerDepth The max number of items dumped per depth level.
  39. * @param bool $useRefHandles False to hide ref. handles.
  40. *
  41. * @return self A depth limited clone of $this.
  42. */
  43. public function getLimitedClone($maxDepth, $maxItemsPerDepth, $useRefHandles = true)
  44. {
  45. $data = clone $this;
  46. $data->maxDepth = (int) $maxDepth;
  47. $data->maxItemsPerDepth = (int) $maxItemsPerDepth;
  48. $data->useRefHandles = $useRefHandles ? -1 : 0;
  49. return $data;
  50. }
  51. /**
  52. * Dumps data with a DumperInterface dumper.
  53. */
  54. public function dump(DumperInterface $dumper)
  55. {
  56. $refs = array(0);
  57. $this->dumpItem($dumper, new Cursor(), $refs, $this->data[0][0]);
  58. }
  59. /**
  60. * Depth-first dumping of items.
  61. *
  62. * @param DumperInterface $dumper The dumper being used for dumping.
  63. * @param Cursor $cursor A cursor used for tracking dumper state position.
  64. * @param array &$refs A map of all references discovered while dumping.
  65. * @param mixed $item A Stub object or the original value being dumped.
  66. */
  67. private function dumpItem($dumper, $cursor, &$refs, $item)
  68. {
  69. $cursor->refIndex = 0;
  70. $cursor->softRefTo = $cursor->softRefHandle = $cursor->softRefCount = 0;
  71. $cursor->hardRefTo = $cursor->hardRefHandle = $cursor->hardRefCount = 0;
  72. $firstSeen = true;
  73. if (!$item instanceof Stub) {
  74. $type = gettype($item);
  75. } elseif (Stub::TYPE_REF === $item->type) {
  76. if ($item->handle) {
  77. if (!isset($refs[$r = $item->handle - (PHP_INT_MAX >> 1)])) {
  78. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  79. } else {
  80. $firstSeen = false;
  81. }
  82. $cursor->hardRefTo = $refs[$r];
  83. $cursor->hardRefHandle = $this->useRefHandles & $item->handle;
  84. $cursor->hardRefCount = $item->refCount;
  85. }
  86. $type = $item->class ?: gettype($item->value);
  87. $item = $item->value;
  88. }
  89. if ($item instanceof Stub) {
  90. if ($item->refCount) {
  91. if (!isset($refs[$r = $item->handle])) {
  92. $cursor->refIndex = $refs[$r] = $cursor->refIndex ?: ++$refs[0];
  93. } else {
  94. $firstSeen = false;
  95. }
  96. $cursor->softRefTo = $refs[$r];
  97. }
  98. $cursor->softRefHandle = $this->useRefHandles & $item->handle;
  99. $cursor->softRefCount = $item->refCount;
  100. $cut = $item->cut;
  101. if ($item->position && $firstSeen) {
  102. $children = $this->data[$item->position];
  103. if ($cursor->stop) {
  104. if ($cut >= 0) {
  105. $cut += count($children);
  106. }
  107. $children = array();
  108. }
  109. } else {
  110. $children = array();
  111. }
  112. switch ($item->type) {
  113. case Stub::TYPE_STRING:
  114. $dumper->dumpString($cursor, $item->value, Stub::STRING_BINARY === $item->class, $cut);
  115. break;
  116. case Stub::TYPE_ARRAY:
  117. $dumper->enterHash($cursor, $item->class, $item->value, (bool) $children);
  118. $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->class);
  119. $dumper->leaveHash($cursor, $item->class, $item->value, (bool) $children, $cut);
  120. break;
  121. case Stub::TYPE_OBJECT:
  122. case Stub::TYPE_RESOURCE:
  123. $dumper->enterHash($cursor, $item->type, $item->class, (bool) $children);
  124. $cut = $this->dumpChildren($dumper, $cursor, $refs, $children, $cut, $item->type);
  125. $dumper->leaveHash($cursor, $item->type, $item->class, (bool) $children, $cut);
  126. break;
  127. default:
  128. throw new \RuntimeException(sprintf('Unexpected Stub type: %s', $item->type));
  129. }
  130. } elseif ('array' === $type) {
  131. $dumper->enterHash($cursor, Cursor::HASH_INDEXED, 0, false);
  132. $dumper->leaveHash($cursor, Cursor::HASH_INDEXED, 0, false, 0);
  133. } elseif ('string' === $type) {
  134. $dumper->dumpString($cursor, $item, false, 0);
  135. } else {
  136. $dumper->dumpScalar($cursor, $type, $item);
  137. }
  138. }
  139. /**
  140. * Dumps children of hash structures.
  141. *
  142. * @param DumperInterface $dumper
  143. * @param Cursor $parentCursor The cursor of the parent hash.
  144. * @param array &$refs A map of all references discovered while dumping.
  145. * @param array $children The children to dump.
  146. * @param int $hashCut The number of items removed from the original hash.
  147. * @param string $hashType A Cursor::HASH_* const.
  148. *
  149. * @return int The final number of removed items.
  150. */
  151. private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType)
  152. {
  153. if ($children) {
  154. if ($parentCursor->depth !== $this->maxDepth && $this->maxItemsPerDepth) {
  155. $cursor = clone $parentCursor;
  156. ++$cursor->depth;
  157. $cursor->hashType = $hashType;
  158. $cursor->hashIndex = 0;
  159. $cursor->hashLength = count($children);
  160. $cursor->hashCut = $hashCut;
  161. foreach ($children as $key => $child) {
  162. $cursor->hashKeyIsBinary = isset($key[0]) && !preg_match('//u', $key);
  163. $cursor->hashKey = $key;
  164. $this->dumpItem($dumper, $cursor, $refs, $child);
  165. if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
  166. $parentCursor->stop = true;
  167. return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;
  168. }
  169. }
  170. } elseif ($hashCut >= 0) {
  171. $hashCut += count($children);
  172. }
  173. }
  174. return $hashCut;
  175. }
  176. }