Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpKernel\DataCollector;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
|
Chris@0
|
15 use Symfony\Component\VarDumper\Caster\ClassStub;
|
Chris@0
|
16 use Symfony\Component\VarDumper\Caster\LinkStub;
|
Chris@0
|
17 use Symfony\Component\VarDumper\Caster\StubCaster;
|
Chris@0
|
18 use Symfony\Component\VarDumper\Cloner\ClonerInterface;
|
Chris@0
|
19 use Symfony\Component\VarDumper\Cloner\Data;
|
Chris@0
|
20 use Symfony\Component\VarDumper\Cloner\Stub;
|
Chris@0
|
21 use Symfony\Component\VarDumper\Cloner\VarCloner;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * DataCollector.
|
Chris@0
|
25 *
|
Chris@0
|
26 * Children of this class must store the collected data in the data property.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
29 * @author Bernhard Schussek <bschussek@symfony.com>
|
Chris@0
|
30 */
|
Chris@0
|
31 abstract class DataCollector implements DataCollectorInterface, \Serializable
|
Chris@0
|
32 {
|
Chris@0
|
33 protected $data = array();
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * @var ValueExporter
|
Chris@0
|
37 */
|
Chris@0
|
38 private $valueExporter;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * @var ClonerInterface
|
Chris@0
|
42 */
|
Chris@0
|
43 private $cloner;
|
Chris@0
|
44
|
Chris@0
|
45 private static $stubsCache = array();
|
Chris@0
|
46
|
Chris@0
|
47 public function serialize()
|
Chris@0
|
48 {
|
Chris@0
|
49 return serialize($this->data);
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 public function unserialize($data)
|
Chris@0
|
53 {
|
Chris@0
|
54 $this->data = unserialize($data);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * Converts the variable into a serializable Data instance.
|
Chris@0
|
59 *
|
Chris@0
|
60 * This array can be displayed in the template using
|
Chris@0
|
61 * the VarDumper component.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @param mixed $var
|
Chris@0
|
64 *
|
Chris@0
|
65 * @return Data
|
Chris@0
|
66 */
|
Chris@0
|
67 protected function cloneVar($var)
|
Chris@0
|
68 {
|
Chris@0
|
69 if (null === $this->cloner) {
|
Chris@0
|
70 if (class_exists(ClassStub::class)) {
|
Chris@0
|
71 $this->cloner = new VarCloner();
|
Chris@0
|
72 $this->cloner->setMaxItems(250);
|
Chris@0
|
73 $this->cloner->addCasters(array(
|
Chris@0
|
74 Stub::class => function (Stub $v, array $a, Stub $s, $isNested) {
|
Chris@0
|
75 return $isNested ? $a : StubCaster::castStub($v, $a, $s, true);
|
Chris@0
|
76 },
|
Chris@0
|
77 ));
|
Chris@0
|
78 } else {
|
Chris@0
|
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);
|
Chris@0
|
80 $this->cloner = false;
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|
Chris@0
|
83 if (false === $this->cloner) {
|
Chris@0
|
84 if (null === $this->valueExporter) {
|
Chris@0
|
85 $this->valueExporter = new ValueExporter();
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 return $this->valueExporter->exportValue($var);
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 return $this->cloner->cloneVar($this->decorateVar($var));
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Converts a PHP variable to a string.
|
Chris@0
|
96 *
|
Chris@0
|
97 * @param mixed $var A PHP variable
|
Chris@0
|
98 *
|
Chris@0
|
99 * @return string The string representation of the variable
|
Chris@0
|
100 *
|
Chris@0
|
101 * @deprecated Deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
|
Chris@0
|
102 */
|
Chris@0
|
103 protected function varToString($var)
|
Chris@0
|
104 {
|
Chris@0
|
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);
|
Chris@0
|
106
|
Chris@0
|
107 if (null === $this->valueExporter) {
|
Chris@0
|
108 $this->valueExporter = new ValueExporter();
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 return $this->valueExporter->exportValue($var);
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 private function decorateVar($var)
|
Chris@0
|
115 {
|
Chris@0
|
116 if (is_array($var)) {
|
Chris@0
|
117 if (isset($var[0], $var[1]) && is_callable($var)) {
|
Chris@0
|
118 return ClassStub::wrapCallable($var);
|
Chris@0
|
119 }
|
Chris@0
|
120 foreach ($var as $k => $v) {
|
Chris@0
|
121 if ($v !== $d = $this->decorateVar($v)) {
|
Chris@0
|
122 $var[$k] = $d;
|
Chris@0
|
123 }
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 return $var;
|
Chris@0
|
127 }
|
Chris@0
|
128 if (is_string($var)) {
|
Chris@0
|
129 if (isset(self::$stubsCache[$var])) {
|
Chris@0
|
130 return self::$stubsCache[$var];
|
Chris@0
|
131 }
|
Chris@0
|
132 if (false !== strpos($var, '\\')) {
|
Chris@0
|
133 $c = (false !== $i = strpos($var, '::')) ? substr($var, 0, $i) : $var;
|
Chris@0
|
134 if (class_exists($c, false) || interface_exists($c, false) || trait_exists($c, false)) {
|
Chris@0
|
135 return self::$stubsCache[$var] = new ClassStub($var);
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|
Chris@0
|
138 if (false !== strpos($var, DIRECTORY_SEPARATOR) && false === strpos($var, '://') && false === strpos($var, "\0") && @is_file($var)) {
|
Chris@0
|
139 return self::$stubsCache[$var] = new LinkStub($var);
|
Chris@0
|
140 }
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 return $var;
|
Chris@0
|
144 }
|
Chris@0
|
145 }
|