VarCloner.php 12 KB

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