vendor/symfony/var-dumper/Cloner/VarCloner.php line 182

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\VarDumper\Cloner;
  11. /**
  12.  * @author Nicolas Grekas <p@tchwork.com>
  13.  */
  14. class VarCloner extends AbstractCloner
  15. {
  16.     private static $gid;
  17.     private static $arrayCache = [];
  18.     /**
  19.      * {@inheritdoc}
  20.      */
  21.     protected function doClone($var)
  22.     {
  23.         $len 1;                       // Length of $queue
  24.         $pos 0;                       // Number of cloned items past the minimum depth
  25.         $refsCounter 0;               // Hard references counter
  26.         $queue = [[$var]];    // This breadth-first queue is the return value
  27.         $indexedArrays = [];       // Map of queue indexes that hold numerically indexed arrays
  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 === $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.         if (!$gid self::$gid) {
  44.             $gid self::$gid uniqid(mt_rand(), true); // Unique string used to detect the special $GLOBALS variable
  45.         }
  46.         $arrayStub = new Stub();
  47.         $arrayStub->type Stub::TYPE_ARRAY;
  48.         $fromObjCast false;
  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.             $refs $vals $queue[$i];
  59.             if (\PHP_VERSION_ID 70200 && empty($indexedArrays[$i])) {
  60.                 // see https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
  61.                 foreach ($vals as $k => $v) {
  62.                     if (\is_int($k)) {
  63.                         continue;
  64.                     }
  65.                     foreach ([$k => true] as $gk => $gv) {
  66.                     }
  67.                     if ($gk !== $k) {
  68.                         $fromObjCast true;
  69.                         $refs $vals = \array_values($queue[$i]);
  70.                         break;
  71.                     }
  72.                 }
  73.             }
  74.             foreach ($vals as $k => $v) {
  75.                 // $v is the original value or a stub object in case of hard references
  76.                 $refs[$k] = $cookie;
  77.                 if ($zvalIsRef $vals[$k] === $cookie) {
  78.                     $vals[$k] = &$stub;         // Break hard references to make $queue completely
  79.                     unset($stub);               // independent from the original structure
  80.                     if ($v instanceof Stub && isset($hardRefs[\spl_object_id($v)])) {
  81.                         $vals[$k] = $refs[$k] = $v;
  82.                         if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
  83.                             ++$v->value->refCount;
  84.                         }
  85.                         ++$v->refCount;
  86.                         continue;
  87.                     }
  88.                     $refs[$k] = $vals[$k] = new Stub();
  89.                     $refs[$k]->value $v;
  90.                     $h = \spl_object_id($refs[$k]);
  91.                     $hardRefs[$h] = &$refs[$k];
  92.                     $values[$h] = $v;
  93.                     $vals[$k]->handle = ++$refsCounter;
  94.                 }
  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 (true) {
  98.                     case null === $v:
  99.                     case \is_bool($v):
  100.                     case \is_int($v):
  101.                     case \is_float($v):
  102.                         continue 2;
  103.                     case \is_string($v):
  104.                         if ('' === $v) {
  105.                             continue 2;
  106.                         }
  107.                         if (!\preg_match('//u'$v)) {
  108.                             $stub = new Stub();
  109.                             $stub->type Stub::TYPE_STRING;
  110.                             $stub->class Stub::STRING_BINARY;
  111.                             if (<= $maxString && $cut = \strlen($v) - $maxString) {
  112.                                 $stub->cut $cut;
  113.                                 $stub->value = \substr($v0, -$cut);
  114.                             } else {
  115.                                 $stub->value $v;
  116.                             }
  117.                         } elseif (<= $maxString && isset($v[+ ($maxString >> 2)]) && $cut = \mb_strlen($v'UTF-8') - $maxString) {
  118.                             $stub = new Stub();
  119.                             $stub->type Stub::TYPE_STRING;
  120.                             $stub->class Stub::STRING_UTF8;
  121.                             $stub->cut $cut;
  122.                             $stub->value = \mb_substr($v0$maxString'UTF-8');
  123.                         } else {
  124.                             continue 2;
  125.                         }
  126.                         $a null;
  127.                         break;
  128.                     case \is_array($v):
  129.                         if (!$v) {
  130.                             continue 2;
  131.                         }
  132.                         $stub $arrayStub;
  133.                         $stub->class Stub::ARRAY_INDEXED;
  134.                         $j = -1;
  135.                         foreach ($v as $gk => $gv) {
  136.                             if ($gk !== ++$j) {
  137.                                 $stub->class Stub::ARRAY_ASSOC;
  138.                                 break;
  139.                             }
  140.                         }
  141.                         $a $v;
  142.                         if (Stub::ARRAY_ASSOC === $stub->class) {
  143.                             // Copies of $GLOBALS have very strange behavior,
  144.                             // let's detect them with some black magic
  145.                             $a[$gid] = true;
  146.                             // Happens with copies of $GLOBALS
  147.                             if (isset($v[$gid])) {
  148.                                 unset($v[$gid]);
  149.                                 $a = [];
  150.                                 foreach ($v as $gk => &$gv) {
  151.                                     $a[$gk] = &$gv;
  152.                                 }
  153.                                 unset($gv);
  154.                             } else {
  155.                                 $a $v;
  156.                             }
  157.                         } elseif (\PHP_VERSION_ID 70200) {
  158.                             $indexedArrays[$len] = true;
  159.                         }
  160.                         break;
  161.                     case \is_object($v):
  162.                     case $v instanceof \__PHP_Incomplete_Class:
  163.                         if (empty($objRefs[$h = \spl_object_id($v)])) {
  164.                             $stub = new Stub();
  165.                             $stub->type Stub::TYPE_OBJECT;
  166.                             $stub->class = \get_class($v);
  167.                             $stub->value $v;
  168.                             $stub->handle $h;
  169.                             $a $this->castObject($stub$i);
  170.                             if ($v !== $stub->value) {
  171.                                 if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
  172.                                     break;
  173.                                 }
  174.                                 $stub->handle $h = \spl_object_id($stub->value);
  175.                             }
  176.                             $stub->value null;
  177.                             if (<= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  178.                                 $stub->cut = \count($a);
  179.                                 $a null;
  180.                             }
  181.                         }
  182.                         if (empty($objRefs[$h])) {
  183.                             $objRefs[$h] = $stub;
  184.                             $objects[] = $v;
  185.                         } else {
  186.                             $stub $objRefs[$h];
  187.                             ++$stub->refCount;
  188.                             $a null;
  189.                         }
  190.                         break;
  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$i);
  201.                             $stub->value null;
  202.                             if (<= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  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.                 if ($a) {
  217.                     if (!$minimumDepthReached || $maxItems) {
  218.                         $queue[$len] = $a;
  219.                         $stub->position $len++;
  220.                     } elseif ($pos $maxItems) {
  221.                         if ($maxItems $pos += \count($a)) {
  222.                             $a = \array_slice($a0$maxItems $pos);
  223.                             if ($stub->cut >= 0) {
  224.                                 $stub->cut += $pos $maxItems;
  225.                             }
  226.                         }
  227.                         $queue[$len] = $a;
  228.                         $stub->position $len++;
  229.                     } elseif ($stub->cut >= 0) {
  230.                         $stub->cut += \count($a);
  231.                         $stub->position 0;
  232.                     }
  233.                 }
  234.                 if ($arrayStub === $stub) {
  235.                     if ($arrayStub->cut) {
  236.                         $stub = [$arrayStub->cut$arrayStub->class => $arrayStub->position];
  237.                         $arrayStub->cut 0;
  238.                     } elseif (isset(self::$arrayCache[$arrayStub->class][$arrayStub->position])) {
  239.                         $stub self::$arrayCache[$arrayStub->class][$arrayStub->position];
  240.                     } else {
  241.                         self::$arrayCache[$arrayStub->class][$arrayStub->position] = $stub = [$arrayStub->class => $arrayStub->position];
  242.                     }
  243.                 }
  244.                 if ($zvalIsRef) {
  245.                     $refs[$k]->value $stub;
  246.                 } else {
  247.                     $vals[$k] = $stub;
  248.                 }
  249.             }
  250.             if ($fromObjCast) {
  251.                 $fromObjCast false;
  252.                 $refs $vals;
  253.                 $vals = [];
  254.                 $j = -1;
  255.                 foreach ($queue[$i] as $k => $v) {
  256.                     foreach ([$k => true] as $gk => $gv) {
  257.                     }
  258.                     if ($gk !== $k) {
  259.                         $vals = (object) $vals;
  260.                         $vals->{$k} = $refs[++$j];
  261.                         $vals = (array) $vals;
  262.                     } else {
  263.                         $vals[$k] = $refs[++$j];
  264.                     }
  265.                 }
  266.             }
  267.             $queue[$i] = $vals;
  268.         }
  269.         foreach ($values as $h => $v) {
  270.             $hardRefs[$h] = $v;
  271.         }
  272.         return $queue;
  273.     }
  274. }