6d1a822e47a9008c1394c96cac6383b06a77ed8f
[yaffs-website] / vendor / symfony / http-kernel / DataCollector / DataCollector.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\DataCollector;
13
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;
20
21 /**
22  * DataCollector.
23  *
24  * Children of this class must store the collected data in the data property.
25  *
26  * @author Fabien Potencier <fabien@symfony.com>
27  * @author Bernhard Schussek <bschussek@symfony.com>
28  */
29 abstract class DataCollector implements DataCollectorInterface, \Serializable
30 {
31     protected $data = array();
32
33     /**
34      * @var ValueExporter
35      */
36     private $valueExporter;
37
38     /**
39      * @var ClonerInterface
40      */
41     private $cloner;
42
43     public function serialize()
44     {
45         return serialize($this->data);
46     }
47
48     public function unserialize($data)
49     {
50         $this->data = unserialize($data);
51     }
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             if (class_exists(CutStub::class)) {
70                 $this->cloner = new VarCloner();
71                 $this->cloner->setMaxItems(-1);
72                 $this->cloner->addCasters($this->getCasters());
73             } else {
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;
76             }
77         }
78         if (false === $this->cloner) {
79             if (null === $this->valueExporter) {
80                 $this->valueExporter = new ValueExporter();
81             }
82
83             return $this->valueExporter->exportValue($var);
84         }
85
86         return $this->cloner->cloneVar($var);
87     }
88
89     /**
90      * Converts a PHP variable to a string.
91      *
92      * @param mixed $var A PHP variable
93      *
94      * @return string The string representation of the variable
95      *
96      * @deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
97      */
98     protected function varToString($var)
99     {
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);
101
102         if (null === $this->valueExporter) {
103             $this->valueExporter = new ValueExporter();
104         }
105
106         return $this->valueExporter->exportValue($var);
107     }
108
109     /**
110      * @return callable[] The casters to add to the cloner
111      */
112     protected function getCasters()
113     {
114         return array(
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);
120                         }
121                     }
122                 }
123
124                 return $a;
125             },
126         );
127     }
128 }