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@0
|
31 protected $data = array();
|
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@0
|
45 return serialize($this->data);
|
Chris@0
|
46 }
|
Chris@0
|
47
|
Chris@0
|
48 public function unserialize($data)
|
Chris@0
|
49 {
|
Chris@0
|
50 $this->data = unserialize($data);
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Converts the variable into a serializable Data instance.
|
Chris@0
|
55 *
|
Chris@0
|
56 * This array can be displayed in the template using
|
Chris@0
|
57 * the VarDumper component.
|
Chris@0
|
58 *
|
Chris@0
|
59 * @param mixed $var
|
Chris@0
|
60 *
|
Chris@0
|
61 * @return Data
|
Chris@0
|
62 */
|
Chris@0
|
63 protected function cloneVar($var)
|
Chris@0
|
64 {
|
Chris@14
|
65 if ($var instanceof Data) {
|
Chris@14
|
66 return $var;
|
Chris@14
|
67 }
|
Chris@0
|
68 if (null === $this->cloner) {
|
Chris@14
|
69 if (class_exists(CutStub::class)) {
|
Chris@0
|
70 $this->cloner = new VarCloner();
|
Chris@14
|
71 $this->cloner->setMaxItems(-1);
|
Chris@14
|
72 $this->cloner->addCasters($this->getCasters());
|
Chris@0
|
73 } else {
|
Chris@14
|
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);
|
Chris@0
|
75 $this->cloner = false;
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78 if (false === $this->cloner) {
|
Chris@0
|
79 if (null === $this->valueExporter) {
|
Chris@0
|
80 $this->valueExporter = new ValueExporter();
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 return $this->valueExporter->exportValue($var);
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@14
|
86 return $this->cloner->cloneVar($var);
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * Converts a PHP variable to a string.
|
Chris@0
|
91 *
|
Chris@0
|
92 * @param mixed $var A PHP variable
|
Chris@0
|
93 *
|
Chris@0
|
94 * @return string The string representation of the variable
|
Chris@0
|
95 *
|
Chris@14
|
96 * @deprecated since version 3.2, to be removed in 4.0. Use cloneVar() instead.
|
Chris@0
|
97 */
|
Chris@0
|
98 protected function varToString($var)
|
Chris@0
|
99 {
|
Chris@14
|
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);
|
Chris@0
|
101
|
Chris@0
|
102 if (null === $this->valueExporter) {
|
Chris@0
|
103 $this->valueExporter = new ValueExporter();
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 return $this->valueExporter->exportValue($var);
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@14
|
109 /**
|
Chris@14
|
110 * @return callable[] The casters to add to the cloner
|
Chris@14
|
111 */
|
Chris@14
|
112 protected function getCasters()
|
Chris@0
|
113 {
|
Chris@14
|
114 return array(
|
Chris@14
|
115 '*' => function ($v, array $a, Stub $s, $isNested) {
|
Chris@14
|
116 if (!$v instanceof Stub) {
|
Chris@14
|
117 foreach ($a as $k => $v) {
|
Chris@14
|
118 if (is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
|
Chris@14
|
119 $a[$k] = new CutStub($v);
|
Chris@14
|
120 }
|
Chris@14
|
121 }
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@14
|
124 return $a;
|
Chris@14
|
125 },
|
Chris@14
|
126 );
|
Chris@0
|
127 }
|
Chris@0
|
128 }
|