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

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\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.     /**
  32.      * @var ClonerInterface
  33.      */
  34.     private $cloner;
  35.     /**
  36.      * @deprecated since Symfony 4.3, store all the serialized state in the data property instead
  37.      */
  38.     public function serialize()
  39.     {
  40.         @trigger_error(sprintf('The "%s" method is deprecated since Symfony 4.3, store all the serialized state in the data property instead.'__METHOD__), \E_USER_DEPRECATED);
  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.     /**
  46.      * @deprecated since Symfony 4.3, store all the serialized state in the data property instead
  47.      */
  48.     public function unserialize($data)
  49.     {
  50.         @trigger_error(sprintf('The "%s" method is deprecated since Symfony 4.3, store all the serialized state in the data property instead.'__METHOD__), \E_USER_DEPRECATED);
  51.         $this->data = \is_array($data) ? $data unserialize($data);
  52.     }
  53.     /**
  54.      * Converts the variable into a serializable Data instance.
  55.      *
  56.      * This array can be displayed in the template using
  57.      * the VarDumper component.
  58.      *
  59.      * @param mixed $var
  60.      *
  61.      * @return Data
  62.      */
  63.     protected function cloneVar($var)
  64.     {
  65.         if ($var instanceof Data) {
  66.             return $var;
  67.         }
  68.         if (null === $this->cloner) {
  69.             $this->cloner = new VarCloner();
  70.             $this->cloner->setMaxItems(-1);
  71.             $this->cloner->addCasters($this->getCasters());
  72.         }
  73.         return $this->cloner->cloneVar($var);
  74.     }
  75.     /**
  76.      * @return callable[] The casters to add to the cloner
  77.      */
  78.     protected function getCasters()
  79.     {
  80.         $casters = [
  81.             '*' => function ($v, array $aStub $s$isNested) {
  82.                 if (!$v instanceof Stub) {
  83.                     foreach ($a as $k => $v) {
  84.                         if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
  85.                             $a[$k] = new CutStub($v);
  86.                         }
  87.                     }
  88.                 }
  89.                 return $a;
  90.             },
  91.         ] + ReflectionCaster::UNSET_CLOSURE_FILE_INFO;
  92.         return $casters;
  93.     }
  94.     /**
  95.      * @return array
  96.      */
  97.     public function __sleep()
  98.     {
  99.         if (__CLASS__ !== $c = (new \ReflectionMethod($this'serialize'))->getDeclaringClass()->name) {
  100.             @trigger_error(sprintf('Implementing the "%s::serialize()" method is deprecated since Symfony 4.3, store all the serialized state in the "data" property instead.'$c), \E_USER_DEPRECATED);
  101.             $this->data $this->serialize();
  102.         }
  103.         return ['data'];
  104.     }
  105.     public function __wakeup()
  106.     {
  107.         if (__CLASS__ !== $c = (new \ReflectionMethod($this'unserialize'))->getDeclaringClass()->name) {
  108.             if (\is_object($this->data)) {
  109.                 throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  110.             }
  111.             @trigger_error(sprintf('Implementing the "%s::unserialize()" method is deprecated since Symfony 4.3, store all the serialized state in the "data" property instead.'$c), \E_USER_DEPRECATED);
  112.             $this->unserialize($this->data);
  113.         }
  114.     }
  115. }