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