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

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