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

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