|
0
|
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\Debug; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\HttpFoundation\Response; |
|
|
15 |
use Symfony\Component\HttpKernel\Exception\FlattenException; |
|
|
16 |
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; |
|
|
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 |
*/ |
|
|
29 |
class ExceptionHandler |
|
|
30 |
{ |
|
|
31 |
private $debug; |
|
|
32 |
|
|
|
33 |
public function __construct($debug = true) |
|
|
34 |
{ |
|
|
35 |
$this->debug = $debug; |
|
|
36 |
} |
|
|
37 |
|
|
|
38 |
/** |
|
|
39 |
* Register the exception handler. |
|
|
40 |
* |
|
|
41 |
* @return The registered exception handler |
|
|
42 |
*/ |
|
|
43 |
static public function register($debug = true) |
|
|
44 |
{ |
|
|
45 |
$handler = new static($debug); |
|
|
46 |
|
|
|
47 |
set_exception_handler(array($handler, 'handle')); |
|
|
48 |
|
|
|
49 |
return $handler; |
|
|
50 |
} |
|
|
51 |
|
|
|
52 |
/** |
|
|
53 |
* Sends a Response for the given Exception. |
|
|
54 |
* |
|
|
55 |
* @param \Exception $exception An \Exception instance |
|
|
56 |
*/ |
|
|
57 |
public function handle(\Exception $exception) |
|
|
58 |
{ |
|
|
59 |
$this->createResponse($exception)->send(); |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
/** |
|
|
63 |
* Creates the error Response associated with the given Exception. |
|
|
64 |
* |
|
|
65 |
* @param \Exception $exception An \Exception instance |
|
|
66 |
* |
|
|
67 |
* @return Response A Response instance |
|
|
68 |
*/ |
|
|
69 |
public function createResponse(\Exception $exception) |
|
|
70 |
{ |
|
|
71 |
$content = ''; |
|
|
72 |
$title = ''; |
|
|
73 |
try { |
|
|
74 |
$code = $exception instanceof HttpExceptionInterface ? $exception->getStatusCode() : 500; |
|
|
75 |
$exception = FlattenException::create($exception); |
|
|
76 |
|
|
|
77 |
switch($code) { |
|
|
78 |
case 404: |
|
|
79 |
$title = 'Sorry, the page you are looking for could not be found.'; |
|
|
80 |
break; |
|
|
81 |
default: |
|
|
82 |
$title = 'Whoops, looks like something went wrong.'; |
|
|
83 |
} |
|
|
84 |
|
|
|
85 |
if ($this->debug) { |
|
|
86 |
$content = $this->getContent($exception); |
|
|
87 |
} |
|
|
88 |
} catch (\Exception $e) { |
|
|
89 |
// something nasty happened and we cannot throw an exception here anymore |
|
|
90 |
if ($this->debug) { |
|
|
91 |
$title = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($exception), $exception->getMessage()); |
|
|
92 |
} else { |
|
|
93 |
$title = 'Whoops, looks like something went wrong.'; |
|
|
94 |
} |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
return new Response($this->decorate($content, $title), $code); |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
private function getContent($exception) |
|
|
101 |
{ |
|
|
102 |
$message = nl2br($exception->getMessage()); |
|
|
103 |
$class = $this->abbrClass($exception->getClass()); |
|
|
104 |
$count = count($exception->getAllPrevious()); |
|
|
105 |
$content = ''; |
|
|
106 |
foreach ($exception->toArray() as $position => $e) { |
|
|
107 |
$ind = $count - $position + 1; |
|
|
108 |
$total = $count + 1; |
|
|
109 |
$class = $this->abbrClass($e['class']); |
|
|
110 |
$message = nl2br($e['message']); |
|
|
111 |
$content .= "<div class=\"block_exception clear_fix\"><h2><span>$ind/$total</span> $class: $message</h2></div><div class=\"block\"><ol class=\"traces list_exception\">"; |
|
|
112 |
foreach ($e['trace'] as $i => $trace) { |
|
|
113 |
$content .= '<li>'; |
|
|
114 |
if ($trace['function']) { |
|
|
115 |
$content .= sprintf('at %s%s%s()', $this->abbrClass($trace['class']), $trace['type'], $trace['function']); |
|
|
116 |
} |
|
|
117 |
if (isset($trace['file']) && isset($trace['line'])) { |
|
|
118 |
$content .= sprintf(' in %s line %s', $trace['file'], $trace['line']); |
|
|
119 |
} |
|
|
120 |
$content .= '</li>'; |
|
|
121 |
} |
|
|
122 |
|
|
|
123 |
$content .= '</ol></div>'; |
|
|
124 |
} |
|
|
125 |
|
|
|
126 |
return $content; |
|
|
127 |
} |
|
|
128 |
|
|
|
129 |
private function decorate($content, $title) |
|
|
130 |
{ |
|
|
131 |
return <<<EOF |
|
|
132 |
<!DOCTYPE html> |
|
|
133 |
<html> |
|
|
134 |
<head> |
|
|
135 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> |
|
|
136 |
<meta name="robots" content="noindex,nofollow" /> |
|
|
137 |
<title>{$title}</title> |
|
|
138 |
<style> |
|
|
139 |
/* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */ |
|
|
140 |
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;} |
|
|
141 |
|
|
|
142 |
html { background: #eee; padding: 10px } |
|
|
143 |
body { font: 11px Verdana, Arial, sans-serif; color: #333 } |
|
|
144 |
img { border: 0; } |
|
|
145 |
.clear { clear:both; height:0; font-size:0; line-height:0; } |
|
|
146 |
.clear_fix:after { display:block; height:0; clear:both; visibility:hidden; } |
|
|
147 |
.clear_fix { display:inline-block; } |
|
|
148 |
* html .clear_fix { height:1%; } |
|
|
149 |
.clear_fix { display:block; } |
|
|
150 |
#content { width:970px; margin:0 auto; } |
|
|
151 |
.sf-exceptionreset, .sf-exceptionreset .block { margin: auto } |
|
|
152 |
.sf-exceptionreset abbr { border-bottom: 1px dotted #000; cursor: help; } |
|
|
153 |
.sf-exceptionreset p { font-size:14px; line-height:20px; color:#868686; padding-bottom:20px } |
|
|
154 |
.sf-exceptionreset strong { font-weight:bold; } |
|
|
155 |
.sf-exceptionreset a { color:#6c6159; } |
|
|
156 |
.sf-exceptionreset a img { border:none; } |
|
|
157 |
.sf-exceptionreset a:hover { text-decoration:underline; } |
|
|
158 |
.sf-exceptionreset em { font-style:italic; } |
|
|
159 |
.sf-exceptionreset h1, .sf-exceptionreset h2 { font: 20px Georgia, "Times New Roman", Times, serif } |
|
|
160 |
.sf-exceptionreset h2 span { background-color: #fff; color: #333; padding: 6px; float: left; margin-right: 10px; } |
|
|
161 |
.sf-exceptionreset .traces li { font-size:12px; padding: 2px 4px; list-style-type:decimal; margin-left:20px; } |
|
|
162 |
.sf-exceptionreset .block { background-color:#FFFFFF; padding:10px 28px; margin-bottom:20px; |
|
|
163 |
-webkit-border-bottom-right-radius: 16px; |
|
|
164 |
-webkit-border-bottom-left-radius: 16px; |
|
|
165 |
-moz-border-radius-bottomright: 16px; |
|
|
166 |
-moz-border-radius-bottomleft: 16px; |
|
|
167 |
border-bottom-right-radius: 16px; |
|
|
168 |
border-bottom-left-radius: 16px; |
|
|
169 |
border-bottom:1px solid #ccc; |
|
|
170 |
border-right:1px solid #ccc; |
|
|
171 |
border-left:1px solid #ccc; |
|
|
172 |
} |
|
|
173 |
.sf-exceptionreset .block_exception { background-color:#ddd; color: #333; padding:20px; |
|
|
174 |
-webkit-border-top-left-radius: 16px; |
|
|
175 |
-webkit-border-top-right-radius: 16px; |
|
|
176 |
-moz-border-radius-topleft: 16px; |
|
|
177 |
-moz-border-radius-topright: 16px; |
|
|
178 |
border-top-left-radius: 16px; |
|
|
179 |
border-top-right-radius: 16px; |
|
|
180 |
border-top:1px solid #ccc; |
|
|
181 |
border-right:1px solid #ccc; |
|
|
182 |
border-left:1px solid #ccc; |
|
|
183 |
} |
|
|
184 |
.sf-exceptionreset li a { background:none; color:#868686; text-decoration:none; } |
|
|
185 |
.sf-exceptionreset li a:hover { background:none; color:#313131; text-decoration:underline; } |
|
|
186 |
.sf-exceptionreset ol { padding: 10px 0; } |
|
|
187 |
.sf-exceptionreset h1 { background-color:#FFFFFF; padding: 15px 28px; margin-bottom: 20px; |
|
|
188 |
-webkit-border-radius: 10px; |
|
|
189 |
-moz-border-radius: 10px; |
|
|
190 |
border-radius: 10px; |
|
|
191 |
border: 1px solid #ccc; |
|
|
192 |
} |
|
|
193 |
</style> |
|
|
194 |
</head> |
|
|
195 |
<body> |
|
|
196 |
<div id="content" class="sf-exceptionreset"> |
|
|
197 |
<h1>$title</h1> |
|
|
198 |
$content |
|
|
199 |
</div> |
|
|
200 |
</body> |
|
|
201 |
</html> |
|
|
202 |
EOF; |
|
|
203 |
} |
|
|
204 |
|
|
|
205 |
private function abbrClass($class) |
|
|
206 |
{ |
|
|
207 |
$parts = explode('\\', $class); |
|
|
208 |
|
|
|
209 |
return sprintf("<abbr title=\"%s\">%s</abbr>", $class, array_pop($parts)); |
|
|
210 |
} |
|
|
211 |
} |