| 148 |
lars |
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\HttpFoundation\Request;
|
|
|
15 |
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
16 |
use Symfony\Component\HttpFoundation\Response;
|
|
|
17 |
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
|
|
|
18 |
use Symfony\Component\Stopwatch\Stopwatch;
|
|
|
19 |
use Symfony\Component\VarDumper\Cloner\Data;
|
|
|
20 |
use Symfony\Component\VarDumper\Cloner\VarCloner;
|
|
|
21 |
use Symfony\Component\VarDumper\Dumper\CliDumper;
|
|
|
22 |
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
|
|
|
23 |
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
|
|
|
24 |
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
|
|
|
25 |
use Symfony\Component\VarDumper\Server\Connection;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
29 |
*
|
|
|
30 |
* @final
|
|
|
31 |
*/
|
|
|
32 |
class DumpDataCollector extends DataCollector implements DataDumperInterface
|
|
|
33 |
{
|
|
|
34 |
private ?Stopwatch $stopwatch = null;
|
|
|
35 |
private string|FileLinkFormatter|false $fileLinkFormat;
|
|
|
36 |
private int $dataCount = 0;
|
|
|
37 |
private bool $isCollected = true;
|
|
|
38 |
private int $clonesCount = 0;
|
|
|
39 |
private int $clonesIndex = 0;
|
|
|
40 |
private array $rootRefs;
|
|
|
41 |
private string $charset;
|
|
|
42 |
private ?RequestStack $requestStack;
|
|
|
43 |
private DataDumperInterface|Connection|null $dumper;
|
|
|
44 |
private mixed $sourceContextProvider;
|
|
|
45 |
|
|
|
46 |
public function __construct(Stopwatch $stopwatch = null, string|FileLinkFormatter $fileLinkFormat = null, string $charset = null, RequestStack $requestStack = null, DataDumperInterface|Connection $dumper = null)
|
|
|
47 |
{
|
|
|
48 |
$fileLinkFormat = $fileLinkFormat ?: \ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
|
|
|
49 |
$this->stopwatch = $stopwatch;
|
|
|
50 |
$this->fileLinkFormat = $fileLinkFormat instanceof FileLinkFormatter && false === $fileLinkFormat->format('', 0) ? false : $fileLinkFormat;
|
|
|
51 |
$this->charset = $charset ?: \ini_get('php.output_encoding') ?: \ini_get('default_charset') ?: 'UTF-8';
|
|
|
52 |
$this->requestStack = $requestStack;
|
|
|
53 |
$this->dumper = $dumper;
|
|
|
54 |
|
|
|
55 |
// All clones share these properties by reference:
|
|
|
56 |
$this->rootRefs = [
|
|
|
57 |
&$this->data,
|
|
|
58 |
&$this->dataCount,
|
|
|
59 |
&$this->isCollected,
|
|
|
60 |
&$this->clonesCount,
|
|
|
61 |
];
|
|
|
62 |
|
|
|
63 |
$this->sourceContextProvider = $dumper instanceof Connection && isset($dumper->getContextProviders()['source']) ? $dumper->getContextProviders()['source'] : new SourceContextProvider($this->charset);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public function __clone()
|
|
|
67 |
{
|
|
|
68 |
$this->clonesIndex = ++$this->clonesCount;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public function dump(Data $data)
|
|
|
72 |
{
|
|
|
73 |
$this->stopwatch?->start('dump');
|
|
|
74 |
|
|
|
75 |
['name' => $name, 'file' => $file, 'line' => $line, 'file_excerpt' => $fileExcerpt] = $this->sourceContextProvider->getContext();
|
|
|
76 |
|
|
|
77 |
if ($this->dumper instanceof Connection) {
|
|
|
78 |
if (!$this->dumper->write($data)) {
|
|
|
79 |
$this->isCollected = false;
|
|
|
80 |
}
|
|
|
81 |
} elseif ($this->dumper) {
|
|
|
82 |
$this->doDump($this->dumper, $data, $name, $file, $line);
|
|
|
83 |
} else {
|
|
|
84 |
$this->isCollected = false;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
if (!$this->dataCount) {
|
|
|
88 |
$this->data = [];
|
|
|
89 |
}
|
|
|
90 |
$this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
|
|
|
91 |
++$this->dataCount;
|
|
|
92 |
|
|
|
93 |
$this->stopwatch?->stop('dump');
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public function collect(Request $request, Response $response, \Throwable $exception = null)
|
|
|
97 |
{
|
|
|
98 |
if (!$this->dataCount) {
|
|
|
99 |
$this->data = [];
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
// Sub-requests and programmatic calls stay in the collected profile.
|
|
|
103 |
if ($this->dumper || ($this->requestStack && $this->requestStack->getMainRequest() !== $request) || $request->isXmlHttpRequest() || $request->headers->has('Origin')) {
|
|
|
104 |
return;
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
// In all other conditions that remove the web debug toolbar, dumps are written on the output.
|
|
|
108 |
if (!$this->requestStack
|
|
|
109 |
|| !$response->headers->has('X-Debug-Token')
|
|
|
110 |
|| $response->isRedirection()
|
|
|
111 |
|| ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type'), 'html'))
|
|
|
112 |
|| 'html' !== $request->getRequestFormat()
|
|
|
113 |
|| false === strripos($response->getContent(), '</body>')
|
|
|
114 |
) {
|
|
|
115 |
if ($response->headers->has('Content-Type') && str_contains($response->headers->get('Content-Type'), 'html')) {
|
|
|
116 |
$dumper = new HtmlDumper('php://output', $this->charset);
|
|
|
117 |
$dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
|
|
|
118 |
} else {
|
|
|
119 |
$dumper = new CliDumper('php://output', $this->charset);
|
|
|
120 |
if (method_exists($dumper, 'setDisplayOptions')) {
|
|
|
121 |
$dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
foreach ($this->data as $dump) {
|
|
|
126 |
$this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line']);
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public function reset()
|
|
|
132 |
{
|
|
|
133 |
$this->stopwatch?->reset();
|
|
|
134 |
$this->data = [];
|
|
|
135 |
$this->dataCount = 0;
|
|
|
136 |
$this->isCollected = true;
|
|
|
137 |
$this->clonesCount = 0;
|
|
|
138 |
$this->clonesIndex = 0;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* @internal
|
|
|
143 |
*/
|
|
|
144 |
public function __sleep(): array
|
|
|
145 |
{
|
|
|
146 |
if (!$this->dataCount) {
|
|
|
147 |
$this->data = [];
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
if ($this->clonesCount !== $this->clonesIndex) {
|
|
|
151 |
return [];
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
$this->data[] = $this->fileLinkFormat;
|
|
|
155 |
$this->data[] = $this->charset;
|
|
|
156 |
$this->dataCount = 0;
|
|
|
157 |
$this->isCollected = true;
|
|
|
158 |
|
|
|
159 |
return parent::__sleep();
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* @internal
|
|
|
164 |
*/
|
|
|
165 |
public function __wakeup()
|
|
|
166 |
{
|
|
|
167 |
parent::__wakeup();
|
|
|
168 |
|
|
|
169 |
$charset = array_pop($this->data);
|
|
|
170 |
$fileLinkFormat = array_pop($this->data);
|
|
|
171 |
$this->dataCount = \count($this->data);
|
|
|
172 |
foreach ($this->data as $dump) {
|
|
|
173 |
if (!\is_string($dump['name']) || !\is_string($dump['file']) || !\is_int($dump['line'])) {
|
|
|
174 |
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
self::__construct($this->stopwatch, \is_string($fileLinkFormat) || $fileLinkFormat instanceof FileLinkFormatter ? $fileLinkFormat : null, \is_string($charset) ? $charset : null);
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
public function getDumpsCount(): int
|
|
|
182 |
{
|
|
|
183 |
return $this->dataCount;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
public function getDumps(string $format, int $maxDepthLimit = -1, int $maxItemsPerDepth = -1): array
|
|
|
187 |
{
|
|
|
188 |
$data = fopen('php://memory', 'r+');
|
|
|
189 |
|
|
|
190 |
if ('html' === $format) {
|
|
|
191 |
$dumper = new HtmlDumper($data, $this->charset);
|
|
|
192 |
$dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
|
|
|
193 |
} else {
|
|
|
194 |
throw new \InvalidArgumentException(sprintf('Invalid dump format: "%s".', $format));
|
|
|
195 |
}
|
|
|
196 |
$dumps = [];
|
|
|
197 |
|
|
|
198 |
if (!$this->dataCount) {
|
|
|
199 |
return $this->data = [];
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
foreach ($this->data as $dump) {
|
|
|
203 |
$dumper->dump($dump['data']->withMaxDepth($maxDepthLimit)->withMaxItemsPerDepth($maxItemsPerDepth));
|
|
|
204 |
$dump['data'] = stream_get_contents($data, -1, 0);
|
|
|
205 |
ftruncate($data, 0);
|
|
|
206 |
rewind($data);
|
|
|
207 |
$dumps[] = $dump;
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
return $dumps;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
public function getName(): string
|
|
|
214 |
{
|
|
|
215 |
return 'dump';
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
public function __destruct()
|
|
|
219 |
{
|
|
|
220 |
if (0 === $this->clonesCount-- && !$this->isCollected && $this->dataCount) {
|
|
|
221 |
$this->clonesCount = 0;
|
|
|
222 |
$this->isCollected = true;
|
|
|
223 |
|
|
|
224 |
$h = headers_list();
|
|
|
225 |
$i = \count($h);
|
|
|
226 |
array_unshift($h, 'Content-Type: '.\ini_get('default_mimetype'));
|
|
|
227 |
while (0 !== stripos($h[$i], 'Content-Type:')) {
|
|
|
228 |
--$i;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && stripos($h[$i], 'html')) {
|
|
|
232 |
$dumper = new HtmlDumper('php://output', $this->charset);
|
|
|
233 |
$dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
|
|
|
234 |
} else {
|
|
|
235 |
$dumper = new CliDumper('php://output', $this->charset);
|
|
|
236 |
if (method_exists($dumper, 'setDisplayOptions')) {
|
|
|
237 |
$dumper->setDisplayOptions(['fileLinkFormat' => $this->fileLinkFormat]);
|
|
|
238 |
}
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
foreach ($this->data as $i => $dump) {
|
|
|
242 |
$this->data[$i] = null;
|
|
|
243 |
$this->doDump($dumper, $dump['data'], $dump['name'], $dump['file'], $dump['line']);
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
$this->data = [];
|
|
|
247 |
$this->dataCount = 0;
|
|
|
248 |
}
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
private function doDump(DataDumperInterface $dumper, Data $data, string $name, string $file, int $line)
|
|
|
252 |
{
|
|
|
253 |
if ($dumper instanceof CliDumper) {
|
|
|
254 |
$contextDumper = function ($name, $file, $line, $fmt) {
|
|
|
255 |
if ($this instanceof HtmlDumper) {
|
|
|
256 |
if ($file) {
|
|
|
257 |
$s = $this->style('meta', '%s');
|
|
|
258 |
$f = strip_tags($this->style('', $file));
|
|
|
259 |
$name = strip_tags($this->style('', $name));
|
|
|
260 |
if ($fmt && $link = \is_string($fmt) ? strtr($fmt, ['%f' => $file, '%l' => $line]) : $fmt->format($file, $line)) {
|
|
|
261 |
$name = sprintf('<a href="%s" title="%s">'.$s.'</a>', strip_tags($this->style('', $link)), $f, $name);
|
|
|
262 |
} else {
|
|
|
263 |
$name = sprintf('<abbr title="%s">'.$s.'</abbr>', $f, $name);
|
|
|
264 |
}
|
|
|
265 |
} else {
|
|
|
266 |
$name = $this->style('meta', $name);
|
|
|
267 |
}
|
|
|
268 |
$this->line = $name.' on line '.$this->style('meta', $line).':';
|
|
|
269 |
} else {
|
|
|
270 |
$this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
|
|
|
271 |
}
|
|
|
272 |
$this->dumpLine(0);
|
|
|
273 |
};
|
|
|
274 |
$contextDumper = $contextDumper->bindTo($dumper, $dumper);
|
|
|
275 |
$contextDumper($name, $file, $line, $this->fileLinkFormat);
|
|
|
276 |
} else {
|
|
|
277 |
$cloner = new VarCloner();
|
|
|
278 |
$dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
|
|
|
279 |
}
|
|
|
280 |
$dumper->dump($data);
|
|
|
281 |
}
|
|
|
282 |
}
|