vendor/symfony/symfony/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php line 92

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\HttpKernel\DataCollector\Util\ValueExporter;
  12. use Symfony\Component\VarDumper\Caster\CutStub;
  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, \Serializable
  26. {
  27.     /**
  28.      * @var array|Data
  29.      */
  30.     protected $data = [];
  31.     /**
  32.      * @var ValueExporter
  33.      */
  34.     private $valueExporter;
  35.     /**
  36.      * @var ClonerInterface
  37.      */
  38.     private $cloner;
  39.     public function serialize()
  40.     {
  41.         $trace debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT2);
  42.         $isCalledFromOverridingMethod = isset($trace[1]['function'], $trace[1]['object']) && 'serialize' === $trace[1]['function'] && $this === $trace[1]['object'];
  43.         return $isCalledFromOverridingMethod $this->data serialize($this->data);
  44.     }
  45.     public function unserialize($data)
  46.     {
  47.         $this->data = \is_array($data) ? $data unserialize($data);
  48.     }
  49.     /**
  50.      * Converts the variable into a serializable Data instance.
  51.      *
  52.      * This array can be displayed in the template using
  53.      * the VarDumper component.
  54.      *
  55.      * @param mixed $var
  56.      *
  57.      * @return Data
  58.      */
  59.     protected function cloneVar($var)
  60.     {
  61.         if ($var instanceof Data) {
  62.             return $var;
  63.         }
  64.         if (null === $this->cloner) {
  65.             if (class_exists(CutStub::class)) {
  66.                 $this->cloner = new VarCloner();
  67.                 $this->cloner->setMaxItems(-1);
  68.                 $this->cloner->addCasters($this->getCasters());
  69.             } else {
  70.                 @trigger_error(sprintf('Using the %s() method without the VarDumper component is deprecated since Symfony 3.2 and won\'t be supported in 4.0. Install symfony/var-dumper version 3.2 or above.'__METHOD__), \E_USER_DEPRECATED);
  71.                 $this->cloner false;
  72.             }
  73.         }
  74.         if (false === $this->cloner) {
  75.             if (null === $this->valueExporter) {
  76.                 $this->valueExporter = new ValueExporter();
  77.             }
  78.             return $this->valueExporter->exportValue($var);
  79.         }
  80.         return $this->cloner->cloneVar($var);
  81.     }
  82.     /**
  83.      * Converts a PHP variable to a string.
  84.      *
  85.      * @param mixed $var A PHP variable
  86.      *
  87.      * @return string The string representation of the variable
  88.      *
  89.      * @deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
  90.      */
  91.     protected function varToString($var)
  92.     {
  93.         @trigger_error(sprintf('The %s() method is deprecated since Symfony 3.2 and will be removed in 4.0. Use cloneVar() instead.'__METHOD__), \E_USER_DEPRECATED);
  94.         if (null === $this->valueExporter) {
  95.             $this->valueExporter = new ValueExporter();
  96.         }
  97.         return $this->valueExporter->exportValue($var);
  98.     }
  99.     /**
  100.      * @return callable[] The casters to add to the cloner
  101.      */
  102.     protected function getCasters()
  103.     {
  104.         return [
  105.             '*' => function ($v, array $aStub $s$isNested) {
  106.                 if (!$v instanceof Stub) {
  107.                     foreach ($a as $k => $v) {
  108.                         if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
  109.                             $a[$k] = new CutStub($v);
  110.                         }
  111.                     }
  112.                 }
  113.                 return $a;
  114.             },
  115.         ];
  116.     }
  117. }