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@14
|
15 use Symfony\Component\VarDumper\Caster\CutStub;
|
Chris@0
|
16 use Symfony\Component\VarDumper\Cloner\ClonerInterface;
|
Chris@0
|
17 use Symfony\Component\VarDumper\Cloner\Data;
|
Chris@0
|
18 use Symfony\Component\VarDumper\Cloner\Stub;
|
Chris@0
|
19 use Symfony\Component\VarDumper\Cloner\VarCloner;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * DataCollector.
|
Chris@0
|
23 *
|
Chris@0
|
24 * Children of this class must store the collected data in the data property.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
27 * @author Bernhard Schussek <bschussek@symfony.com>
|
Chris@0
|
28 */
|
Chris@0
|
29 abstract class DataCollector implements DataCollectorInterface, \Serializable
|
Chris@0
|
30 {
|
Chris@17
|
31 protected $data = [];
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * @var ValueExporter
|
Chris@0
|
35 */
|
Chris@0
|
36 private $valueExporter;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * @var ClonerInterface
|
Chris@0
|
40 */
|
Chris@0
|
41 private $cloner;
|
Chris@0
|
42
|
Chris@0
|
43 public function serialize()
|
Chris@0
|
44 {
|
Chris@17
|
45 $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 2);
|
Chris@17
|
46 $isCalledFromOverridingMethod = isset($trace[1]['function'], $trace[1]['object']) && 'serialize' === $trace[1]['function'] && $this === $trace[1]['object'];
|
Chris@17
|
47
|
Chris@17
|
48 return $isCalledFromOverridingMethod ? $this->data : serialize($this->data);
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 public function unserialize($data)
|
Chris@0
|
52 {
|
Chris@17
|
53 $this->data = \is_array($data) ? $data : unserialize($data);
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * Converts the variable into a serializable Data instance.
|
Chris@0
|
58 *
|
Chris@0
|
59 * This array can be displayed in the template using
|
Chris@0
|
60 * the VarDumper component.
|
Chris@0
|
61 *
|
Chris@0
|
62 * @param mixed $var
|
Chris@0
|
63 *
|
Chris@0
|
64 * @return Data
|
Chris@0
|
65 */
|
Chris@0
|
66 protected function cloneVar($var)
|
Chris@0
|
67 {
|
Chris@14
|
68 if ($var instanceof Data) {
|
Chris@14
|
69 return $var;
|
Chris@14
|
70 }
|
Chris@0
|
71 if (null === $this->cloner) {
|
Chris@14
|
72 if (class_exists(CutStub::class)) {
|
Chris@0
|
73 $this->cloner = new VarCloner();
|
Chris@14
|
74 $this->cloner->setMaxItems(-1);
|
Chris@14
|
75 $this->cloner->addCasters($this->getCasters());
|
Chris@0
|
76 } else {
|
Chris@14
|
77 @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);
|
Chris@0
|
78 $this->cloner = false;
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81 if (false === $this->cloner) {
|
Chris@0
|
82 if (null === $this->valueExporter) {
|
Chris@0
|
83 $this->valueExporter = new ValueExporter();
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 return $this->valueExporter->exportValue($var);
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@14
|
89 return $this->cloner->cloneVar($var);
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Converts a PHP variable to a string.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @param mixed $var A PHP variable
|
Chris@0
|
96 *
|
Chris@0
|
97 * @return string The string representation of the variable
|
Chris@0
|
98 *
|
Chris@14
|
99 * @deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
|
Chris@0
|
100 */
|
Chris@0
|
101 protected function varToString($var)
|
Chris@0
|
102 {
|
Chris@14
|
103 @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);
|
Chris@0
|
104
|
Chris@0
|
105 if (null === $this->valueExporter) {
|
Chris@0
|
106 $this->valueExporter = new ValueExporter();
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 return $this->valueExporter->exportValue($var);
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@14
|
112 /**
|
Chris@14
|
113 * @return callable[] The casters to add to the cloner
|
Chris@14
|
114 */
|
Chris@14
|
115 protected function getCasters()
|
Chris@0
|
116 {
|
Chris@17
|
117 return [
|
Chris@14
|
118 '*' => function ($v, array $a, Stub $s, $isNested) {
|
Chris@14
|
119 if (!$v instanceof Stub) {
|
Chris@14
|
120 foreach ($a as $k => $v) {
|
Chris@17
|
121 if (\is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
|
Chris@14
|
122 $a[$k] = new CutStub($v);
|
Chris@14
|
123 }
|
Chris@14
|
124 }
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@14
|
127 return $a;
|
Chris@14
|
128 },
|
Chris@17
|
129 ];
|
Chris@0
|
130 }
|
Chris@0
|
131 }
|