4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\DataCollector;
14 use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
15 use Symfony\Component\VarDumper\Caster\CutStub;
16 use Symfony\Component\VarDumper\Cloner\ClonerInterface;
17 use Symfony\Component\VarDumper\Cloner\Data;
18 use Symfony\Component\VarDumper\Cloner\Stub;
19 use Symfony\Component\VarDumper\Cloner\VarCloner;
24 * Children of this class must store the collected data in the data property.
26 * @author Fabien Potencier <fabien@symfony.com>
27 * @author Bernhard Schussek <bschussek@symfony.com>
29 abstract class DataCollector implements DataCollectorInterface, \Serializable
31 protected $data = array();
36 private $valueExporter;
39 * @var ClonerInterface
43 public function serialize()
45 return serialize($this->data);
48 public function unserialize($data)
50 $this->data = unserialize($data);
54 * Converts the variable into a serializable Data instance.
56 * This array can be displayed in the template using
57 * the VarDumper component.
63 protected function cloneVar($var)
65 if ($var instanceof Data) {
68 if (null === $this->cloner) {
69 if (class_exists(CutStub::class)) {
70 $this->cloner = new VarCloner();
71 $this->cloner->setMaxItems(-1);
72 $this->cloner->addCasters($this->getCasters());
74 @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);
75 $this->cloner = false;
78 if (false === $this->cloner) {
79 if (null === $this->valueExporter) {
80 $this->valueExporter = new ValueExporter();
83 return $this->valueExporter->exportValue($var);
86 return $this->cloner->cloneVar($var);
90 * Converts a PHP variable to a string.
92 * @param mixed $var A PHP variable
94 * @return string The string representation of the variable
96 * @deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
98 protected function varToString($var)
100 @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);
102 if (null === $this->valueExporter) {
103 $this->valueExporter = new ValueExporter();
106 return $this->valueExporter->exportValue($var);
110 * @return callable[] The casters to add to the cloner
112 protected function getCasters()
115 '*' => function ($v, array $a, Stub $s, $isNested) {
116 if (!$v instanceof Stub) {
117 foreach ($a as $k => $v) {
118 if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
119 $a[$k] = new CutStub($v);