| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\VarDumper\Cloner;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
16 |
*/
|
|
|
17 |
class VarCloner extends AbstractCloner
|
|
|
18 |
{
|
|
|
19 |
private static string $gid;
|
|
|
20 |
private static array $arrayCache = [];
|
|
|
21 |
|
|
|
22 |
protected function doClone(mixed $var): array
|
|
|
23 |
{
|
|
|
24 |
$len = 1; // Length of $queue
|
|
|
25 |
$pos = 0; // Number of cloned items past the minimum depth
|
|
|
26 |
$refsCounter = 0; // Hard references counter
|
|
|
27 |
$queue = [[$var]]; // This breadth-first queue is the return value
|
|
|
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 |
|
|
|
44 |
$gid = self::$gid ??= md5(random_bytes(6)); // Unique string used to detect the special $GLOBALS variable
|
|
|
45 |
$arrayStub = new Stub();
|
|
|
46 |
$arrayStub->type = Stub::TYPE_ARRAY;
|
|
|
47 |
$fromObjCast = false;
|
|
|
48 |
|
|
|
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 |
|
|
|
59 |
$refs = $vals = $queue[$i];
|
|
|
60 |
foreach ($vals as $k => $v) {
|
|
|
61 |
// $v is the original value or a stub object in case of hard references
|
|
|
62 |
|
|
|
63 |
$zvalRef = ($r = \ReflectionReference::fromArrayElement($vals, $k)) ? $r->getId() : null;
|
|
|
64 |
|
|
|
65 |
if ($zvalRef) {
|
|
|
66 |
$vals[$k] = &$stub; // Break hard references to make $queue completely
|
|
|
67 |
unset($stub); // independent from the original structure
|
|
|
68 |
if (null !== $vals[$k] = $hardRefs[$zvalRef] ?? null) {
|
|
|
69 |
$v = $vals[$k];
|
|
|
70 |
if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
|
|
|
71 |
++$v->value->refCount;
|
|
|
72 |
}
|
|
|
73 |
++$v->refCount;
|
|
|
74 |
continue;
|
|
|
75 |
}
|
|
|
76 |
$vals[$k] = new Stub();
|
|
|
77 |
$vals[$k]->value = $v;
|
|
|
78 |
$vals[$k]->handle = ++$refsCounter;
|
|
|
79 |
$hardRefs[$zvalRef] = $vals[$k];
|
|
|
80 |
}
|
|
|
81 |
// Create $stub when the original value $v cannot be used directly
|
|
|
82 |
// If $v is a nested structure, put that structure in array $a
|
|
|
83 |
switch (true) {
|
|
|
84 |
case null === $v:
|
|
|
85 |
case \is_bool($v):
|
|
|
86 |
case \is_int($v):
|
|
|
87 |
case \is_float($v):
|
|
|
88 |
continue 2;
|
|
|
89 |
case \is_string($v):
|
|
|
90 |
if ('' === $v) {
|
|
|
91 |
continue 2;
|
|
|
92 |
}
|
|
|
93 |
if (!preg_match('//u', $v)) {
|
|
|
94 |
$stub = new Stub();
|
|
|
95 |
$stub->type = Stub::TYPE_STRING;
|
|
|
96 |
$stub->class = Stub::STRING_BINARY;
|
|
|
97 |
if (0 <= $maxString && 0 < $cut = \strlen($v) - $maxString) {
|
|
|
98 |
$stub->cut = $cut;
|
|
|
99 |
$stub->value = substr($v, 0, -$cut);
|
|
|
100 |
} else {
|
|
|
101 |
$stub->value = $v;
|
|
|
102 |
}
|
|
|
103 |
} elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = mb_strlen($v, 'UTF-8') - $maxString) {
|
|
|
104 |
$stub = new Stub();
|
|
|
105 |
$stub->type = Stub::TYPE_STRING;
|
|
|
106 |
$stub->class = Stub::STRING_UTF8;
|
|
|
107 |
$stub->cut = $cut;
|
|
|
108 |
$stub->value = mb_substr($v, 0, $maxString, 'UTF-8');
|
|
|
109 |
} else {
|
|
|
110 |
continue 2;
|
|
|
111 |
}
|
|
|
112 |
$a = null;
|
|
|
113 |
break;
|
|
|
114 |
|
|
|
115 |
case \is_array($v):
|
|
|
116 |
if (!$v) {
|
|
|
117 |
continue 2;
|
|
|
118 |
}
|
|
|
119 |
$stub = $arrayStub;
|
|
|
120 |
|
|
|
121 |
$stub->class = array_is_list($v) ? Stub::ARRAY_INDEXED : Stub::ARRAY_ASSOC;
|
|
|
122 |
$a = $v;
|
|
|
123 |
break;
|
|
|
124 |
|
|
|
125 |
case \is_object($v):
|
|
|
126 |
if (empty($objRefs[$h = spl_object_id($v)])) {
|
|
|
127 |
$stub = new Stub();
|
|
|
128 |
$stub->type = Stub::TYPE_OBJECT;
|
|
|
129 |
$stub->class = $v::class;
|
|
|
130 |
$stub->value = $v;
|
|
|
131 |
$stub->handle = $h;
|
|
|
132 |
$a = $this->castObject($stub, 0 < $i);
|
|
|
133 |
if ($v !== $stub->value) {
|
|
|
134 |
if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
|
|
|
135 |
break;
|
|
|
136 |
}
|
|
|
137 |
$stub->handle = $h = spl_object_id($stub->value);
|
|
|
138 |
}
|
|
|
139 |
$stub->value = null;
|
|
|
140 |
if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
|
|
|
141 |
$stub->cut = \count($a);
|
|
|
142 |
$a = null;
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
if (empty($objRefs[$h])) {
|
|
|
146 |
$objRefs[$h] = $stub;
|
|
|
147 |
$objects[] = $v;
|
|
|
148 |
} else {
|
|
|
149 |
$stub = $objRefs[$h];
|
|
|
150 |
++$stub->refCount;
|
|
|
151 |
$a = null;
|
|
|
152 |
}
|
|
|
153 |
break;
|
|
|
154 |
|
|
|
155 |
default: // resource
|
|
|
156 |
if (empty($resRefs[$h = (int) $v])) {
|
|
|
157 |
$stub = new Stub();
|
|
|
158 |
$stub->type = Stub::TYPE_RESOURCE;
|
|
|
159 |
if ('Unknown' === $stub->class = @get_resource_type($v)) {
|
|
|
160 |
$stub->class = 'Closed';
|
|
|
161 |
}
|
|
|
162 |
$stub->value = $v;
|
|
|
163 |
$stub->handle = $h;
|
|
|
164 |
$a = $this->castResource($stub, 0 < $i);
|
|
|
165 |
$stub->value = null;
|
|
|
166 |
if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
|
|
|
167 |
$stub->cut = \count($a);
|
|
|
168 |
$a = null;
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
if (empty($resRefs[$h])) {
|
|
|
172 |
$resRefs[$h] = $stub;
|
|
|
173 |
} else {
|
|
|
174 |
$stub = $resRefs[$h];
|
|
|
175 |
++$stub->refCount;
|
|
|
176 |
$a = null;
|
|
|
177 |
}
|
|
|
178 |
break;
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
if ($a) {
|
|
|
182 |
if (!$minimumDepthReached || 0 > $maxItems) {
|
|
|
183 |
$queue[$len] = $a;
|
|
|
184 |
$stub->position = $len++;
|
|
|
185 |
} elseif ($pos < $maxItems) {
|
|
|
186 |
if ($maxItems < $pos += \count($a)) {
|
|
|
187 |
$a = \array_slice($a, 0, $maxItems - $pos, true);
|
|
|
188 |
if ($stub->cut >= 0) {
|
|
|
189 |
$stub->cut += $pos - $maxItems;
|
|
|
190 |
}
|
|
|
191 |
}
|
|
|
192 |
$queue[$len] = $a;
|
|
|
193 |
$stub->position = $len++;
|
|
|
194 |
} elseif ($stub->cut >= 0) {
|
|
|
195 |
$stub->cut += \count($a);
|
|
|
196 |
$stub->position = 0;
|
|
|
197 |
}
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
if ($arrayStub === $stub) {
|
|
|
201 |
if ($arrayStub->cut) {
|
|
|
202 |
$stub = [$arrayStub->cut, $arrayStub->class => $arrayStub->position];
|
|
|
203 |
$arrayStub->cut = 0;
|
|
|
204 |
} elseif (isset(self::$arrayCache[$arrayStub->class][$arrayStub->position])) {
|
|
|
205 |
$stub = self::$arrayCache[$arrayStub->class][$arrayStub->position];
|
|
|
206 |
} else {
|
|
|
207 |
self::$arrayCache[$arrayStub->class][$arrayStub->position] = $stub = [$arrayStub->class => $arrayStub->position];
|
|
|
208 |
}
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
if (!$zvalRef) {
|
|
|
212 |
$vals[$k] = $stub;
|
|
|
213 |
} else {
|
|
|
214 |
$hardRefs[$zvalRef]->value = $stub;
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
if ($fromObjCast) {
|
|
|
219 |
$fromObjCast = false;
|
|
|
220 |
$refs = $vals;
|
|
|
221 |
$vals = [];
|
|
|
222 |
$j = -1;
|
|
|
223 |
foreach ($queue[$i] as $k => $v) {
|
|
|
224 |
foreach ([$k => true] as $gk => $gv) {
|
|
|
225 |
}
|
|
|
226 |
if ($gk !== $k) {
|
|
|
227 |
$vals = (object) $vals;
|
|
|
228 |
$vals->{$k} = $refs[++$j];
|
|
|
229 |
$vals = (array) $vals;
|
|
|
230 |
} else {
|
|
|
231 |
$vals[$k] = $refs[++$j];
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
$queue[$i] = $vals;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
foreach ($values as $h => $v) {
|
|
|
240 |
$hardRefs[$h] = $v;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
return $queue;
|
|
|
244 |
}
|
|
|
245 |
}
|