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
19 private static $hashMask = 0;
20 private static $hashOffset = 0;
25 protected function doClone($var)
27 $useExt = $this->useExt;
28 $len = 1; // Length of $queue
29 $pos = 0; // Number of cloned items past the first level
30 $refsCounter = 0; // Hard references counter
31 $queue = array(array($var)); // This breadth-first queue is the return value
32 $arrayRefs = array(); // Map of queue indexes to stub array objects
33 $hardRefs = array(); // Map of original zval hashes to stub objects
34 $objRefs = array(); // Map of original object handles to their stub object couterpart
35 $resRefs = array(); // Map of original resource handles to their stub object couterpart
36 $values = array(); // Map of stub objects' hashes to original values
37 $maxItems = $this->maxItems;
38 $maxString = $this->maxString;
39 $cookie = (object) array(); // Unique object used to detect hard references
40 $gid = uniqid(mt_rand(), true); // Unique string used to detect the special $GLOBALS variable
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 $zval = array( // Main properties of the current value
48 'array_count' => null,
49 'object_class' => null,
50 'object_handle' => null,
51 'resource_type' => null,
53 if (!self::$hashMask) {
56 $hashMask = self::$hashMask;
57 $hashOffset = self::$hashOffset;
59 for ($i = 0; $i < $len; ++$i) {
60 $indexed = true; // Whether the currently iterated array is numerically indexed or not
61 $j = -1; // Position in the currently iterated array
62 $fromObjCast = array_keys($queue[$i]);
63 $fromObjCast = array_keys(array_flip($fromObjCast)) !== $fromObjCast;
64 $refs = $vals = $fromObjCast ? array_values($queue[$i]) : $queue[$i];
65 foreach ($queue[$i] as $k => $v) {
66 // $k is the original key
67 // $v is the original value or a stub object in case of hard references
75 $zval = symfony_zval_info($k, $refs);
78 if ($zval['zval_isref'] = $vals[$k] === $cookie) {
79 $zval['zval_hash'] = $v instanceof Stub ? spl_object_hash($v) : null;
81 $zval['type'] = gettype($v);
83 if ($zval['zval_isref']) {
84 $vals[$k] = &$stub; // Break hard references to make $queue completely
85 unset($stub); // independent from the original structure
86 if (isset($hardRefs[$zval['zval_hash']])) {
87 $vals[$k] = $useExt ? ($v = $hardRefs[$zval['zval_hash']]) : ($refs[$k] = $v);
88 if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
89 ++$v->value->refCount;
95 // Create $stub when the original value $v can not be used directly
96 // If $v is a nested structure, put that structure in array $a
97 switch ($zval['type']) {
99 if (isset($v[0]) && !preg_match('//u', $v)) {
101 $stub->type = Stub::TYPE_STRING;
102 $stub->class = Stub::STRING_BINARY;
103 if (0 <= $maxString && 0 < $cut = strlen($v) - $maxString) {
105 $stub->value = substr($v, 0, -$cut);
109 } elseif (0 <= $maxString && isset($v[1 + ($maxString >> 2)]) && 0 < $cut = mb_strlen($v, 'UTF-8') - $maxString) {
111 $stub->type = Stub::TYPE_STRING;
112 $stub->class = Stub::STRING_UTF8;
114 $stub->value = mb_substr($v, 0, $maxString, 'UTF-8');
123 $stub = $arrayRefs[$len] = new Stub();
124 $stub->type = Stub::TYPE_ARRAY;
125 $stub->class = Stub::ARRAY_ASSOC;
127 // Copies of $GLOBALS have very strange behavior,
128 // let's detect them with some black magic
132 // Happens with copies of $GLOBALS
133 if (isset($v[$gid])) {
136 foreach ($v as $gk => &$gv) {
143 $stub->value = $zval['array_count'] ?: count($a);
148 if (empty($objRefs[$h = $zval['object_handle'] ?: ($hashMask ^ hexdec(substr(spl_object_hash($v), $hashOffset, PHP_INT_SIZE)))])) {
150 $stub->type = Stub::TYPE_OBJECT;
151 $stub->class = $zval['object_class'] ?: get_class($v);
154 $a = $this->castObject($stub, 0 < $i);
155 if ($v !== $stub->value) {
156 if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
160 $zval['type'] = $stub->value;
161 $zval = symfony_zval_info('type', $zval);
162 $h = $zval['object_handle'];
164 $h = $hashMask ^ hexdec(substr(spl_object_hash($stub->value), $hashOffset, PHP_INT_SIZE));
169 if (0 <= $maxItems && $maxItems <= $pos) {
170 $stub->cut = count($a);
174 if (empty($objRefs[$h])) {
175 $objRefs[$h] = $stub;
177 $stub = $objRefs[$h];
185 case 'resource (closed)':
186 if (empty($resRefs[$h = (int) $v])) {
188 $stub->type = Stub::TYPE_RESOURCE;
189 if ('Unknown' === $stub->class = $zval['resource_type'] ?: @get_resource_type($v)) {
190 $stub->class = 'Closed';
194 $a = $this->castResource($stub, 0 < $i);
196 if (0 <= $maxItems && $maxItems <= $pos) {
197 $stub->cut = count($a);
201 if (empty($resRefs[$h])) {
202 $resRefs[$h] = $stub;
204 $stub = $resRefs[$h];
212 if ($zval['zval_isref']) {
214 $vals[$k] = $hardRefs[$zval['zval_hash']] = $v = new Stub();
217 $refs[$k] = new Stub();
218 $refs[$k]->value = $stub;
219 $h = spl_object_hash($refs[$k]);
220 $vals[$k] = $hardRefs[$h] = &$refs[$k];
223 $vals[$k]->handle = ++$refsCounter;
229 if ($i && 0 <= $maxItems) {
231 if ($pos < $maxItems) {
232 if ($maxItems < $pos += $k) {
233 $a = array_slice($a, 0, $maxItems - $pos);
234 if ($stub->cut >= 0) {
235 $stub->cut += $pos - $maxItems;
239 if ($stub->cut >= 0) {
243 unset($arrayRefs[$len]);
248 $stub->position = $len++;
251 } elseif ($zval['zval_isref']) {
253 $vals[$k] = $hardRefs[$zval['zval_hash']] = new Stub();
254 $vals[$k]->value = $v;
256 $refs[$k] = $vals[$k] = new Stub();
257 $refs[$k]->value = $v;
258 $h = spl_object_hash($refs[$k]);
259 $hardRefs[$h] = &$refs[$k];
262 $vals[$k]->handle = ++$refsCounter;
270 foreach ($queue[$i] as $k => $v) {
271 foreach (array($k => $v) as $a => $v) {
274 $vals = (object) $vals;
275 $vals->{$k} = $refs[++$j];
276 $vals = (array) $vals;
278 $vals[$k] = $refs[++$j];
285 if (isset($arrayRefs[$i])) {
287 $arrayRefs[$i]->class = Stub::ARRAY_INDEXED;
289 unset($arrayRefs[$i]);
293 foreach ($values as $h => $v) {
300 private static function initHashMask()
302 $obj = (object) array();
303 self::$hashOffset = 16 - PHP_INT_SIZE;
304 self::$hashMask = -1;
306 if (defined('HHVM_VERSION')) {
307 self::$hashOffset += 16;
309 // check if we are nested in an output buffering handler to prevent a fatal error with ob_start() below
310 $obFuncs = array('ob_clean', 'ob_end_clean', 'ob_flush', 'ob_end_flush', 'ob_get_contents', 'ob_get_flush');
311 foreach (debug_backtrace(\PHP_VERSION_ID >= 50400 ? DEBUG_BACKTRACE_IGNORE_ARGS : false) as $frame) {
312 if (isset($frame['function'][0]) && !isset($frame['class']) && 'o' === $frame['function'][0] && in_array($frame['function'], $obFuncs)) {
317 if (!empty($frame['line'])) {
319 debug_zval_dump($obj);
320 self::$hashMask = (int) substr(ob_get_clean(), 17);
324 self::$hashMask ^= hexdec(substr(spl_object_hash($obj), self::$hashOffset, PHP_INT_SIZE));