VarCloner.php 14 KB

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