VarCloner.php 12 KB

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