vendor/symfony/http-kernel/DataCollector/DataCollector.php line 44

  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\HttpKernel\DataCollector;
  11. use Symfony\Component\VarDumper\Caster\CutStub;
  12. use Symfony\Component\VarDumper\Caster\ReflectionCaster;
  13. use Symfony\Component\VarDumper\Cloner\ClonerInterface;
  14. use Symfony\Component\VarDumper\Cloner\Data;
  15. use Symfony\Component\VarDumper\Cloner\Stub;
  16. use Symfony\Component\VarDumper\Cloner\VarCloner;
  17. /**
  18.  * DataCollector.
  19.  *
  20.  * Children of this class must store the collected data in the data property.
  21.  *
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  * @author Bernhard Schussek <bschussek@symfony.com>
  24.  */
  25. abstract class DataCollector implements DataCollectorInterface
  26. {
  27.     /**
  28.      * @var array|Data
  29.      */
  30.     protected $data = [];
  31.     private ClonerInterface $cloner;
  32.     /**
  33.      * Converts the variable into a serializable Data instance.
  34.      *
  35.      * This array can be displayed in the template using
  36.      * the VarDumper component.
  37.      */
  38.     protected function cloneVar(mixed $var): Data
  39.     {
  40.         if ($var instanceof Data) {
  41.             return $var;
  42.         }
  43.         if (!isset($this->cloner)) {
  44.             $this->cloner = new VarCloner();
  45.             $this->cloner->setMaxItems(-1);
  46.             $this->cloner->addCasters($this->getCasters());
  47.         }
  48.         return $this->cloner->cloneVar($var);
  49.     }
  50.     /**
  51.      * @return callable[] The casters to add to the cloner
  52.      */
  53.     protected function getCasters()
  54.     {
  55.         $casters = [
  56.             '*' => function ($v, array $aStub $s$isNested) {
  57.                 if (!$v instanceof Stub) {
  58.                     foreach ($a as $k => $v) {
  59.                         if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
  60.                             $a[$k] = new CutStub($v);
  61.                         }
  62.                     }
  63.                 }
  64.                 return $a;
  65.             },
  66.         ] + ReflectionCaster::UNSET_CLOSURE_FILE_INFO;
  67.         return $casters;
  68.     }
  69.     public function __sleep(): array
  70.     {
  71.         return ['data'];
  72.     }
  73.     public function __wakeup()
  74.     {
  75.     }
  76.     /**
  77.      * @internal to prevent implementing \Serializable
  78.      */
  79.     final protected function serialize()
  80.     {
  81.     }
  82.     /**
  83.      * @internal to prevent implementing \Serializable
  84.      */
  85.     final protected function unserialize(string $data)
  86.     {
  87.     }
  88. }