VarCloner.php 14 KB

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