3808852dfaafb6e650ab6d3c56aa89529ef2d0f6
[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\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;
22
23 /**
24  * DataCollector.
25  *
26  * Children of this class must store the collected data in the data property.
27  *
28  * @author Fabien Potencier <fabien@symfony.com>
29  * @author Bernhard Schussek <bschussek@symfony.com>
30  */
31 abstract class DataCollector implements DataCollectorInterface, \Serializable
32 {
33     protected $data = array();
34
35     /**
36      * @var ValueExporter
37      */
38     private $valueExporter;
39
40     /**
41      * @var ClonerInterface
42      */
43     private $cloner;
44
45     private static $stubsCache = array();
46
47     public function serialize()
48     {
49         return serialize($this->data);
50     }
51
52     public function unserialize($data)
53     {
54         $this->data = unserialize($data);
55     }
56
57     /**
58      * Converts the variable into a serializable Data instance.
59      *
60      * This array can be displayed in the template using
61      * the VarDumper component.
62      *
63      * @param mixed $var
64      *
65      * @return Data
66      */
67     protected function cloneVar($var)
68     {
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);
76                     },
77                 ));
78             } else {
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;
81             }
82         }
83         if (false === $this->cloner) {
84             if (null === $this->valueExporter) {
85                 $this->valueExporter = new ValueExporter();
86             }
87
88             return $this->valueExporter->exportValue($var);
89         }
90
91         return $this->cloner->cloneVar($this->decorateVar($var));
92     }
93
94     /**
95      * Converts a PHP variable to a string.
96      *
97      * @param mixed $var A PHP variable
98      *
99      * @return string The string representation of the variable
100      *
101      * @deprecated Deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
102      */
103     protected function varToString($var)
104     {
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);
106
107         if (null === $this->valueExporter) {
108             $this->valueExporter = new ValueExporter();
109         }
110
111         return $this->valueExporter->exportValue($var);
112     }
113
114     private function decorateVar($var)
115     {
116         if (is_array($var)) {
117             if (isset($var[0], $var[1]) && is_callable($var)) {
118                 return ClassStub::wrapCallable($var);
119             }
120             foreach ($var as $k => $v) {
121                 if ($v !== $d = $this->decorateVar($v)) {
122                     $var[$k] = $d;
123                 }
124             }
125
126             return $var;
127         }
128         if (is_string($var)) {
129             if (isset(self::$stubsCache[$var])) {
130                 return self::$stubsCache[$var];
131             }
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);
136                 }
137             }
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);
140             }
141         }
142
143         return $var;
144     }
145 }