4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\VarDumper\Cloner;
15 * @author Nicolas Grekas <p@tchwork.com>
17 class VarCloner extends AbstractCloner
20 private static $hashMask = 0;
21 private static $hashOffset = 0;
22 private static $arrayCache = array();
27 protected function doClone($var)
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
49 if (!self::$hashMask) {
50 self::$gid = uniqid(mt_rand(), true); // Unique string used to detect the special $GLOBALS variable
54 $hashMask = self::$hashMask;
55 $hashOffset = self::$hashOffset;
56 $arrayStub = new Stub();
57 $arrayStub->type = Stub::TYPE_ARRAY;
60 for ($i = 0; $i < $len; ++$i) {
61 // Detect when we move on to the next tree depth
62 if ($i > $currentDepthFinalIndex) {
64 $currentDepthFinalIndex = $len - 1;
65 if ($currentDepth >= $minDepth) {
66 $minimumDepthReached = true;
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) {
77 foreach (array($k => true) as $gk => $gv) {
81 $refs = $vals = \array_values($queue[$i]);
86 foreach ($vals as $k => $v) {
87 // $v is the original value or a stub object in case of hard references
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;
100 $refs[$k] = $vals[$k] = new Stub();
101 $refs[$k]->value = $v;
102 $h = \spl_object_hash($refs[$k]);
103 $hardRefs[$h] = &$refs[$k];
105 $vals[$k]->handle = ++$refsCounter;
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
120 if (!\preg_match('//u', $v)) {
122 $stub->type = Stub::TYPE_STRING;
123 $stub->class = Stub::STRING_BINARY;
124 if (0 <= $maxString && 0 < $cut = \strlen($v) - $maxString) {
126 $stub->value = \substr($v, 0, -$cut);
130 } elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = \mb_strlen($v, 'UTF-8') - $maxString) {
132 $stub->type = Stub::TYPE_STRING;
133 $stub->class = Stub::STRING_UTF8;
135 $stub->value = \mb_substr($v, 0, $maxString, 'UTF-8');
147 $stub->class = Stub::ARRAY_INDEXED;
150 foreach ($v as $gk => $gv) {
152 $stub->class = Stub::ARRAY_ASSOC;
158 if (Stub::ARRAY_ASSOC === $stub->class) {
159 // Copies of $GLOBALS have very strange behavior,
160 // let's detect them with some black magic
163 // Happens with copies of $GLOBALS
164 if (isset($v[$gid])) {
167 foreach ($v as $gk => &$gv) {
174 } elseif (\PHP_VERSION_ID < 70200) {
175 $indexedArrays[$len] = true;
180 case $v instanceof \__PHP_Incomplete_Class:
181 if (empty($objRefs[$h = $hashMask ^ \hexdec(\substr(\spl_object_hash($v), $hashOffset, \PHP_INT_SIZE))])) {
183 $stub->type = Stub::TYPE_OBJECT;
184 $stub->class = \get_class($v);
187 $a = $this->castObject($stub, 0 < $i);
188 if ($v !== $stub->value) {
189 if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
192 $h = $hashMask ^ \hexdec(\substr(\spl_object_hash($stub->value), $hashOffset, \PHP_INT_SIZE));
196 if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
197 $stub->cut = \count($a);
201 if (empty($objRefs[$h])) {
202 $objRefs[$h] = $stub;
204 $stub = $objRefs[$h];
211 if (empty($resRefs[$h = (int) $v])) {
213 $stub->type = Stub::TYPE_RESOURCE;
214 if ('Unknown' === $stub->class = @\get_resource_type($v)) {
215 $stub->class = 'Closed';
219 $a = $this->castResource($stub, 0 < $i);
221 if (0 <= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
222 $stub->cut = \count($a);
226 if (empty($resRefs[$h])) {
227 $resRefs[$h] = $stub;
229 $stub = $resRefs[$h];
237 if (!$minimumDepthReached || 0 > $maxItems) {
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;
248 $stub->position = $len++;
249 } elseif ($stub->cut >= 0) {
250 $stub->cut += \count($a);
255 if ($arrayStub === $stub) {
256 if ($arrayStub->cut) {
257 $stub = array($arrayStub->cut, $arrayStub->class => $arrayStub->position);
259 } elseif (isset(self::$arrayCache[$arrayStub->class][$arrayStub->position])) {
260 $stub = self::$arrayCache[$arrayStub->class][$arrayStub->position];
262 self::$arrayCache[$arrayStub->class][$arrayStub->position] = $stub = array($arrayStub->class => $arrayStub->position);
267 $refs[$k]->value = $stub;
274 $fromObjCast = false;
278 foreach ($queue[$i] as $k => $v) {
279 foreach (array($k => true) as $gk => $gv) {
282 $vals = (object) $vals;
283 $vals->{$k} = $refs[++$j];
284 $vals = (array) $vals;
286 $vals[$k] = $refs[++$j];
294 foreach ($values as $h => $v) {
301 private static function initHashMask()
303 $obj = (object) array();
304 self::$hashOffset = 16 - PHP_INT_SIZE;
305 self::$hashMask = -1;
307 if (\defined('HHVM_VERSION')) {
308 self::$hashOffset += 16;
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)) {
318 if (!empty($frame['line'])) {
320 debug_zval_dump($obj);
321 self::$hashMask = (int) substr(ob_get_clean(), 17);
325 self::$hashMask ^= hexdec(substr(spl_object_hash($obj), self::$hashOffset, PHP_INT_SIZE));