Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/http-kernel/DataCollector/DumpDataCollector.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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\Stopwatch\Stopwatch; | |
18 use Symfony\Component\VarDumper\Cloner\Data; | |
19 use Symfony\Component\VarDumper\Cloner\VarCloner; | |
20 use Symfony\Component\VarDumper\Dumper\CliDumper; | |
21 use Symfony\Component\VarDumper\Dumper\HtmlDumper; | |
22 use Symfony\Component\VarDumper\Dumper\DataDumperInterface; | |
23 | |
24 /** | |
25 * @author Nicolas Grekas <p@tchwork.com> | |
26 */ | |
27 class DumpDataCollector extends DataCollector implements DataDumperInterface | |
28 { | |
29 private $stopwatch; | |
30 private $fileLinkFormat; | |
31 private $dataCount = 0; | |
32 private $isCollected = true; | |
33 private $clonesCount = 0; | |
34 private $clonesIndex = 0; | |
35 private $rootRefs; | |
36 private $charset; | |
37 private $requestStack; | |
38 private $dumper; | |
39 private $dumperIsInjected; | |
40 | |
41 public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null, $charset = null, RequestStack $requestStack = null, DataDumperInterface $dumper = null) | |
42 { | |
43 $this->stopwatch = $stopwatch; | |
44 $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format'); | |
45 $this->charset = $charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8'; | |
46 $this->requestStack = $requestStack; | |
47 $this->dumper = $dumper; | |
48 $this->dumperIsInjected = null !== $dumper; | |
49 | |
50 // All clones share these properties by reference: | |
51 $this->rootRefs = array( | |
52 &$this->data, | |
53 &$this->dataCount, | |
54 &$this->isCollected, | |
55 &$this->clonesCount, | |
56 ); | |
57 } | |
58 | |
59 public function __clone() | |
60 { | |
61 $this->clonesIndex = ++$this->clonesCount; | |
62 } | |
63 | |
64 public function dump(Data $data) | |
65 { | |
66 if ($this->stopwatch) { | |
67 $this->stopwatch->start('dump'); | |
68 } | |
69 if ($this->isCollected) { | |
70 $this->isCollected = false; | |
71 } | |
72 | |
73 $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 7); | |
74 | |
75 $file = $trace[0]['file']; | |
76 $line = $trace[0]['line']; | |
77 $name = false; | |
78 $fileExcerpt = false; | |
79 | |
80 for ($i = 1; $i < 7; ++$i) { | |
81 if (isset($trace[$i]['class'], $trace[$i]['function']) | |
82 && 'dump' === $trace[$i]['function'] | |
83 && 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class'] | |
84 ) { | |
85 $file = $trace[$i]['file']; | |
86 $line = $trace[$i]['line']; | |
87 | |
88 while (++$i < 7) { | |
89 if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) { | |
90 $file = $trace[$i]['file']; | |
91 $line = $trace[$i]['line']; | |
92 | |
93 break; | |
94 } elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof \Twig_Template) { | |
95 $template = $trace[$i]['object']; | |
96 $name = $template->getTemplateName(); | |
97 $src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false); | |
98 $info = $template->getDebugInfo(); | |
99 if (isset($info[$trace[$i - 1]['line']])) { | |
100 $line = $info[$trace[$i - 1]['line']]; | |
101 $file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : null; | |
102 | |
103 if ($src) { | |
104 $src = explode("\n", $src); | |
105 $fileExcerpt = array(); | |
106 | |
107 for ($i = max($line - 3, 1), $max = min($line + 3, count($src)); $i <= $max; ++$i) { | |
108 $fileExcerpt[] = '<li'.($i === $line ? ' class="selected"' : '').'><code>'.$this->htmlEncode($src[$i - 1]).'</code></li>'; | |
109 } | |
110 | |
111 $fileExcerpt = '<ol start="'.max($line - 3, 1).'">'.implode("\n", $fileExcerpt).'</ol>'; | |
112 } | |
113 } | |
114 break; | |
115 } | |
116 } | |
117 break; | |
118 } | |
119 } | |
120 | |
121 if (false === $name) { | |
122 $name = str_replace('\\', '/', $file); | |
123 $name = substr($name, strrpos($name, '/') + 1); | |
124 } | |
125 | |
126 if ($this->dumper) { | |
127 $this->doDump($data, $name, $file, $line); | |
128 } | |
129 | |
130 $this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt'); | |
131 ++$this->dataCount; | |
132 | |
133 if ($this->stopwatch) { | |
134 $this->stopwatch->stop('dump'); | |
135 } | |
136 } | |
137 | |
138 public function collect(Request $request, Response $response, \Exception $exception = null) | |
139 { | |
140 // Sub-requests and programmatic calls stay in the collected profile. | |
141 if ($this->dumper || ($this->requestStack && $this->requestStack->getMasterRequest() !== $request) || $request->isXmlHttpRequest() || $request->headers->has('Origin')) { | |
142 return; | |
143 } | |
144 | |
145 // In all other conditions that remove the web debug toolbar, dumps are written on the output. | |
146 if (!$this->requestStack | |
147 || !$response->headers->has('X-Debug-Token') | |
148 || $response->isRedirection() | |
149 || ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html')) | |
150 || 'html' !== $request->getRequestFormat() | |
151 || false === strripos($response->getContent(), '</body>') | |
152 ) { | |
153 if ($response->headers->has('Content-Type') && false !== strpos($response->headers->get('Content-Type'), 'html')) { | |
154 $this->dumper = new HtmlDumper('php://output', $this->charset); | |
155 $this->dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat)); | |
156 } else { | |
157 $this->dumper = new CliDumper('php://output', $this->charset); | |
158 } | |
159 | |
160 foreach ($this->data as $dump) { | |
161 $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']); | |
162 } | |
163 } | |
164 } | |
165 | |
166 public function serialize() | |
167 { | |
168 if ($this->clonesCount !== $this->clonesIndex) { | |
169 return 'a:0:{}'; | |
170 } | |
171 | |
172 $this->data[] = $this->fileLinkFormat; | |
173 $this->data[] = $this->charset; | |
174 $ser = serialize($this->data); | |
175 $this->data = array(); | |
176 $this->dataCount = 0; | |
177 $this->isCollected = true; | |
178 if (!$this->dumperIsInjected) { | |
179 $this->dumper = null; | |
180 } | |
181 | |
182 return $ser; | |
183 } | |
184 | |
185 public function unserialize($data) | |
186 { | |
187 parent::unserialize($data); | |
188 $charset = array_pop($this->data); | |
189 $fileLinkFormat = array_pop($this->data); | |
190 $this->dataCount = count($this->data); | |
191 self::__construct($this->stopwatch, $fileLinkFormat, $charset); | |
192 } | |
193 | |
194 public function getDumpsCount() | |
195 { | |
196 return $this->dataCount; | |
197 } | |
198 | |
199 public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1) | |
200 { | |
201 $data = fopen('php://memory', 'r+b'); | |
202 | |
203 if ('html' === $format) { | |
204 $dumper = new HtmlDumper($data, $this->charset); | |
205 $dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat)); | |
206 } else { | |
207 throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format)); | |
208 } | |
209 $dumps = array(); | |
210 | |
211 foreach ($this->data as $dump) { | |
212 $dumper->dump($dump['data']->withMaxDepth($maxDepthLimit)->withMaxItemsPerDepth($maxItemsPerDepth)); | |
213 $dump['data'] = stream_get_contents($data, -1, 0); | |
214 ftruncate($data, 0); | |
215 rewind($data); | |
216 $dumps[] = $dump; | |
217 } | |
218 | |
219 return $dumps; | |
220 } | |
221 | |
222 public function getName() | |
223 { | |
224 return 'dump'; | |
225 } | |
226 | |
227 public function __destruct() | |
228 { | |
229 if (0 === $this->clonesCount-- && !$this->isCollected && $this->data) { | |
230 $this->clonesCount = 0; | |
231 $this->isCollected = true; | |
232 | |
233 $h = headers_list(); | |
234 $i = count($h); | |
235 array_unshift($h, 'Content-Type: '.ini_get('default_mimetype')); | |
236 while (0 !== stripos($h[$i], 'Content-Type:')) { | |
237 --$i; | |
238 } | |
239 | |
240 if ('cli' !== PHP_SAPI && stripos($h[$i], 'html')) { | |
241 $this->dumper = new HtmlDumper('php://output', $this->charset); | |
242 $this->dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat)); | |
243 } else { | |
244 $this->dumper = new CliDumper('php://output', $this->charset); | |
245 } | |
246 | |
247 foreach ($this->data as $i => $dump) { | |
248 $this->data[$i] = null; | |
249 $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']); | |
250 } | |
251 | |
252 $this->data = array(); | |
253 $this->dataCount = 0; | |
254 } | |
255 } | |
256 | |
257 private function doDump($data, $name, $file, $line) | |
258 { | |
259 if ($this->dumper instanceof CliDumper) { | |
260 $contextDumper = function ($name, $file, $line, $fmt) { | |
261 if ($this instanceof HtmlDumper) { | |
262 if ($file) { | |
263 $s = $this->style('meta', '%s'); | |
264 $f = strip_tags($this->style('', $file)); | |
265 $name = strip_tags($this->style('', $name)); | |
266 if ($fmt && $link = is_string($fmt) ? strtr($fmt, array('%f' => $file, '%l' => $line)) : $fmt->format($file, $line)) { | |
267 $name = sprintf('<a href="%s" title="%s">'.$s.'</a>', strip_tags($this->style('', $link)), $f, $name); | |
268 } else { | |
269 $name = sprintf('<abbr title="%s">'.$s.'</abbr>', $f, $name); | |
270 } | |
271 } else { | |
272 $name = $this->style('meta', $name); | |
273 } | |
274 $this->line = $name.' on line '.$this->style('meta', $line).':'; | |
275 } else { | |
276 $this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':'; | |
277 } | |
278 $this->dumpLine(0); | |
279 }; | |
280 $contextDumper = $contextDumper->bindTo($this->dumper, $this->dumper); | |
281 $contextDumper($name, $file, $line, $this->fileLinkFormat); | |
282 } else { | |
283 $cloner = new VarCloner(); | |
284 $this->dumper->dump($cloner->cloneVar($name.' on line '.$line.':')); | |
285 } | |
286 $this->dumper->dump($data); | |
287 } | |
288 | |
289 private function htmlEncode($s) | |
290 { | |
291 $html = ''; | |
292 | |
293 $dumper = new HtmlDumper(function ($line) use (&$html) { $html .= $line; }, $this->charset); | |
294 $dumper->setDumpHeader(''); | |
295 $dumper->setDumpBoundaries('', ''); | |
296 | |
297 $cloner = new VarCloner(); | |
298 $dumper->dump($cloner->cloneVar($s)); | |
299 | |
300 return substr(strip_tags($html), 1, -1); | |
301 } | |
302 } |