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\ClassStub;
16 use Symfony\Component\VarDumper\Caster\LinkStub;
17 use Symfony\Component\VarDumper\Caster\StubCaster;
18 use Symfony\Component\VarDumper\Cloner\ClonerInterface;
19 use Symfony\Component\VarDumper\Cloner\Data;
20 use Symfony\Component\VarDumper\Cloner\Stub;
21 use Symfony\Component\VarDumper\Cloner\VarCloner;
26 * Children of this class must store the collected data in the data property.
28 * @author Fabien Potencier <fabien@symfony.com>
29 * @author Bernhard Schussek <bschussek@symfony.com>
31 abstract class DataCollector implements DataCollectorInterface, \Serializable
33 protected $data = array();
38 private $valueExporter;
41 * @var ClonerInterface
45 private static $stubsCache = array();
47 public function serialize()
49 return serialize($this->data);
52 public function unserialize($data)
54 $this->data = unserialize($data);
58 * Converts the variable into a serializable Data instance.
60 * This array can be displayed in the template using
61 * the VarDumper component.
67 protected function cloneVar($var)
69 if (null === $this->cloner) {
70 if (class_exists(ClassStub::class)) {
71 $this->cloner = new VarCloner();
72 $this->cloner->setMaxItems(250);
73 $this->cloner->addCasters(array(
74 Stub::class => function (Stub $v, array $a, Stub $s, $isNested) {
75 return $isNested ? $a : StubCaster::castStub($v, $a, $s, true);
79 @trigger_error(sprintf('Using the %s() method without the VarDumper component is deprecated since version 3.2 and won\'t be supported in 4.0. Install symfony/var-dumper version 3.2 or above.', __METHOD__), E_USER_DEPRECATED);
80 $this->cloner = false;
83 if (false === $this->cloner) {
84 if (null === $this->valueExporter) {
85 $this->valueExporter = new ValueExporter();
88 return $this->valueExporter->exportValue($var);
91 return $this->cloner->cloneVar($this->decorateVar($var));
95 * Converts a PHP variable to a string.
97 * @param mixed $var A PHP variable
99 * @return string The string representation of the variable
101 * @deprecated Deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
103 protected function varToString($var)
105 @trigger_error(sprintf('The %s() method is deprecated since version 3.2 and will be removed in 4.0. Use cloneVar() instead.', __METHOD__), E_USER_DEPRECATED);
107 if (null === $this->valueExporter) {
108 $this->valueExporter = new ValueExporter();
111 return $this->valueExporter->exportValue($var);
114 private function decorateVar($var)
116 if (is_array($var)) {
117 if (isset($var[0], $var[1]) && is_callable($var)) {
118 return ClassStub::wrapCallable($var);
120 foreach ($var as $k => $v) {
121 if ($v !== $d = $this->decorateVar($v)) {
128 if (is_string($var)) {
129 if (isset(self::$stubsCache[$var])) {
130 return self::$stubsCache[$var];
132 if (false !== strpos($var, '\\')) {
133 $c = (false !== $i = strpos($var, '::')) ? substr($var, 0, $i) : $var;
134 if (class_exists($c, false) || interface_exists($c, false) || trait_exists($c, false)) {
135 return self::$stubsCache[$var] = new ClassStub($var);
138 if (false !== strpos($var, DIRECTORY_SEPARATOR) && false === strpos($var, '://') && false === strpos($var, "\0") && @is_file($var)) {
139 return self::$stubsCache[$var] = new LinkStub($var);