You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

311 lines
14 KiB

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