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

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.         $hardRefs = [];            // Map of original zval ids to stub objects
  28.         $objRefs = [];             // Map of original object handles to their stub object counterpart
  29.         $objects = [];             // Keep a ref to objects to ensure their handle cannot be reused while cloning
  30.         $resRefs = [];             // Map of original resource handles to their stub object counterpart
  31.         $values = [];              // Map of stub objects' ids to original values
  32.         $maxItems $this->maxItems;
  33.         $maxString $this->maxString;
  34.         $minDepth $this->minDepth;
  35.         $currentDepth 0;              // Current tree depth
  36.         $currentDepthFinalIndex 0;    // Final $queue index for current tree depth
  37.         $minimumDepthReached === $minDepth// Becomes true when minimum tree depth has been reached
  38.         $cookie = (object) [];          // Unique object used to detect hard references
  39.         $a null;                      // Array cast for nested structures
  40.         $stub null;                   // Stub capturing the main properties of an original item value
  41.                                         // or null if the original value is used directly
  42.         if (!$gid self::$gid) {
  43.             $gid self::$gid md5(random_bytes(6)); // Unique string used to detect the special $GLOBALS variable
  44.         }
  45.         $arrayStub = new Stub();
  46.         $arrayStub->type Stub::TYPE_ARRAY;
  47.         $fromObjCast false;
  48.         for ($i 0$i $len; ++$i) {
  49.             // Detect when we move on to the next tree depth
  50.             if ($i $currentDepthFinalIndex) {
  51.                 ++$currentDepth;
  52.                 $currentDepthFinalIndex $len 1;
  53.                 if ($currentDepth >= $minDepth) {
  54.                     $minimumDepthReached true;
  55.                 }
  56.             }
  57.             $refs $vals $queue[$i];
  58.             foreach ($vals as $k => $v) {
  59.                 // $v is the original value or a stub object in case of hard references
  60.                 if (\PHP_VERSION_ID >= 70400) {
  61.                     $zvalIsRef null !== \ReflectionReference::fromArrayElement($vals$k);
  62.                 } else {
  63.                     $refs[$k] = $cookie;
  64.                     $zvalIsRef $vals[$k] === $cookie;
  65.                 }
  66.                 if ($zvalIsRef) {
  67.                     $vals[$k] = &$stub;         // Break hard references to make $queue completely
  68.                     unset($stub);               // independent from the original structure
  69.                     if ($v instanceof Stub && isset($hardRefs[spl_object_id($v)])) {
  70.                         $vals[$k] = $refs[$k] = $v;
  71.                         if ($v->value instanceof Stub && (Stub::TYPE_OBJECT === $v->value->type || Stub::TYPE_RESOURCE === $v->value->type)) {
  72.                             ++$v->value->refCount;
  73.                         }
  74.                         ++$v->refCount;
  75.                         continue;
  76.                     }
  77.                     $refs[$k] = $vals[$k] = new Stub();
  78.                     $refs[$k]->value $v;
  79.                     $h spl_object_id($refs[$k]);
  80.                     $hardRefs[$h] = &$refs[$k];
  81.                     $values[$h] = $v;
  82.                     $vals[$k]->handle = ++$refsCounter;
  83.                 }
  84.                 // Create $stub when the original value $v can not be used directly
  85.                 // If $v is a nested structure, put that structure in array $a
  86.                 switch (true) {
  87.                     case null === $v:
  88.                     case \is_bool($v):
  89.                     case \is_int($v):
  90.                     case \is_float($v):
  91.                         continue 2;
  92.                     case \is_string($v):
  93.                         if ('' === $v) {
  94.                             continue 2;
  95.                         }
  96.                         if (!preg_match('//u'$v)) {
  97.                             $stub = new Stub();
  98.                             $stub->type Stub::TYPE_STRING;
  99.                             $stub->class Stub::STRING_BINARY;
  100.                             if (<= $maxString && $cut = \strlen($v) - $maxString) {
  101.                                 $stub->cut $cut;
  102.                                 $stub->value substr($v0, -$cut);
  103.                             } else {
  104.                                 $stub->value $v;
  105.                             }
  106.                         } elseif (<= $maxString && isset($v[+ ($maxString >> 2)]) && $cut mb_strlen($v'UTF-8') - $maxString) {
  107.                             $stub = new Stub();
  108.                             $stub->type Stub::TYPE_STRING;
  109.                             $stub->class Stub::STRING_UTF8;
  110.                             $stub->cut $cut;
  111.                             $stub->value mb_substr($v0$maxString'UTF-8');
  112.                         } else {
  113.                             continue 2;
  114.                         }
  115.                         $a null;
  116.                         break;
  117.                     case \is_array($v):
  118.                         if (!$v) {
  119.                             continue 2;
  120.                         }
  121.                         $stub $arrayStub;
  122.                         $stub->class Stub::ARRAY_INDEXED;
  123.                         $j = -1;
  124.                         foreach ($v as $gk => $gv) {
  125.                             if ($gk !== ++$j) {
  126.                                 $stub->class Stub::ARRAY_ASSOC;
  127.                                 break;
  128.                             }
  129.                         }
  130.                         $a $v;
  131.                         if (Stub::ARRAY_ASSOC === $stub->class) {
  132.                             // Copies of $GLOBALS have very strange behavior,
  133.                             // let's detect them with some black magic
  134.                             $a[$gid] = true;
  135.                             // Happens with copies of $GLOBALS
  136.                             if (isset($v[$gid])) {
  137.                                 unset($v[$gid]);
  138.                                 $a = [];
  139.                                 foreach ($v as $gk => &$gv) {
  140.                                     $a[$gk] = &$gv;
  141.                                 }
  142.                                 unset($gv);
  143.                             } else {
  144.                                 $a $v;
  145.                             }
  146.                         }
  147.                         break;
  148.                     case \is_object($v):
  149.                     case $v instanceof \__PHP_Incomplete_Class:
  150.                         if (empty($objRefs[$h spl_object_id($v)])) {
  151.                             $stub = new Stub();
  152.                             $stub->type Stub::TYPE_OBJECT;
  153.                             $stub->class = \get_class($v);
  154.                             $stub->value $v;
  155.                             $stub->handle $h;
  156.                             $a $this->castObject($stub$i);
  157.                             if ($v !== $stub->value) {
  158.                                 if (Stub::TYPE_OBJECT !== $stub->type || null === $stub->value) {
  159.                                     break;
  160.                                 }
  161.                                 $stub->handle $h spl_object_id($stub->value);
  162.                             }
  163.                             $stub->value null;
  164.                             if (<= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  165.                                 $stub->cut = \count($a);
  166.                                 $a null;
  167.                             }
  168.                         }
  169.                         if (empty($objRefs[$h])) {
  170.                             $objRefs[$h] = $stub;
  171.                             $objects[] = $v;
  172.                         } else {
  173.                             $stub $objRefs[$h];
  174.                             ++$stub->refCount;
  175.                             $a null;
  176.                         }
  177.                         break;
  178.                     default: // resource
  179.                         if (empty($resRefs[$h = (int) $v])) {
  180.                             $stub = new Stub();
  181.                             $stub->type Stub::TYPE_RESOURCE;
  182.                             if ('Unknown' === $stub->class = @get_resource_type($v)) {
  183.                                 $stub->class 'Closed';
  184.                             }
  185.                             $stub->value $v;
  186.                             $stub->handle $h;
  187.                             $a $this->castResource($stub$i);
  188.                             $stub->value null;
  189.                             if (<= $maxItems && $maxItems <= $pos && $minimumDepthReached) {
  190.                                 $stub->cut = \count($a);
  191.                                 $a null;
  192.                             }
  193.                         }
  194.                         if (empty($resRefs[$h])) {
  195.                             $resRefs[$h] = $stub;
  196.                         } else {
  197.                             $stub $resRefs[$h];
  198.                             ++$stub->refCount;
  199.                             $a null;
  200.                         }
  201.                         break;
  202.                 }
  203.                 if ($a) {
  204.                     if (!$minimumDepthReached || $maxItems) {
  205.                         $queue[$len] = $a;
  206.                         $stub->position $len++;
  207.                     } elseif ($pos $maxItems) {
  208.                         if ($maxItems $pos += \count($a)) {
  209.                             $a = \array_slice($a0$maxItems $postrue);
  210.                             if ($stub->cut >= 0) {
  211.                                 $stub->cut += $pos $maxItems;
  212.                             }
  213.                         }
  214.                         $queue[$len] = $a;
  215.                         $stub->position $len++;
  216.                     } elseif ($stub->cut >= 0) {
  217.                         $stub->cut += \count($a);
  218.                         $stub->position 0;
  219.                     }
  220.                 }
  221.                 if ($arrayStub === $stub) {
  222.                     if ($arrayStub->cut) {
  223.                         $stub = [$arrayStub->cut$arrayStub->class => $arrayStub->position];
  224.                         $arrayStub->cut 0;
  225.                     } elseif (isset(self::$arrayCache[$arrayStub->class][$arrayStub->position])) {
  226.                         $stub self::$arrayCache[$arrayStub->class][$arrayStub->position];
  227.                     } else {
  228.                         self::$arrayCache[$arrayStub->class][$arrayStub->position] = $stub = [$arrayStub->class => $arrayStub->position];
  229.                     }
  230.                 }
  231.                 if ($zvalIsRef) {
  232.                     $refs[$k]->value $stub;
  233.                 } else {
  234.                     $vals[$k] = $stub;
  235.                 }
  236.             }
  237.             if ($fromObjCast) {
  238.                 $fromObjCast false;
  239.                 $refs $vals;
  240.                 $vals = [];
  241.                 $j = -1;
  242.                 foreach ($queue[$i] as $k => $v) {
  243.                     foreach ([$k => true] as $gk => $gv) {
  244.                     }
  245.                     if ($gk !== $k) {
  246.                         $vals = (object) $vals;
  247.                         $vals->{$k} = $refs[++$j];
  248.                         $vals = (array) $vals;
  249.                     } else {
  250.                         $vals[$k] = $refs[++$j];
  251.                     }
  252.                 }
  253.             }
  254.             $queue[$i] = $vals;
  255.         }
  256.         foreach ($values as $h => $v) {
  257.             $hardRefs[$h] = $v;
  258.         }
  259.         return $queue;
  260.     }
  261. }