Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ExceptionHandler.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\Debug;
013
014 use Symfony\Component\HttpFoundation\Response;
015 use Symfony\Component\Debug\Exception\FlattenException;
016
017 if (!defined('ENT_SUBSTITUTE')) {
018 define('ENT_SUBSTITUTE', 8);
019 }
020
021 /**
022 * ExceptionHandler converts an exception to a Response object.
023 *
024 * It is mostly useful in debug mode to replace the default PHP/XDebug
025 * output with something prettier and more useful.
026 *
027 * As this class is mainly used during Kernel boot, where nothing is yet
028 * available, the Response content is always HTML.
029 *
030 * @author Fabien Potencier <fabien@symfony.com>
031 */
032 class ExceptionHandler
033 {
034 private $debug;
035 private $charset;
036
037 public function __construct($debug = true, $charset = 'UTF-8')
038 {
039 $this->debug = $debug;
040 $this->charset = $charset;
041 }
042
043 /**
044 * Registers the exception handler.
045 *
046 * @param bool $debug
047 *
048 * @return ExceptionHandler The registered exception handler
049 */
050 public static function register($debug = true)
051 {
052 $handler = new static($debug);
053
054 set_exception_handler(array($handler, 'handle'));
055
056 return $handler;
057 }
058
059 /**
060 * Sends a response for the given Exception.
061 *
062 * If you have the Symfony HttpFoundation component installed,
063 * this method will use it to create and send the response. If not,
064 * it will fallback to plain PHP functions.
065 *
066 * @param \Exception $exception An \Exception instance
067 *
068 * @see sendPhpResponse
069 * @see createResponse
070 */
071 public function handle(\Exception $exception)
072 {
073 if (class_exists('Symfony\Component\HttpFoundation\Response')) {
074 $this->createResponse($exception)->send();
075 } else {
076 $this->sendPhpResponse($exception);
077 }
078 }
079
080 /**
081 * Sends the error associated with the given Exception as a plain PHP response.
082 *
083 * This method uses plain PHP functions like header() and echo to output
084 * the response.
085 *
086 * @param \Exception|FlattenException $exception An \Exception instance
087 */
088 public function sendPhpResponse($exception)
089 {
090 if (!$exception instanceof FlattenException) {
091 $exception = FlattenException::create($exception);
092 }
093
094 if (!headers_sent()) {
095 header(sprintf('HTTP/1.0 %s', $exception->getStatusCode()));
096 foreach ($exception->getHeaders() as $name => $value) {
097 header($name.': '.$value, false);
098 }
099 }
100
101 echo $this->decorate($this->getContent($exception), $this->getStylesheet($exception));
102 }
103
104 /**
105 * Creates the error Response associated with the given Exception.
106 *
107 * @param \Exception|FlattenException $exception An \Exception instance
108 *
109 * @return Response A Response instance
110 */
111 public function createResponse($exception)
112 {
113 if (!$exception instanceof FlattenException) {
114 $exception = FlattenException::create($exception);
115 }
116
117 return new Response($this->decorate($this->getContent($exception), $this->getStylesheet($exception)), $exception->getStatusCode(), $exception->getHeaders());
118 }
119
120 /**
121 * Gets the HTML content associated with the given exception.
122 *
123 * @param FlattenException $exception A FlattenException instance
124 *
125 * @return string The content as a string
126 */
127 public function getContent(FlattenException $exception)
128 {
129 switch ($exception->getStatusCode()) {
130 case 404:
131 $title = 'Sorry, the page you are looking for could not be found.';
132 break;
133 default:
134 $title = 'Whoops, looks like something went wrong.';
135 }
136
137 $content = '';
138 if ($this->debug) {
139 try {
140 $count = count($exception->getAllPrevious());
141 $total = $count + 1;
142 foreach ($exception->toArray() as $position => $e) {
143 $ind = $count - $position + 1;
144 $class = $this->abbrClass($e['class']);
145 $message = nl2br($e['message']);
146 $content .= sprintf(<<<EOF
147 <div class="block_exception clear_fix">
148 <h2><span>%d/%d</span> %s: %s</h2>
149 </div>
150 <div class="block">
151 <ol class="traces list_exception">
152
153 EOF
154 , $ind, $total, $class, $message);
155 foreach ($e['trace'] as $trace) {
156 $content .= ' <li>';
157 if ($trace['function']) {
158 $content .= sprintf('at %s%s%s(%s)', $this->abbrClass($trace['class']), $trace['type'], $trace['function'], $this->formatArgs($trace['args']));
159 }
160 if (isset($trace['file']) && isset($trace['line'])) {
161 if ($linkFormat = ini_get('xdebug.file_link_format')) {
162 $link = str_replace(array('%f', '%l'), array($trace['file'], $trace['line']), $linkFormat);
163 $content .= sprintf(' in <a href="%s" title="Go to source">%s line %s</a>', $link, $trace['file'], $trace['line']);
164 } else {
165 $content .= sprintf(' in %s line %s', $trace['file'], $trace['line']);
166 }
167 }
168 $content .= "</li>\n";
169 }
170
171 $content .= " </ol>\n</div>\n";
172 }
173 } catch (\Exception $e) {
174 // something nasty happened and we cannot throw an exception anymore
175 if ($this->debug) {
176 $title = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($exception), $exception->getMessage());
177 } else {
178 $title = 'Whoops, looks like something went wrong.';
179 }
180 }
181 }
182
183 return <<<EOF
184 <div id="sf-resetcontent" class="sf-reset">
185 <h1>$title</h1>
186 $content
187 </div>
188 EOF;
189
190 }
191
192 /**
193 * Gets the stylesheet associated with the given exception.
194 *
195 * @param FlattenException $exception A FlattenException instance
196 *
197 * @return string The stylesheet as a string
198 */
199 public function getStylesheet(FlattenException $exception)
200 {
201 return <<<EOF
202 .sf-reset { font: 11px Verdana, Arial, sans-serif; color: #333 }
203 .sf-reset .clear { clear:both; height:0; font-size:0; line-height:0; }
204 .sf-reset .clear_fix:after { display:block; height:0; clear:both; visibility:hidden; }
205 .sf-reset .clear_fix { display:inline-block; }
206 .sf-reset * html .clear_fix { height:1%; }
207 .sf-reset .clear_fix { display:block; }
208 .sf-reset, .sf-reset .block { margin: auto }
209 .sf-reset abbr { border-bottom: 1px dotted #000; cursor: help; }
210 .sf-reset p { font-size:14px; line-height:20px; color:#868686; padding-bottom:20px }
211 .sf-reset strong { font-weight:bold; }
212 .sf-reset a { color:#6c6159; }
213 .sf-reset a img { border:none; }
214 .sf-reset a:hover { text-decoration:underline; }
215 .sf-reset em { font-style:italic; }
216 .sf-reset h1, .sf-reset h2 { font: 20px Georgia, "Times New Roman", Times, serif }
217 .sf-reset h2 span { background-color: #fff; color: #333; padding: 6px; float: left; margin-right: 10px; }
218 .sf-reset .traces li { font-size:12px; padding: 2px 4px; list-style-type:decimal; margin-left:20px; }
219 .sf-reset .block { background-color:#FFFFFF; padding:10px 28px; margin-bottom:20px;
220 -webkit-border-bottom-right-radius: 16px;
221 -webkit-border-bottom-left-radius: 16px;
222 -moz-border-radius-bottomright: 16px;
223 -moz-border-radius-bottomleft: 16px;
224 border-bottom-right-radius: 16px;
225 border-bottom-left-radius: 16px;
226 border-bottom:1px solid #ccc;
227 border-right:1px solid #ccc;
228 border-left:1px solid #ccc;
229 }
230 .sf-reset .block_exception { background-color:#ddd; color: #333; padding:20px;
231 -webkit-border-top-left-radius: 16px;
232 -webkit-border-top-right-radius: 16px;
233 -moz-border-radius-topleft: 16px;
234 -moz-border-radius-topright: 16px;
235 border-top-left-radius: 16px;
236 border-top-right-radius: 16px;
237 border-top:1px solid #ccc;
238 border-right:1px solid #ccc;
239 border-left:1px solid #ccc;
240 overflow: hidden;
241 word-wrap: break-word;
242 }
243 .sf-reset li a { background:none; color:#868686; text-decoration:none; }
244 .sf-reset li a:hover { background:none; color:#313131; text-decoration:underline; }
245 .sf-reset ol { padding: 10px 0; }
246 .sf-reset h1 { background-color:#FFFFFF; padding: 15px 28px; margin-bottom: 20px;
247 -webkit-border-radius: 10px;
248 -moz-border-radius: 10px;
249 border-radius: 10px;
250 border: 1px solid #ccc;
251 }
252 EOF;
253
254 }
255
256 private function decorate($content, $css)
257 {
258 return <<<EOF
259 <!DOCTYPE html>
260 <html>
261 <head>
262 <meta charset="UTF-8" />
263 <meta name="robots" content="noindex,nofollow" />
264 <style>
265 /* Copyright (c) 2010, Yahoo! Inc. All rights reserved. Code licensed under the BSD License: http://developer.yahoo.com/yui/license.html */
266 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;}
267
268 html { background: #eee; padding: 10px }
269 img { border: 0; }
270 #sf-resetcontent { width:970px; margin:0 auto; }
271 $css
272 </style>
273 </head>
274 <body>
275 $content
276 </body>
277 </html>
278 EOF;
279
280 }
281
282 private function abbrClass($class)
283 {
284 $parts = explode('\\', $class);
285
286 return sprintf("<abbr title=\"%s\">%s</abbr>", $class, array_pop($parts));
287 }
288
289 /**
290 * Formats an array as a string.
291 *
292 * @param array $args The argument array
293 *
294 * @return string
295 */
296 private function formatArgs(array $args)
297 {
298 $result = array();
299 foreach ($args as $key => $item) {
300 if ('object' === $item[0]) {
301 $formattedValue = sprintf("<em>object</em>(%s)", $this->abbrClass($item[1]));
302 } elseif ('array' === $item[0]) {
303 $formattedValue = sprintf("<em>array</em>(%s)", is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
304 } elseif ('string' === $item[0]) {
305 $formattedValue = sprintf("'%s'", htmlspecialchars($item[1], ENT_QUOTES | ENT_SUBSTITUTE, $this->charset));
306 } elseif ('null' === $item[0]) {
307 $formattedValue = '<em>null</em>';
308 } elseif ('boolean' === $item[0]) {
309 $formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
310 } elseif ('resource' === $item[0]) {
311 $formattedValue = '<em>resource</em>';
312 } else {
313 $formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], ENT_QUOTES | ENT_SUBSTITUTE, $this->charset), true));
314 }
315
316 $result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
317 }
318
319 return implode(', ', $result);
320 }
321 }
322