comparison vendor/symfony/debug/ExceptionHandler.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
54 */ 54 */
55 public static function register($debug = true, $charset = null, $fileLinkFormat = null) 55 public static function register($debug = true, $charset = null, $fileLinkFormat = null)
56 { 56 {
57 $handler = new static($debug, $charset, $fileLinkFormat); 57 $handler = new static($debug, $charset, $fileLinkFormat);
58 58
59 $prev = set_exception_handler(array($handler, 'handle')); 59 $prev = set_exception_handler([$handler, 'handle']);
60 if (is_array($prev) && $prev[0] instanceof ErrorHandler) { 60 if (\is_array($prev) && $prev[0] instanceof ErrorHandler) {
61 restore_exception_handler(); 61 restore_exception_handler();
62 $prev[0]->setExceptionHandler(array($handler, 'handle')); 62 $prev[0]->setExceptionHandler([$handler, 'handle']);
63 } 63 }
64 64
65 return $handler; 65 return $handler;
66 } 66 }
67 67
140 $caughtLength = ob_get_length(); 140 $caughtLength = ob_get_length();
141 } 141 }
142 $this->caughtBuffer = null; 142 $this->caughtBuffer = null;
143 143
144 try { 144 try {
145 call_user_func($this->handler, $exception); 145 \call_user_func($this->handler, $exception);
146 $this->caughtLength = $caughtLength; 146 $this->caughtLength = $caughtLength;
147 } catch (\Exception $e) { 147 } catch (\Exception $e) {
148 if (!$caughtLength) { 148 if (!$caughtLength) {
149 // All handlers failed. Let PHP handle that now. 149 // All handlers failed. Let PHP handle that now.
150 throw $exception; 150 throw $exception;
206 break; 206 break;
207 default: 207 default:
208 $title = 'Whoops, looks like something went wrong.'; 208 $title = 'Whoops, looks like something went wrong.';
209 } 209 }
210 210
211 if (!$this->debug) {
212 return <<<EOF
213 <div class="container">
214 <h1>$title</h1>
215 </div>
216 EOF;
217 }
218
211 $content = ''; 219 $content = '';
212 if ($this->debug) { 220 try {
213 try { 221 $count = \count($exception->getAllPrevious());
214 $count = count($exception->getAllPrevious()); 222 $total = $count + 1;
215 $total = $count + 1; 223 foreach ($exception->toArray() as $position => $e) {
216 foreach ($exception->toArray() as $position => $e) { 224 $ind = $count - $position + 1;
217 $ind = $count - $position + 1; 225 $class = $this->formatClass($e['class']);
218 $class = $this->formatClass($e['class']); 226 $message = nl2br($this->escapeHtml($e['message']));
219 $message = nl2br($this->escapeHtml($e['message'])); 227 $content .= sprintf(<<<'EOF'
220 $content .= sprintf(<<<'EOF' 228 <div class="trace trace-as-html">
221 <div class="trace trace-as-html"> 229 <table class="trace-details">
222 <table class="trace-details"> 230 <thead class="trace-head"><tr><th>
223 <thead class="trace-head"><tr><th> 231 <h3 class="trace-class">
224 <h3 class="trace-class"> 232 <span class="text-muted">(%d/%d)</span>
225 <span class="text-muted">(%d/%d)</span> 233 <span class="exception_title">%s</span>
226 <span class="exception_title">%s</span> 234 </h3>
227 </h3> 235 <p class="break-long-words trace-message">%s</p>
228 <p class="break-long-words trace-message">%s</p> 236 </th></tr></thead>
229 </th></tr></thead> 237 <tbody>
230 <tbody>
231 EOF 238 EOF
232 , $ind, $total, $class, $message); 239 , $ind, $total, $class, $message);
233 foreach ($e['trace'] as $trace) { 240 foreach ($e['trace'] as $trace) {
234 $content .= '<tr><td>'; 241 $content .= '<tr><td>';
235 if ($trace['function']) { 242 if ($trace['function']) {
236 $content .= sprintf('at <span class="trace-class">%s</span><span class="trace-type">%s</span><span class="trace-method">%s</span>(<span class="trace-arguments">%s</span>)', $this->formatClass($trace['class']), $trace['type'], $trace['function'], $this->formatArgs($trace['args'])); 243 $content .= sprintf('at <span class="trace-class">%s</span><span class="trace-type">%s</span><span class="trace-method">%s</span>(<span class="trace-arguments">%s</span>)', $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 .= "</td></tr>\n";
242 } 244 }
243 245 if (isset($trace['file']) && isset($trace['line'])) {
244 $content .= "</tbody>\n</table>\n</div>\n"; 246 $content .= $this->formatPath($trace['file'], $trace['line']);
247 }
248 $content .= "</td></tr>\n";
245 } 249 }
246 } catch (\Exception $e) { 250
247 // something nasty happened and we cannot throw an exception anymore 251 $content .= "</tbody>\n</table>\n</div>\n";
248 if ($this->debug) { 252 }
249 $title = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $this->escapeHtml($e->getMessage())); 253 } catch (\Exception $e) {
250 } else { 254 // something nasty happened and we cannot throw an exception anymore
251 $title = 'Whoops, looks like something went wrong.'; 255 if ($this->debug) {
252 } 256 $title = sprintf('Exception thrown when handling an exception (%s: %s)', \get_class($e), $this->escapeHtml($e->getMessage()));
257 } else {
258 $title = 'Whoops, looks like something went wrong.';
253 } 259 }
254 } 260 }
255 261
256 $symfonyGhostImageContents = $this->getSymfonyGhostAsSvg(); 262 $symfonyGhostImageContents = $this->getSymfonyGhostAsSvg();
257 263
276 * 282 *
277 * @return string The stylesheet as a string 283 * @return string The stylesheet as a string
278 */ 284 */
279 public function getStylesheet(FlattenException $exception) 285 public function getStylesheet(FlattenException $exception)
280 { 286 {
287 if (!$this->debug) {
288 return <<<'EOF'
289 body { background-color: #fff; color: #222; font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; margin: 0; }
290 .container { margin: 30px; max-width: 600px; }
291 h1 { color: #dc3545; font-size: 24px; }
292 EOF;
293 }
294
281 return <<<'EOF' 295 return <<<'EOF'
282 body { background-color: #F9F9F9; color: #222; font: 14px/1.4 Helvetica, Arial, sans-serif; margin: 0; padding-bottom: 45px; } 296 body { background-color: #F9F9F9; color: #222; font: 14px/1.4 Helvetica, Arial, sans-serif; margin: 0; padding-bottom: 45px; }
283 297
284 a { cursor: pointer; text-decoration: none; } 298 a { cursor: pointer; text-decoration: none; }
285 a:hover { text-decoration: underline; } 299 a:hover { text-decoration: underline; }
360 if (!$fmt) { 374 if (!$fmt) {
361 return sprintf('<span class="block trace-file-path">in <a title="%s%3$s"><strong>%s</strong>%s</a></span>', $this->escapeHtml($path), $file, 0 < $line ? ' line '.$line : ''); 375 return sprintf('<span class="block trace-file-path">in <a title="%s%3$s"><strong>%s</strong>%s</a></span>', $this->escapeHtml($path), $file, 0 < $line ? ' line '.$line : '');
362 } 376 }
363 377
364 if (\is_string($fmt)) { 378 if (\is_string($fmt)) {
365 $i = strpos($f = $fmt, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: strlen($f); 379 $i = strpos($f = $fmt, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: \strlen($f);
366 $fmt = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE); 380 $fmt = [substr($f, 0, $i)] + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE);
367 381
368 for ($i = 1; isset($fmt[$i]); ++$i) { 382 for ($i = 1; isset($fmt[$i]); ++$i) {
369 if (0 === strpos($path, $k = $fmt[$i++])) { 383 if (0 === strpos($path, $k = $fmt[$i++])) {
370 $path = substr_replace($path, $fmt[$i], 0, strlen($k)); 384 $path = substr_replace($path, $fmt[$i], 0, \strlen($k));
371 break; 385 break;
372 } 386 }
373 } 387 }
374 388
375 $link = strtr($fmt[0], array('%f' => $path, '%l' => $line)); 389 $link = strtr($fmt[0], ['%f' => $path, '%l' => $line]);
376 } else { 390 } else {
377 $link = $fmt->format($path, $line); 391 $link = $fmt->format($path, $line);
378 } 392 }
379 393
380 return sprintf('<span class="block trace-file-path">in <a href="%s" title="Go to source"><strong>%s</string>%s</a></span>', $this->escapeHtml($link), $file, 0 < $line ? ' line '.$line : ''); 394 return sprintf('<span class="block trace-file-path">in <a href="%s" title="Go to source"><strong>%s</string>%s</a></span>', $this->escapeHtml($link), $file, 0 < $line ? ' line '.$line : '');
387 * 401 *
388 * @return string 402 * @return string
389 */ 403 */
390 private function formatArgs(array $args) 404 private function formatArgs(array $args)
391 { 405 {
392 $result = array(); 406 $result = [];
393 foreach ($args as $key => $item) { 407 foreach ($args as $key => $item) {
394 if ('object' === $item[0]) { 408 if ('object' === $item[0]) {
395 $formattedValue = sprintf('<em>object</em>(%s)', $this->formatClass($item[1])); 409 $formattedValue = sprintf('<em>object</em>(%s)', $this->formatClass($item[1]));
396 } elseif ('array' === $item[0]) { 410 } elseif ('array' === $item[0]) {
397 $formattedValue = sprintf('<em>array</em>(%s)', is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]); 411 $formattedValue = sprintf('<em>array</em>(%s)', \is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
398 } elseif ('null' === $item[0]) { 412 } elseif ('null' === $item[0]) {
399 $formattedValue = '<em>null</em>'; 413 $formattedValue = '<em>null</em>';
400 } elseif ('boolean' === $item[0]) { 414 } elseif ('boolean' === $item[0]) {
401 $formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>'; 415 $formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
402 } elseif ('resource' === $item[0]) { 416 } elseif ('resource' === $item[0]) {
403 $formattedValue = '<em>resource</em>'; 417 $formattedValue = '<em>resource</em>';
404 } else { 418 } else {
405 $formattedValue = str_replace("\n", '', $this->escapeHtml(var_export($item[1], true))); 419 $formattedValue = str_replace("\n", '', $this->escapeHtml(var_export($item[1], true)));
406 } 420 }
407 421
408 $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $this->escapeHtml($key), $formattedValue); 422 $result[] = \is_int($key) ? $formattedValue : sprintf("'%s' => %s", $this->escapeHtml($key), $formattedValue);
409 } 423 }
410 424
411 return implode(', ', $result); 425 return implode(', ', $result);
412 } 426 }
413 427