VarCloner.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 array $arrayCache = [];
  17. protected function doClone(mixed $var): array
  18. {
  19. $len = 1; // Length of $queue
  20. $pos = 0; // Number of cloned items past the minimum depth
  21. $refsCounter = 0; // Hard references counter
  22. $queue = [[$var]]; // This breadth-first queue is the return value
  23. $hardRefs = []; // Map of original zval ids to stub objects
  24. $objRefs = []; // Map of original object handles to their stub object counterpart
  25. $objects = []; // Keep a ref to objects to ensure their handle cannot be reused while cloning
  26. $resRefs = []; // Map of original resource handles to their stub object counterpart
  27. $maxItems = $this->maxItems;
  28. $maxString = $this->maxString;
  29. $minDepth = $this->minDepth;
  30. $currentDepth = 0; // Current tree depth
  31. $currentDepthFinalIndex = 0; // Final $queue index for current tree depth
  32. $minimumDepthReached = 0 === $minDepth; // Becomes true when minimum tree depth has been reached
  33. $a = null; // Array cast for nested structures
  34. $stub = null; // Stub capturing the main properties of an original item value
  35. // or null if the original value is used directly
  36. $arrayStub = new Stub();
  37. $arrayStub->type = Stub::TYPE_ARRAY;
  38. for ($i = 0; $i < $len; ++$i) {
  39. // Detect when we move on to the next tree depth
  40. if ($i > $currentDepthFinalIndex) {
  41. ++$currentDepth;
  42. $currentDepthFinalIndex = $len - 1;
  43. if ($currentDepth >= $minDepth) {
  44. $minimumDepthReached = true;
  45. }
  46. }
  47. $vals = $queue[$i];
  48. foreach ($vals as $k => $v) {
  49. // $v is the original value or a stub object in case of hard references
  50. $zvalRef = ($r = \ReflectionReference::fromArrayElement($vals, $k)) ? $r->getId() : null;
  51. if ($zvalRef) {
  52. $vals[$k] = &$stub; // Break hard references to make $queue completely
  53. unset($stub); // independent from the original structure
  54. if (null !== $vals[$k] = $hardRefs[$zvalRef] ?? null) {
  55. $v = $vals[$k];
  56. if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
  57. ++$v->value->refCount;
  58. }
  59. ++$v->refCount;
  60. continue;
  61. }
  62. $vals[$k] = new Stub();
  63. $vals[$k]->value = $v;
  64. $vals[$k]->handle = ++$refsCounter;
  65. $hardRefs[$zvalRef] = $vals[$k];
  66. }
  67. // Create $stub when the original value $v cannot be used directly
  68. // If $v is a nested structure, put that structure in array $a
  69. switch (true) {
  70. case null === $v:
  71. case \is_bool($v):
  72. case \is_int($v):
  73. case \is_float($v):
  74. continue 2;
  75. case \is_string($v):
  76. if ('' === $v) {
  77. continue 2;
  78. }
  79. if (!preg_match('//u', $v)) {
  80. $stub = new Stub();
  81. $stub->type = Stub::TYPE_STRING;
  82. $stub->class = Stub::STRING_BINARY;
  83. if (0 <= $maxString && 0 < $cut = \strlen($v) - $maxString) {
  84. $stub->cut = $cut;
  85. $stub->value = substr($v, 0, -$cut);
  86. } else {
  87. $stub->value = $v;
  88. }
  89. } elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = mb_strlen($v, 'UTF-8') - $maxString) {
  90. $stub = new Stub();
  91. $stub->type = Stub::TYPE_STRING;
  92. $stub->class = Stub::STRING_UTF8;
  93. $stub->cut = $cut;
  94. $stub->value = mb_substr($v, 0, $maxString, 'UTF-8');
  95. } else {
  96. continue 2;
  97. }
  98. $a = null;
  99. break;
  100. case \is_array($v):
  101. if (!$v) {
  102. continue 2;
  103. }
  104. $stub = $arrayStub;
  105. $stub->class = array_is_list($v) ? Stub::ARRAY_INDEXED : Stub::ARRAY_ASSOC;
  106. $a = $v;
  107. break;
  108. case \is_object($v):
  109. if (empty($objRefs[$h = spl_object_id($v)])) {
  110. $stub = new Stub();
  111. $stub->type = Stub::TYPE_OBJECT;
  112. $stub->class = $v::class;
  113. $stub->value = $v;
  114. $stub->handle = $h;
  115. $a = $this->castObject($stub, 0 < $i);
  116. if ($v !== $stub->value) {
  117. if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
  118. break;
  119. }
  120. $stub->handle = $h = spl_object_id($stub->value);
  121. }
  122. $stub->value = null;
  123. if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  124. $stub->cut = \count($a);
  125. $a = null;
  126. }
  127. }
  128. if (empty($objRefs[$h])) {
  129. $objRefs[$h] = $stub;
  130. $objects[] = $v;
  131. } else {
  132. $stub = $objRefs[$h];
  133. ++$stub->refCount;
  134. $a = null;
  135. }
  136. break;
  137. default: // resource
  138. if (empty($resRefs[$h = (int) $v])) {
  139. $stub = new Stub();
  140. $stub->type = Stub::TYPE_RESOURCE;
  141. if ('Unknown' === $stub->class = @get_resource_type($v)) {
  142. $stub->class = 'Closed';
  143. }
  144. $stub->value = $v;
  145. $stub->handle = $h;
  146. $a = $this->castResource($stub, 0 < $i);
  147. $stub->value = null;
  148. if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  149. $stub->cut = \count($a);
  150. $a = null;
  151. }
  152. }
  153. if (empty($resRefs[$h])) {
  154. $resRefs[$h] = $stub;
  155. } else {
  156. $stub = $resRefs[$h];
  157. ++$stub->refCount;
  158. $a = null;
  159. }
  160. break;
  161. }
  162. if ($a) {
  163. if (!$minimumDepthReached || 0 > $maxItems) {
  164. $queue[$len] = $a;
  165. $stub->position = $len++;
  166. } elseif ($pos < $maxItems) {
  167. if ($maxItems < $pos += \count($a)) {
  168. $a = \array_slice($a, 0, $maxItems - $pos, true);
  169. if ($stub->cut >= 0) {
  170. $stub->cut += $pos - $maxItems;
  171. }
  172. }
  173. $queue[$len] = $a;
  174. $stub->position = $len++;
  175. } elseif ($stub->cut >= 0) {
  176. $stub->cut += \count($a);
  177. $stub->position = 0;
  178. }
  179. }
  180. if ($arrayStub === $stub) {
  181. if ($arrayStub->cut) {
  182. $stub = [$arrayStub->cut, $arrayStub->class => $arrayStub->position];
  183. $arrayStub->cut = 0;
  184. } elseif (isset(self::$arrayCache[$arrayStub->class][$arrayStub->position])) {
  185. $stub = self::$arrayCache[$arrayStub->class][$arrayStub->position];
  186. } else {
  187. self::$arrayCache[$arrayStub->class][$arrayStub->position] = $stub = [$arrayStub->class => $arrayStub->position];
  188. }
  189. }
  190. if (!$zvalRef) {
  191. $vals[$k] = $stub;
  192. } else {
  193. $hardRefs[$zvalRef]->value = $stub;
  194. }
  195. }
  196. $queue[$i] = $vals;
  197. }
  198. return $queue;
  199. }
  200. }