3664609c5e64824b4c62b4bb79c1d54f1640c0f4
[yaffs-website] / vendor / symfony / var-dumper / Cloner / VarCloner.php
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 minimum depth
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         $minDepth = $this->minDepth;
41         $currentDepth = 0;              // Current tree depth
42         $currentDepthFinalIndex = 0;    // Final $queue index for current tree depth
43         $minimumDepthReached = 0 === $minDepth; // Becomes true when minimum tree depth has been reached
44         $cookie = (object) array();     // Unique object used to detect hard references
45         $a = null;                      // Array cast for nested structures
46         $stub = null;                   // Stub capturing the main properties of an original item value
47                                         // or null if the original value is used directly
48
49         if (!self::$hashMask) {
50             self::$gid = uniqid(mt_rand(), true); // Unique string used to detect the special $GLOBALS variable
51             self::initHashMask();
52         }
53         $gid = self::$gid;
54         $hashMask = self::$hashMask;
55         $hashOffset = self::$hashOffset;
56         $arrayStub = new Stub();
57         $arrayStub->type = Stub::TYPE_ARRAY;
58         $fromObjCast = false;
59
60         for ($i = 0; $i < $len; ++$i) {
61             // Detect when we move on to the next tree depth
62             if ($i > $currentDepthFinalIndex) {
63                 ++$currentDepth;
64                 $currentDepthFinalIndex = $len - 1;
65                 if ($currentDepth >= $minDepth) {
66                     $minimumDepthReached = true;
67                 }
68             }
69
70             $refs = $vals = $queue[$i];
71             if (\PHP_VERSION_ID < 70200 && empty($indexedArrays[$i])) {
72                 // see https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
73                 foreach ($vals as $k => $v) {
74                     if (\is_int($k)) {
75                         continue;
76                     }
77                     foreach (array($k => true) as $gk => $gv) {
78                     }
79                     if ($gk !== $k) {
80                         $fromObjCast = true;
81                         $refs = $vals = \array_values($queue[$i]);
82                         break;
83                     }
84                 }
85             }
86             foreach ($vals as $k => $v) {
87                 // $v is the original value or a stub object in case of hard references
88                 $refs[$k] = $cookie;
89                 if ($zvalIsRef = $vals[$k] === $cookie) {
90                     $vals[$k] = &$stub;         // Break hard references to make $queue completely
91                     unset($stub);               // independent from the original structure
92                     if ($v instanceof Stub && isset($hardRefs[\spl_object_hash($v)])) {
93                         $vals[$k] = $refs[$k] = $v;
94                         if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
95                             ++$v->value->refCount;
96                         }
97                         ++$v->refCount;
98                         continue;
99                     }
100                     $refs[$k] = $vals[$k] = new Stub();
101                     $refs[$k]->value = $v;
102                     $h = \spl_object_hash($refs[$k]);
103                     $hardRefs[$h] = &$refs[$k];
104                     $values[$h] = $v;
105                     $vals[$k]->handle = ++$refsCounter;
106                 }
107                 // Create $stub when the original value $v can not be used directly
108                 // If $v is a nested structure, put that structure in array $a
109                 switch (true) {
110                     case null === $v:
111                     case \is_bool($v):
112                     case \is_int($v):
113                     case \is_float($v):
114                         continue 2;
115
116                     case \is_string($v):
117                         if ('' === $v) {
118                             continue 2;
119                         }
120                         if (!\preg_match('//u', $v)) {
121                             $stub = new Stub();
122                             $stub->type = Stub::TYPE_STRING;
123                             $stub->class = Stub::STRING_BINARY;
124                             if (0 <= $maxString && 0 < $cut = \strlen($v) - $maxString) {
125                                 $stub->cut = $cut;
126                                 $stub->value = \substr($v, 0, -$cut);
127                             } else {
128                                 $stub->value = $v;
129                             }
130                         } elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = \mb_strlen($v, 'UTF-8') - $maxString) {
131                             $stub = new Stub();
132                             $stub->type = Stub::TYPE_STRING;
133                             $stub->class = Stub::STRING_UTF8;
134                             $stub->cut = $cut;
135                             $stub->value = \mb_substr($v, 0, $maxString, 'UTF-8');
136                         } else {
137                             continue 2;
138                         }
139                         $a = null;
140                         break;
141
142                     case \is_array($v):
143                         if (!$v) {
144                             continue 2;
145                         }
146                         $stub = $arrayStub;
147                         $stub->class = Stub::ARRAY_INDEXED;
148
149                         $j = -1;
150                         foreach ($v as $gk => $gv) {
151                             if ($gk !== ++$j) {
152                                 $stub->class = Stub::ARRAY_ASSOC;
153                                 break;
154                             }
155                         }
156                         $a = $v;
157
158                         if (Stub::ARRAY_ASSOC === $stub->class) {
159                             // Copies of $GLOBALS have very strange behavior,
160                             // let's detect them with some black magic
161                             $a[$gid] = true;
162
163                             // Happens with copies of $GLOBALS
164                             if (isset($v[$gid])) {
165                                 unset($v[$gid]);
166                                 $a = array();
167                                 foreach ($v as $gk => &$gv) {
168                                     $a[$gk] = &$gv;
169                                 }
170                                 unset($gv);
171                             } else {
172                                 $a = $v;
173                             }
174                         } elseif (\PHP_VERSION_ID < 70200) {
175                             $indexedArrays[$len] = true;
176                         }
177                         break;
178
179                     case \is_object($v):
180                     case $v instanceof \__PHP_Incomplete_Class:
181                         if (empty($objRefs[$h = $hashMask ^ \hexdec(\substr(\spl_object_hash($v), $hashOffset, \PHP_INT_SIZE))])) {
182                             $stub = new Stub();
183                             $stub->type = Stub::TYPE_OBJECT;
184                             $stub->class = \get_class($v);
185                             $stub->value = $v;
186                             $stub->handle = $h;
187                             $a = $this->castObject($stub, 0 < $i);
188                             if ($v !== $stub->value) {
189                                 if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
190                                     break;
191                                 }
192                                 $h = $hashMask ^ \hexdec(\substr(\spl_object_hash($stub->value), $hashOffset, \PHP_INT_SIZE));
193                                 $stub->handle = $h;
194                             }
195                             $stub->value = null;
196                             if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
197                                 $stub->cut = \count($a);
198                                 $a = null;
199                             }
200                         }
201                         if (empty($objRefs[$h])) {
202                             $objRefs[$h] = $stub;
203                         } else {
204                             $stub = $objRefs[$h];
205                             ++$stub->refCount;
206                             $a = null;
207                         }
208                         break;
209
210                     default: // resource
211                         if (empty($resRefs[$h = (int) $v])) {
212                             $stub = new Stub();
213                             $stub->type = Stub::TYPE_RESOURCE;
214                             if ('Unknown' === $stub->class = @\get_resource_type($v)) {
215                                 $stub->class = 'Closed';
216                             }
217                             $stub->value = $v;
218                             $stub->handle = $h;
219                             $a = $this->castResource($stub, 0 < $i);
220                             $stub->value = null;
221                             if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
222                                 $stub->cut = \count($a);
223                                 $a = null;
224                             }
225                         }
226                         if (empty($resRefs[$h])) {
227                             $resRefs[$h] = $stub;
228                         } else {
229                             $stub = $resRefs[$h];
230                             ++$stub->refCount;
231                             $a = null;
232                         }
233                         break;
234                 }
235
236                 if ($a) {
237                     if (!$minimumDepthReached || 0 > $maxItems) {
238                         $queue[$len] = $a;
239                         $stub->position = $len++;
240                     } elseif ($pos < $maxItems) {
241                         if ($maxItems < $pos += \count($a)) {
242                             $a = \array_slice($a, 0, $maxItems - $pos);
243                             if ($stub->cut >= 0) {
244                                 $stub->cut += $pos - $maxItems;
245                             }
246                         }
247                         $queue[$len] = $a;
248                         $stub->position = $len++;
249                     } elseif ($stub->cut >= 0) {
250                         $stub->cut += \count($a);
251                         $stub->position = 0;
252                     }
253                 }
254
255                 if ($arrayStub === $stub) {
256                     if ($arrayStub->cut) {
257                         $stub = array($arrayStub->cut, $arrayStub->class => $arrayStub->position);
258                         $arrayStub->cut = 0;
259                     } elseif (isset(self::$arrayCache[$arrayStub->class][$arrayStub->position])) {
260                         $stub = self::$arrayCache[$arrayStub->class][$arrayStub->position];
261                     } else {
262                         self::$arrayCache[$arrayStub->class][$arrayStub->position] = $stub = array($arrayStub->class => $arrayStub->position);
263                     }
264                 }
265
266                 if ($zvalIsRef) {
267                     $refs[$k]->value = $stub;
268                 } else {
269                     $vals[$k] = $stub;
270                 }
271             }
272
273             if ($fromObjCast) {
274                 $fromObjCast = false;
275                 $refs = $vals;
276                 $vals = array();
277                 $j = -1;
278                 foreach ($queue[$i] as $k => $v) {
279                     foreach (array($k => true) as $gk => $gv) {
280                     }
281                     if ($gk !== $k) {
282                         $vals = (object) $vals;
283                         $vals->{$k} = $refs[++$j];
284                         $vals = (array) $vals;
285                     } else {
286                         $vals[$k] = $refs[++$j];
287                     }
288                 }
289             }
290
291             $queue[$i] = $vals;
292         }
293
294         foreach ($values as $h => $v) {
295             $hardRefs[$h] = $v;
296         }
297
298         return $queue;
299     }
300
301     private static function initHashMask()
302     {
303         $obj = (object) array();
304         self::$hashOffset = 16 - PHP_INT_SIZE;
305         self::$hashMask = -1;
306
307         if (defined('HHVM_VERSION')) {
308             self::$hashOffset += 16;
309         } else {
310             // check if we are nested in an output buffering handler to prevent a fatal error with ob_start() below
311             $obFuncs = array('ob_clean', 'ob_end_clean', 'ob_flush', 'ob_end_flush', 'ob_get_contents', 'ob_get_flush');
312             foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
313                 if (isset($frame['function'][0]) && !isset($frame['class']) && 'o' === $frame['function'][0] && in_array($frame['function'], $obFuncs)) {
314                     $frame['line'] = 0;
315                     break;
316                 }
317             }
318             if (!empty($frame['line'])) {
319                 ob_start();
320                 debug_zval_dump($obj);
321                 self::$hashMask = (int) substr(ob_get_clean(), 17);
322             }
323         }
324
325         self::$hashMask ^= hexdec(substr(spl_object_hash($obj), self::$hashOffset, PHP_INT_SIZE));
326     }
327 }