comparison vendor/symfony/var-dumper/Cloner/VarCloner.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 $gid;
20 private static $hashMask = 0;
21 private static $hashOffset = 0;
22 private static $arrayCache = array();
23
24 /**
25 * {@inheritdoc}
26 */
27 protected function doClone($var)
28 {
29 $len = 1; // Length of $queue
30 $pos = 0; // Number of cloned items past the first level
31 $refsCounter = 0; // Hard references counter
32 $queue = array(array($var)); // This breadth-first queue is the return value
33 $indexedArrays = array(); // Map of queue indexes that hold numerically indexed arrays
34 $hardRefs = array(); // Map of original zval hashes to stub objects
35 $objRefs = array(); // Map of original object handles to their stub object couterpart
36 $resRefs = array(); // Map of original resource handles to their stub object couterpart
37 $values = array(); // Map of stub objects' hashes to original values
38 $maxItems = $this->maxItems;
39 $maxString = $this->maxString;
40 $cookie = (object) array(); // Unique object used to detect hard references
41 $a = null; // Array cast for nested structures
42 $stub = null; // Stub capturing the main properties of an original item value
43 // or null if the original value is used directly
44
45 if (!self::$hashMask) {
46 self::$gid = uniqid(mt_rand(), true); // Unique string used to detect the special $GLOBALS variable
47 self::initHashMask();
48 }
49 $gid = self::$gid;
50 $hashMask = self::$hashMask;
51 $hashOffset = self::$hashOffset;
52 $arrayStub = new Stub();
53 $arrayStub->type = Stub::TYPE_ARRAY;
54 $fromObjCast = false;
55
56 for ($i = 0; $i < $len; ++$i) {
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 (array($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_hash($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_hash($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 empty($v):
98 case true === $v:
99 case \is_int($v):
100 case \is_float($v):
101 continue 2;
102
103 case \is_string($v):
104 if (!\preg_match('//u', $v)) {
105 $stub = new Stub();
106 $stub->type = Stub::TYPE_STRING;
107 $stub->class = Stub::STRING_BINARY;
108 if (0 <= $maxString && 0 < $cut = \strlen($v) - $maxString) {
109 $stub->cut = $cut;
110 $stub->value = \substr($v, 0, -$cut);
111 } else {
112 $stub->value = $v;
113 }
114 } elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = \mb_strlen($v, 'UTF-8') - $maxString) {
115 $stub = new Stub();
116 $stub->type = Stub::TYPE_STRING;
117 $stub->class = Stub::STRING_UTF8;
118 $stub->cut = $cut;
119 $stub->value = \mb_substr($v, 0, $maxString, 'UTF-8');
120 } else {
121 continue 2;
122 }
123 $a = null;
124 break;
125
126 case \is_array($v):
127 $stub = $arrayStub;
128 $stub->class = Stub::ARRAY_INDEXED;
129
130 $j = -1;
131 foreach ($v as $gk => $gv) {
132 if ($gk !== ++$j) {
133 $stub->class = Stub::ARRAY_ASSOC;
134 break;
135 }
136 }
137 $a = $v;
138
139 if (Stub::ARRAY_ASSOC === $stub->class) {
140 // Copies of $GLOBALS have very strange behavior,
141 // let's detect them with some black magic
142 $a[$gid] = true;
143
144 // Happens with copies of $GLOBALS
145 if (isset($v[$gid])) {
146 unset($v[$gid]);
147 $a = array();
148 foreach ($v as $gk => &$gv) {
149 $a[$gk] = &$gv;
150 }
151 unset($gv);
152 } else {
153 $a = $v;
154 }
155 } elseif (\PHP_VERSION_ID < 70200) {
156 $indexedArrays[$len] = true;
157 }
158 break;
159
160 case \is_object($v):
161 case $v instanceof \__PHP_Incomplete_Class:
162 if (empty($objRefs[$h = $hashMask ^ \hexdec(\substr(\spl_object_hash($v), $hashOffset, \PHP_INT_SIZE))])) {
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 $h = $hashMask ^ \hexdec(\substr(\spl_object_hash($stub->value), $hashOffset, \PHP_INT_SIZE));
174 $stub->handle = $h;
175 }
176 $stub->value = null;
177 if (0 <= $maxItems && $maxItems <= $pos) {
178 $stub->cut = \count($a);
179 $a = null;
180 }
181 }
182 if (empty($objRefs[$h])) {
183 $objRefs[$h] = $stub;
184 } else {
185 $stub = $objRefs[$h];
186 ++$stub->refCount;
187 $a = null;
188 }
189 break;
190
191 default: // resource
192 if (empty($resRefs[$h = (int) $v])) {
193 $stub = new Stub();
194 $stub->type = Stub::TYPE_RESOURCE;
195 if ('Unknown' === $stub->class = @\get_resource_type($v)) {
196 $stub->class = 'Closed';
197 }
198 $stub->value = $v;
199 $stub->handle = $h;
200 $a = $this->castResource($stub, 0 < $i);
201 $stub->value = null;
202 if (0 <= $maxItems && $maxItems <= $pos) {
203 $stub->cut = \count($a);
204 $a = null;
205 }
206 }
207 if (empty($resRefs[$h])) {
208 $resRefs[$h] = $stub;
209 } else {
210 $stub = $resRefs[$h];
211 ++$stub->refCount;
212 $a = null;
213 }
214 break;
215 }
216
217 if ($a) {
218 if (!$i || 0 > $maxItems) {
219 $queue[$len] = $a;
220 $stub->position = $len++;
221 } elseif ($pos < $maxItems) {
222 if ($maxItems < $pos += \count($a)) {
223 $a = \array_slice($a, 0, $maxItems - $pos);
224 if ($stub->cut >= 0) {
225 $stub->cut += $pos - $maxItems;
226 }
227 }
228 $queue[$len] = $a;
229 $stub->position = $len++;
230 } elseif ($stub->cut >= 0) {
231 $stub->cut += \count($a);
232 $stub->position = 0;
233 }
234 }
235
236 if ($arrayStub === $stub) {
237 if ($arrayStub->cut) {
238 $stub = array($arrayStub->cut, $arrayStub->class => $arrayStub->position);
239 $arrayStub->cut = 0;
240 } elseif (isset(self::$arrayCache[$arrayStub->class][$arrayStub->position])) {
241 $stub = self::$arrayCache[$arrayStub->class][$arrayStub->position];
242 } else {
243 self::$arrayCache[$arrayStub->class][$arrayStub->position] = $stub = array($arrayStub->class => $arrayStub->position);
244 }
245 }
246
247 if ($zvalIsRef) {
248 $refs[$k]->value = $stub;
249 } else {
250 $vals[$k] = $stub;
251 }
252 }
253
254 if ($fromObjCast) {
255 $fromObjCast = false;
256 $refs = $vals;
257 $vals = array();
258 $j = -1;
259 foreach ($queue[$i] as $k => $v) {
260 foreach (array($k => true) as $gk => $gv) {
261 }
262 if ($gk !== $k) {
263 $vals = (object) $vals;
264 $vals->{$k} = $refs[++$j];
265 $vals = (array) $vals;
266 } else {
267 $vals[$k] = $refs[++$j];
268 }
269 }
270 }
271
272 $queue[$i] = $vals;
273 }
274
275 foreach ($values as $h => $v) {
276 $hardRefs[$h] = $v;
277 }
278
279 return $queue;
280 }
281
282 private static function initHashMask()
283 {
284 $obj = (object) array();
285 self::$hashOffset = 16 - PHP_INT_SIZE;
286 self::$hashMask = -1;
287
288 if (defined('HHVM_VERSION')) {
289 self::$hashOffset += 16;
290 } else {
291 // check if we are nested in an output buffering handler to prevent a fatal error with ob_start() below
292 $obFuncs = array('ob_clean', 'ob_end_clean', 'ob_flush', 'ob_end_flush', 'ob_get_contents', 'ob_get_flush');
293 foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
294 if (isset($frame['function'][0]) && !isset($frame['class']) && 'o' === $frame['function'][0] && in_array($frame['function'], $obFuncs)) {
295 $frame['line'] = 0;
296 break;
297 }
298 }
299 if (!empty($frame['line'])) {
300 ob_start();
301 debug_zval_dump($obj);
302 self::$hashMask = (int) substr(ob_get_clean(), 17);
303 }
304 }
305
306 self::$hashMask ^= hexdec(substr(spl_object_hash($obj), self::$hashOffset, PHP_INT_SIZE));
307 }
308 }