Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/debug/ExceptionHandler.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\Debug; | |
13 | |
14 use Symfony\Component\Debug\Exception\FlattenException; | |
15 use Symfony\Component\Debug\Exception\OutOfMemoryException; | |
16 use Symfony\Component\HttpKernel\Debug\FileLinkFormatter; | |
17 | |
18 /** | |
19 * ExceptionHandler converts an exception to a Response object. | |
20 * | |
21 * It is mostly useful in debug mode to replace the default PHP/XDebug | |
22 * output with something prettier and more useful. | |
23 * | |
24 * As this class is mainly used during Kernel boot, where nothing is yet | |
25 * available, the Response content is always HTML. | |
26 * | |
27 * @author Fabien Potencier <fabien@symfony.com> | |
28 * @author Nicolas Grekas <p@tchwork.com> | |
29 */ | |
30 class ExceptionHandler | |
31 { | |
32 private $debug; | |
33 private $charset; | |
34 private $handler; | |
35 private $caughtBuffer; | |
36 private $caughtLength; | |
37 private $fileLinkFormat; | |
38 | |
39 public function __construct($debug = true, $charset = null, $fileLinkFormat = null) | |
40 { | |
41 $this->debug = $debug; | |
42 $this->charset = $charset ?: ini_get('default_charset') ?: 'UTF-8'; | |
43 $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format'); | |
44 } | |
45 | |
46 /** | |
47 * Registers the exception handler. | |
48 * | |
49 * @param bool $debug Enable/disable debug mode, where the stack trace is displayed | |
50 * @param string|null $charset The charset used by exception messages | |
51 * @param string|null $fileLinkFormat The IDE link template | |
52 * | |
53 * @return static | |
54 */ | |
55 public static function register($debug = true, $charset = null, $fileLinkFormat = null) | |
56 { | |
57 $handler = new static($debug, $charset, $fileLinkFormat); | |
58 | |
59 $prev = set_exception_handler(array($handler, 'handle')); | |
60 if (is_array($prev) && $prev[0] instanceof ErrorHandler) { | |
61 restore_exception_handler(); | |
62 $prev[0]->setExceptionHandler(array($handler, 'handle')); | |
63 } | |
64 | |
65 return $handler; | |
66 } | |
67 | |
68 /** | |
69 * Sets a user exception handler. | |
70 * | |
71 * @param callable $handler An handler that will be called on Exception | |
72 * | |
73 * @return callable|null The previous exception handler if any | |
74 */ | |
75 public function setHandler(callable $handler = null) | |
76 { | |
77 $old = $this->handler; | |
78 $this->handler = $handler; | |
79 | |
80 return $old; | |
81 } | |
82 | |
83 /** | |
84 * Sets the format for links to source files. | |
85 * | |
86 * @param string|FileLinkFormatter $fileLinkFormat The format for links to source files | |
87 * | |
88 * @return string The previous file link format | |
89 */ | |
90 public function setFileLinkFormat($fileLinkFormat) | |
91 { | |
92 $old = $this->fileLinkFormat; | |
93 $this->fileLinkFormat = $fileLinkFormat; | |
94 | |
95 return $old; | |
96 } | |
97 | |
98 /** | |
99 * Sends a response for the given Exception. | |
100 * | |
101 * To be as fail-safe as possible, the exception is first handled | |
102 * by our simple exception handler, then by the user exception handler. | |
103 * The latter takes precedence and any output from the former is cancelled, | |
104 * if and only if nothing bad happens in this handling path. | |
105 */ | |
106 public function handle(\Exception $exception) | |
107 { | |
108 if (null === $this->handler || $exception instanceof OutOfMemoryException) { | |
109 $this->sendPhpResponse($exception); | |
110 | |
111 return; | |
112 } | |
113 | |
114 $caughtLength = $this->caughtLength = 0; | |
115 | |
116 ob_start(function ($buffer) { | |
117 $this->caughtBuffer = $buffer; | |
118 | |
119 return ''; | |
120 }); | |
121 | |
122 $this->sendPhpResponse($exception); | |
123 while (null === $this->caughtBuffer && ob_end_flush()) { | |
124 // Empty loop, everything is in the condition | |
125 } | |
126 if (isset($this->caughtBuffer[0])) { | |
127 ob_start(function ($buffer) { | |
128 if ($this->caughtLength) { | |
129 // use substr_replace() instead of substr() for mbstring overloading resistance | |
130 $cleanBuffer = substr_replace($buffer, '', 0, $this->caughtLength); | |
131 if (isset($cleanBuffer[0])) { | |
132 $buffer = $cleanBuffer; | |
133 } | |
134 } | |
135 | |
136 return $buffer; | |
137 }); | |
138 | |
139 echo $this->caughtBuffer; | |
140 $caughtLength = ob_get_length(); | |
141 } | |
142 $this->caughtBuffer = null; | |
143 | |
144 try { | |
145 call_user_func($this->handler, $exception); | |
146 $this->caughtLength = $caughtLength; | |
147 } catch (\Exception $e) { | |
148 if (!$caughtLength) { | |
149 // All handlers failed. Let PHP handle that now. | |
150 throw $exception; | |
151 } | |
152 } | |
153 } | |
154 | |
155 /** | |
156 * Sends the error associated with the given Exception as a plain PHP response. | |
157 * | |
158 * This method uses plain PHP functions like header() and echo to output | |
159 * the response. | |
160 * | |
161 * @param \Exception|FlattenException $exception An \Exception or FlattenException instance | |
162 */ | |
163 public function sendPhpResponse($exception) | |
164 { | |
165 if (!$exception instanceof FlattenException) { | |
166 $exception = FlattenException::create($exception); | |
167 } | |
168 | |
169 if (!headers_sent()) { | |
170 header(sprintf('HTTP/1.0 %s', $exception->getStatusCode())); | |
171 foreach ($exception->getHeaders() as $name => $value) { | |
172 header($name.': '.$value, false); | |
173 } | |
174 header('Content-Type: text/html; charset='.$this->charset); | |
175 } | |
176 | |
177 echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception)); | |
178 } | |
179 | |
180 /** | |
181 * Gets the full HTML content associated with the given exception. | |
182 * | |
183 * @param \Exception|FlattenException $exception An \Exception or FlattenException instance | |
184 * | |
185 * @return string The HTML content as a string | |
186 */ | |
187 public function getHtml($exception) | |
188 { | |
189 if (!$exception instanceof FlattenException) { | |
190 $exception = FlattenException::create($exception); | |
191 } | |
192 | |
193 return $this->decorate($this->getContent($exception), $this->getStylesheet($exception)); | |
194 } | |
195 | |
196 /** | |
197 * Gets the HTML content associated with the given exception. | |
198 * | |
199 * @param FlattenException $exception A FlattenException instance | |
200 * | |
201 * @return string The content as a string | |
202 */ | |
203 public function getContent(FlattenException $exception) | |
204 { | |
205 switch ($exception->getStatusCode()) { | |
206 case 404: | |
207 $title = 'Sorry, the page you are looking for could not be found.'; | |
208 break; | |
209 default: | |
210 $title = 'Whoops, looks like something went wrong.'; | |
211 } | |
212 | |
213 $content = ''; | |
214 if ($this->debug) { | |
215 try { | |
216 $count = count($exception->getAllPrevious()); | |
217 $total = $count + 1; | |
218 foreach ($exception->toArray() as $position => $e) { | |
219 $ind = $count - $position + 1; | |
220 $class = $this->formatClass($e['class']); | |
221 $message = nl2br($this->escapeHtml($e['message'])); | |
222 $content .= sprintf(<<<'EOF' | |
223 <h2 class="block_exception clear_fix"> | |
224 <span class="exception_counter">%d/%d</span> | |
225 <span class="exception_title">%s%s:</span> | |
226 <span class="exception_message">%s</span> | |
227 </h2> | |
228 <div class="block"> | |
229 <ol class="traces list_exception"> | |
230 | |
231 EOF | |
232 , $ind, $total, $class, $this->formatPath($e['trace'][0]['file'], $e['trace'][0]['line']), $message); | |
233 foreach ($e['trace'] as $trace) { | |
234 $content .= ' <li>'; | |
235 if ($trace['function']) { | |
236 $content .= sprintf('at %s%s%s(%s)', $this->formatClass($trace['class']), $trace['type'], $trace['function'], $this->formatArgs($trace['args'])); | |
237 } | |
238 if (isset($trace['file']) && isset($trace['line'])) { | |
239 $content .= $this->formatPath($trace['file'], $trace['line']); | |
240 } | |
241 $content .= "</li>\n"; | |
242 } | |
243 | |
244 $content .= " </ol>\n</div>\n"; | |
245 } | |
246 } catch (\Exception $e) { | |
247 // something nasty happened and we cannot throw an exception anymore | |
248 if ($this->debug) { | |
249 $title = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $this->escapeHtml($e->getMessage())); | |
250 } else { | |
251 $title = 'Whoops, looks like something went wrong.'; | |
252 } | |
253 } | |
254 } | |
255 | |
256 return <<<EOF | |
257 <div id="sf-resetcontent" class="sf-reset"> | |
258 <h1>$title</h1> | |
259 $content | |
260 </div> | |
261 EOF; | |
262 } | |
263 | |
264 /** | |
265 * Gets the stylesheet associated with the given exception. | |
266 * | |
267 * @param FlattenException $exception A FlattenException instance | |
268 * | |
269 * @return string The stylesheet as a string | |
270 */ | |
271 public function getStylesheet(FlattenException $exception) | |
272 { | |
273 return <<<'EOF' | |
274 .sf-reset { font: 11px Verdana, Arial, sans-serif; color: #333 } | |
275 .sf-reset .clear { clear:both; height:0; font-size:0; line-height:0; } | |
276 .sf-reset .clear_fix:after { display:block; height:0; clear:both; visibility:hidden; } | |
277 .sf-reset .clear_fix { display:inline-block; } | |
278 .sf-reset * html .clear_fix { height:1%; } | |
279 .sf-reset .clear_fix { display:block; } | |
280 .sf-reset, .sf-reset .block { margin: auto } | |
281 .sf-reset abbr { border-bottom: 1px dotted #000; cursor: help; } | |
282 .sf-reset p { font-size:14px; line-height:20px; color:#868686; padding-bottom:20px } | |
283 .sf-reset strong { font-weight:bold; } | |
284 .sf-reset a { color:#6c6159; cursor: default; } | |
285 .sf-reset a img { border:none; } | |
286 .sf-reset a:hover { text-decoration:underline; } | |
287 .sf-reset em { font-style:italic; } | |
288 .sf-reset h1, .sf-reset h2 { font: 20px Georgia, "Times New Roman", Times, serif } | |
289 .sf-reset .exception_counter { background-color: #fff; color: #333; padding: 6px; float: left; margin-right: 10px; float: left; display: block; } | |
290 .sf-reset .exception_title { margin-left: 3em; margin-bottom: 0.7em; display: block; } | |
291 .sf-reset .exception_message { margin-left: 3em; display: block; } | |
292 .sf-reset .traces li { font-size:12px; padding: 2px 4px; list-style-type:decimal; margin-left:20px; } | |
293 .sf-reset .block { background-color:#FFFFFF; padding:10px 28px; margin-bottom:20px; | |
294 border-bottom-right-radius: 16px; | |
295 border-bottom-left-radius: 16px; | |
296 border-bottom:1px solid #ccc; | |
297 border-right:1px solid #ccc; | |
298 border-left:1px solid #ccc; | |
299 word-wrap: break-word; | |
300 } | |
301 .sf-reset .block_exception { background-color:#ddd; color: #333; padding:20px; | |
302 border-top-left-radius: 16px; | |
303 border-top-right-radius: 16px; | |
304 border-top:1px solid #ccc; | |
305 border-right:1px solid #ccc; | |
306 border-left:1px solid #ccc; | |
307 overflow: hidden; | |
308 word-wrap: break-word; | |
309 } | |
310 .sf-reset a { background:none; color:#868686; text-decoration:none; } | |
311 .sf-reset a:hover { background:none; color:#313131; text-decoration:underline; } | |
312 .sf-reset ol { padding: 10px 0; } | |
313 .sf-reset h1 { background-color:#FFFFFF; padding: 15px 28px; margin-bottom: 20px; | |
314 border-radius: 10px; | |
315 border: 1px solid #ccc; | |
316 } | |
317 EOF; | |
318 } | |
319 | |
320 private function decorate($content, $css) | |
321 { | |
322 return <<<EOF | |
323 <!DOCTYPE html> | |
324 <html> | |
325 <head> | |
326 <meta charset="{$this->charset}" /> | |
327 <meta name="robots" content="noindex,nofollow" /> | |
328 <style> | |
329 /* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */ | |
330 html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:text-top;}sub{vertical-align:text-bottom;}input,textarea,select{font-family:inherit;font-size:inherit;font-weight:inherit;}input,textarea,select{*font-size:100%;}legend{color:#000;} | |
331 | |
332 html { background: #eee; padding: 10px } | |
333 img { border: 0; } | |
334 #sf-resetcontent { width:970px; margin:0 auto; } | |
335 $css | |
336 </style> | |
337 </head> | |
338 <body ondblclick="var t = event.target; if (t.title && !t.href) { var f = t.innerHTML; t.innerHTML = t.title; t.title = f; }"> | |
339 $content | |
340 </body> | |
341 </html> | |
342 EOF; | |
343 } | |
344 | |
345 private function formatClass($class) | |
346 { | |
347 $parts = explode('\\', $class); | |
348 | |
349 return sprintf('<abbr title="%s">%s</abbr>', $class, array_pop($parts)); | |
350 } | |
351 | |
352 private function formatPath($path, $line) | |
353 { | |
354 $file = $this->escapeHtml(preg_match('#[^/\\\\]*+$#', $path, $file) ? $file[0] : $path); | |
355 $fmt = $this->fileLinkFormat; | |
356 | |
357 if ($fmt && $link = is_string($fmt) ? strtr($fmt, array('%f' => $path, '%l' => $line)) : $fmt->format($path, $line)) { | |
358 return sprintf(' in <a href="%s" title="Go to source">%s line %d</a>', $this->escapeHtml($link), $file, $line); | |
359 } | |
360 | |
361 return sprintf(' in <a title="%s line %3$d">%s line %d</a>', $this->escapeHtml($path), $file, $line); | |
362 } | |
363 | |
364 /** | |
365 * Formats an array as a string. | |
366 * | |
367 * @param array $args The argument array | |
368 * | |
369 * @return string | |
370 */ | |
371 private function formatArgs(array $args) | |
372 { | |
373 $result = array(); | |
374 foreach ($args as $key => $item) { | |
375 if ('object' === $item[0]) { | |
376 $formattedValue = sprintf('<em>object</em>(%s)', $this->formatClass($item[1])); | |
377 } elseif ('array' === $item[0]) { | |
378 $formattedValue = sprintf('<em>array</em>(%s)', is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]); | |
379 } elseif ('null' === $item[0]) { | |
380 $formattedValue = '<em>null</em>'; | |
381 } elseif ('boolean' === $item[0]) { | |
382 $formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>'; | |
383 } elseif ('resource' === $item[0]) { | |
384 $formattedValue = '<em>resource</em>'; | |
385 } else { | |
386 $formattedValue = str_replace("\n", '', $this->escapeHtml(var_export($item[1], true))); | |
387 } | |
388 | |
389 $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue); | |
390 } | |
391 | |
392 return implode(', ', $result); | |
393 } | |
394 | |
395 /** | |
396 * HTML-encodes a string. | |
397 */ | |
398 private function escapeHtml($str) | |
399 { | |
400 return htmlspecialchars($str, ENT_COMPAT | ENT_SUBSTITUTE, $this->charset); | |
401 } | |
402 } |