VarCloner.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 VarCloner extends AbstractCloner
  15. {
  16. private static $gid;
  17. private static $hashMask = 0;
  18. private static $hashOffset = 0;
  19. private static $arrayCache = array();
  20. /**
  21. * {@inheritdoc}
  22. */
  23. protected function doClone($var)
  24. {
  25. $len = 1; // Length of $queue
  26. $pos = 0; // Number of cloned items past the minimum depth
  27. $refsCounter = 0; // Hard references counter
  28. $queue = array(array($var)); // This breadth-first queue is the return value
  29. $indexedArrays = array(); // Map of queue indexes that hold numerically indexed arrays
  30. $hardRefs = array(); // Map of original zval hashes to stub objects
  31. $objRefs = array(); // Map of original object handles to their stub object couterpart
  32. $resRefs = array(); // Map of original resource handles to their stub object couterpart
  33. $values = array(); // Map of stub objects' hashes to original values
  34. $maxItems = $this->maxItems;
  35. $maxString = $this->maxString;
  36. $minDepth = $this->minDepth;
  37. $currentDepth = 0; // Current tree depth
  38. $currentDepthFinalIndex = 0; // Final $queue index for current tree depth
  39. $minimumDepthReached = 0 === $minDepth; // Becomes true when minimum tree depth has been reached
  40. $cookie = (object) array(); // Unique object used to detect hard references
  41. $a = null; // Array cast for nested structures
  42. $stub = null; // Stub capturing the main properties of an original item value
  43. // or null if the original value is used directly
  44. if (!self::$hashMask) {
  45. self::$gid = uniqid(mt_rand(), true); // Unique string used to detect the special $GLOBALS variable
  46. self::initHashMask();
  47. }
  48. $gid = self::$gid;
  49. $hashMask = self::$hashMask;
  50. $hashOffset = self::$hashOffset;
  51. $arrayStub = new Stub();
  52. $arrayStub->type = Stub::TYPE_ARRAY;
  53. $fromObjCast = false;
  54. for ($i = 0; $i < $len; ++$i) {
  55. // Detect when we move on to the next tree depth
  56. if ($i > $currentDepthFinalIndex) {
  57. ++$currentDepth;
  58. $currentDepthFinalIndex = $len - 1;
  59. if ($currentDepth >= $minDepth) {
  60. $minimumDepthReached = true;
  61. }
  62. }
  63. $refs = $vals = $queue[$i];
  64. if (\PHP_VERSION_ID < 70200 && empty($indexedArrays[$i])) {
  65. // see https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
  66. foreach ($vals as $k => $v) {
  67. if (\is_int($k)) {
  68. continue;
  69. }
  70. foreach (array($k => true) as $gk => $gv) {
  71. }
  72. if ($gk !== $k) {
  73. $fromObjCast = true;
  74. $refs = $vals = \array_values($queue[$i]);
  75. break;
  76. }
  77. }
  78. }
  79. foreach ($vals as $k => $v) {
  80. // $v is the original value or a stub object in case of hard references
  81. $refs[$k] = $cookie;
  82. if ($zvalIsRef = $vals[$k] === $cookie) {
  83. $vals[$k] = &$stub; // Break hard references to make $queue completely
  84. unset($stub); // independent from the original structure
  85. if ($v instanceof Stub && isset($hardRefs[\spl_object_hash($v)])) {
  86. $vals[$k] = $refs[$k] = $v;
  87. if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
  88. ++$v->value->refCount;
  89. }
  90. ++$v->refCount;
  91. continue;
  92. }
  93. $refs[$k] = $vals[$k] = new Stub();
  94. $refs[$k]->value = $v;
  95. $h = \spl_object_hash($refs[$k]);
  96. $hardRefs[$h] = &$refs[$k];
  97. $values[$h] = $v;
  98. $vals[$k]->handle = ++$refsCounter;
  99. }
  100. // Create $stub when the original value $v can not be used directly
  101. // If $v is a nested structure, put that structure in array $a
  102. switch (true) {
  103. case null === $v:
  104. case \is_bool($v):
  105. case \is_int($v):
  106. case \is_float($v):
  107. continue 2;
  108. case \is_string($v):
  109. if ('' === $v) {
  110. continue 2;
  111. }
  112. if (!\preg_match('//u', $v)) {
  113. $stub = new Stub();
  114. $stub->type = Stub::TYPE_STRING;
  115. $stub->class = Stub::STRING_BINARY;
  116. if (0 <= $maxString && 0 < $cut = \strlen($v) - $maxString) {
  117. $stub->cut = $cut;
  118. $stub->value = \substr($v, 0, -$cut);
  119. } else {
  120. $stub->value = $v;
  121. }
  122. } elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = \mb_strlen($v, 'UTF-8') - $maxString) {
  123. $stub = new Stub();
  124. $stub->type = Stub::TYPE_STRING;
  125. $stub->class = Stub::STRING_UTF8;
  126. $stub->cut = $cut;
  127. $stub->value = \mb_substr($v, 0, $maxString, 'UTF-8');
  128. } else {
  129. continue 2;
  130. }
  131. $a = null;
  132. break;
  133. case \is_array($v):
  134. if (!$v) {
  135. continue 2;
  136. }
  137. $stub = $arrayStub;
  138. $stub->class = Stub::ARRAY_INDEXED;
  139. $j = -1;
  140. foreach ($v as $gk => $gv) {
  141. if ($gk !== ++$j) {
  142. $stub->class = Stub::ARRAY_ASSOC;
  143. break;
  144. }
  145. }
  146. $a = $v;
  147. if (Stub::ARRAY_ASSOC === $stub->class) {
  148. // Copies of $GLOBALS have very strange behavior,
  149. // let's detect them with some black magic
  150. $a[$gid] = true;
  151. // Happens with copies of $GLOBALS
  152. if (isset($v[$gid])) {
  153. unset($v[$gid]);
  154. $a = array();
  155. foreach ($v as $gk => &$gv) {
  156. $a[$gk] = &$gv;
  157. }
  158. unset($gv);
  159. } else {
  160. $a = $v;
  161. }
  162. } elseif (\PHP_VERSION_ID < 70200) {
  163. $indexedArrays[$len] = true;
  164. }
  165. break;
  166. case \is_object($v):
  167. case $v instanceof \__PHP_Incomplete_Class:
  168. if (empty($objRefs[$h = $hashMask ^ \hexdec(\substr(\spl_object_hash($v), $hashOffset, \PHP_INT_SIZE))])) {
  169. $stub = new Stub();
  170. $stub->type = Stub::TYPE_OBJECT;
  171. $stub->class = \get_class($v);
  172. $stub->value = $v;
  173. $stub->handle = $h;
  174. $a = $this->castObject($stub, 0 < $i);
  175. if ($v !== $stub->value) {
  176. if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
  177. break;
  178. }
  179. $h = $hashMask ^ \hexdec(\substr(\spl_object_hash($stub->value), $hashOffset, \PHP_INT_SIZE));
  180. $stub->handle = $h;
  181. }
  182. $stub->value = null;
  183. if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  184. $stub->cut = \count($a);
  185. $a = null;
  186. }
  187. }
  188. if (empty($objRefs[$h])) {
  189. $objRefs[$h] = $stub;
  190. } else {
  191. $stub = $objRefs[$h];
  192. ++$stub->refCount;
  193. $a = null;
  194. }
  195. break;
  196. default: // resource
  197. if (empty($resRefs[$h = (int) $v])) {
  198. $stub = new Stub();
  199. $stub->type = Stub::TYPE_RESOURCE;
  200. if ('Unknown' === $stub->class = @\get_resource_type($v)) {
  201. $stub->class = 'Closed';
  202. }
  203. $stub->value = $v;
  204. $stub->handle = $h;
  205. $a = $this->castResource($stub, 0 < $i);
  206. $stub->value = null;
  207. if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  208. $stub->cut = \count($a);
  209. $a = null;
  210. }
  211. }
  212. if (empty($resRefs[$h])) {
  213. $resRefs[$h] = $stub;
  214. } else {
  215. $stub = $resRefs[$h];
  216. ++$stub->refCount;
  217. $a = null;
  218. }
  219. break;
  220. }
  221. if ($a) {
  222. if (!$minimumDepthReached || 0 > $maxItems) {
  223. $queue[$len] = $a;
  224. $stub->position = $len++;
  225. } elseif ($pos < $maxItems) {
  226. if ($maxItems < $pos += \count($a)) {
  227. $a = \array_slice($a, 0, $maxItems - $pos);
  228. if ($stub->cut >= 0) {
  229. $stub->cut += $pos - $maxItems;
  230. }
  231. }
  232. $queue[$len] = $a;
  233. $stub->position = $len++;
  234. } elseif ($stub->cut >= 0) {
  235. $stub->cut += \count($a);
  236. $stub->position = 0;
  237. }
  238. }
  239. if ($arrayStub === $stub) {
  240. if ($arrayStub->cut) {
  241. $stub = array($arrayStub->cut, $arrayStub->class => $arrayStub->position);
  242. $arrayStub->cut = 0;
  243. } elseif (isset(self::$arrayCache[$arrayStub->class][$arrayStub->position])) {
  244. $stub = self::$arrayCache[$arrayStub->class][$arrayStub->position];
  245. } else {
  246. self::$arrayCache[$arrayStub->class][$arrayStub->position] = $stub = array($arrayStub->class => $arrayStub->position);
  247. }
  248. }
  249. if ($zvalIsRef) {
  250. $refs[$k]->value = $stub;
  251. } else {
  252. $vals[$k] = $stub;
  253. }
  254. }
  255. if ($fromObjCast) {
  256. $fromObjCast = false;
  257. $refs = $vals;
  258. $vals = array();
  259. $j = -1;
  260. foreach ($queue[$i] as $k => $v) {
  261. foreach (array($k => true) as $gk => $gv) {
  262. }
  263. if ($gk !== $k) {
  264. $vals = (object) $vals;
  265. $vals->{$k} = $refs[++$j];
  266. $vals = (array) $vals;
  267. } else {
  268. $vals[$k] = $refs[++$j];
  269. }
  270. }
  271. }
  272. $queue[$i] = $vals;
  273. }
  274. foreach ($values as $h => $v) {
  275. $hardRefs[$h] = $v;
  276. }
  277. return $queue;
  278. }
  279. private static function initHashMask()
  280. {
  281. $obj = (object) array();
  282. self::$hashOffset = 16 - PHP_INT_SIZE;
  283. self::$hashMask = -1;
  284. if (\defined('HHVM_VERSION')) {
  285. self::$hashOffset += 16;
  286. } else {
  287. // check if we are nested in an output buffering handler to prevent a fatal error with ob_start() below
  288. $obFuncs = array('ob_clean', 'ob_end_clean', 'ob_flush', 'ob_end_flush', 'ob_get_contents', 'ob_get_flush');
  289. foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
  290. if (isset($frame['function'][0]) && !isset($frame['class']) && 'o' === $frame['function'][0] && \in_array($frame['function'], $obFuncs)) {
  291. $frame['line'] = 0;
  292. break;
  293. }
  294. }
  295. if (!empty($frame['line'])) {
  296. ob_start();
  297. debug_zval_dump($obj);
  298. self::$hashMask = (int) substr(ob_get_clean(), 17);
  299. }
  300. }
  301. self::$hashMask ^= hexdec(substr(spl_object_hash($obj), self::$hashOffset, PHP_INT_SIZE));
  302. }
  303. }