forked from FriendsOfSymfony1/symfony1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfException.class.php
419 lines (358 loc) · 13.7 KB
/
sfException.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <[email protected]>
* (c) 2004-2006 Sean Kerr <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfException is the base class for all symfony related throwables and
* provides an additional method for printing up a detailed view of an
* throwable.
*
* @author Fabien Potencier <[email protected]>
* @author Sean Kerr <[email protected]>
*/
class sfException extends Exception
{
/** @var Exception|Throwable|null */
protected $wrappedException;
protected static $lastException;
/**
* Wraps an Throwable.
*
* @param Exception|Throwable $e An Throwable instance
*
* @return sfException An sfException instance that wraps the given Throwable object
*/
public static function createFromException($e)
{
$exception = new sfException(sprintf('Wrapped %s: %s', get_class($e), $e->getMessage()));
$exception->setWrappedException($e);
self::$lastException = $e;
return $exception;
}
/**
* Sets the wrapped exception.
*
* @param Exception|Throwable $e A Throwable instance
*/
public function setWrappedException($e)
{
$this->wrappedException = $e;
self::$lastException = $e;
}
/**
* Gets the last wrapped throwable.
*
* @return Exception|Throwable An Throwable instance
*/
public static function getLastException()
{
return self::$lastException;
}
/**
* Clears the $lastException property (added for #6342).
*/
public static function clearLastException()
{
self::$lastException = null;
}
/**
* Prints the stack trace for this exception.
*/
public function printStackTrace()
{
if (null === $this->wrappedException) {
$this->setWrappedException($this);
}
$exception = $this->wrappedException;
if (!sfConfig::get('sf_test')) {
// log all exceptions in php log
error_log($exception->getMessage());
// clean current output buffer
while (ob_get_level()) {
if (!ob_end_clean()) {
break;
}
}
if (sfConfig::get('sf_compressed')) {
ob_start('ob_gzhandler');
}
header('HTTP/1.0 500 Internal Server Error');
}
if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
try {
$this->outputStackTrace($exception);
} catch (Throwable $e) {
}
} else {
try {
$this->outputStackTrace($exception);
} catch (Exception $e) {
}
}
if (!sfConfig::get('sf_test')) {
exit(1);
}
}
/**
* Returns the path for the template error message.
*
* @param string $format The request format
* @param bool $debug Whether to return a template for the debug mode or not
*
* @return bool|string false if the template cannot be found for the given format,
* the absolute path to the template otherwise
*/
public static function getTemplatePathForError($format, $debug)
{
$templatePaths = [
sfConfig::get('sf_app_config_dir').'/error',
sfConfig::get('sf_config_dir').'/error',
__DIR__.'/data',
];
$template = sprintf('%s.%s.php', $debug ? 'exception' : 'error', $format);
foreach ($templatePaths as $path) {
if (null !== $path && is_readable($file = $path.'/'.$template)) {
return $file;
}
}
return false;
}
/**
* Gets the stack trace for this exception.
*
* @param Exception|Throwable $exception
*/
protected static function outputStackTrace($exception)
{
$format = 'html';
$code = '500';
$text = 'Internal Server Error';
$response = null;
if (class_exists('sfContext', false) && sfContext::hasInstance() && is_object($request = sfContext::getInstance()->getRequest()) && is_object($response = sfContext::getInstance()->getResponse())) {
/** @var sfWebRequest $request */
/** @var sfWebResponse $response */
$dispatcher = sfContext::getInstance()->getEventDispatcher();
if (sfConfig::get('sf_logging_enabled')) {
$priority = $exception instanceof sfError404Exception ? sfLogger::ERR : sfLogger::CRIT;
$dispatcher->notify(new sfEvent($exception, 'application.log', [$exception->getMessage(), 'priority' => $priority]));
}
$event = $dispatcher->notifyUntil(new sfEvent($exception, 'application.throw_exception'));
if ($event->isProcessed()) {
return;
}
if ($response->getStatusCode() < 300) {
// status code has already been sent, but is included here for the purpose of testing
$response->setStatusCode(500);
}
$response->setContentType('text/html');
if (!sfConfig::get('sf_test')) {
foreach ($response->getHttpHeaders() as $name => $value) {
header($name.': '.$value);
}
}
$code = $response->getStatusCode();
$text = $response->getStatusText();
$format = $request->getRequestFormat();
if (!$format) {
$format = 'html';
}
if ($mimeType = $request->getMimeType($format)) {
$response->setContentType($mimeType);
}
} else {
// a backward compatible default
if (!sfConfig::get('sf_test')) {
header('Content-Type: text/html; charset='.sfConfig::get('sf_charset', 'utf-8'));
}
}
// send an error 500 if not in debug mode
if (!sfConfig::get('sf_debug')) {
if ($template = self::getTemplatePathForError($format, false)) {
include $template;
return;
}
}
// when using CLI, we force the format to be TXT. Compare exactly to
// the string 'cli' because the php 5.4 server is identified by 'cli-server'
if ('cli' == PHP_SAPI) {
$format = 'txt';
}
$message = null === $exception->getMessage() ? 'n/a' : $exception->getMessage();
$name = get_class($exception);
$traces = self::getTraces($exception, $format);
// dump main objects values
$sf_settings = '';
$settingsTable = $requestTable = $responseTable = $globalsTable = $userTable = '';
if (class_exists('sfContext', false) && sfContext::hasInstance()) {
$context = sfContext::getInstance();
$settingsTable = self::formatArrayAsHtml(sfDebug::settingsAsArray());
$requestTable = self::formatArrayAsHtml(sfDebug::requestAsArray($context->getRequest()));
$responseTable = self::formatArrayAsHtml(sfDebug::responseAsArray($context->getResponse()));
$userTable = self::formatArrayAsHtml(sfDebug::userAsArray($context->getUser()));
$globalsTable = self::formatArrayAsHtml(sfDebug::globalsAsArray());
}
if (isset($response) && $response) {
$response->sendHttpHeaders();
}
if ($template = self::getTemplatePathForError($format, true)) {
if (isset($dispatcher)) {
ob_start();
include $template;
$content = ob_get_clean();
$event = $dispatcher->filter(new sfEvent($response, 'response.filter_content'), $content);
echo $event->getReturnValue();
} else {
include $template;
}
return;
}
}
/**
* Returns an array of exception traces.
*
* @param Exception|Throwable $exception An Throwable implementation instance
* @param string $format The trace format (txt or html)
*
* @return array An array of traces
*/
protected static function getTraces($exception, $format = 'txt')
{
$traceData = $exception->getTrace();
array_unshift($traceData, [
'function' => '',
'file' => null != $exception->getFile() ? $exception->getFile() : null,
'line' => null != $exception->getLine() ? $exception->getLine() : null,
'args' => [],
]);
$traces = [];
if ('html' == $format) {
$lineFormat = 'at <strong>%s%s%s</strong>(%s)<br />in <em>%s</em> line %s <a href="#" onclick="toggle(\'%s\'); return false;">...</a><br /><ul class="code" id="%s" style="display: %s">%s</ul>';
} else {
$lineFormat = 'at %s%s%s(%s) in %s line %s';
}
for ($i = 0, $count = count($traceData); $i < $count; ++$i) {
$line = isset($traceData[$i]['line']) ? $traceData[$i]['line'] : null;
$file = isset($traceData[$i]['file']) ? $traceData[$i]['file'] : null;
$args = isset($traceData[$i]['args']) ? $traceData[$i]['args'] : [];
$traces[] = sprintf(
$lineFormat,
isset($traceData[$i]['class']) ? $traceData[$i]['class'] : '',
isset($traceData[$i]['type']) ? $traceData[$i]['type'] : '',
$traceData[$i]['function'],
self::formatArgs($args, false, $format),
self::formatFile($file, $line, $format, null === $file ? 'n/a' : sfDebug::shortenFilePath($file)),
null === $line ? 'n/a' : $line,
'trace_'.$i,
'trace_'.$i,
0 == $i ? 'block' : 'none',
self::fileExcerpt($file, $line)
);
}
return $traces;
}
/**
* Returns an HTML version of an array as YAML.
*
* @param array $values The values array
*
* @return string An HTML string
*/
protected static function formatArrayAsHtml($values)
{
return '<pre>'.self::escape(@sfYaml::dump($values)).'</pre>';
}
/**
* Returns an excerpt of a code file around the given line number.
*
* @param string $file A file path
* @param int $line The selected line number
*
* @return string An HTML string
*/
protected static function fileExcerpt($file, $line)
{
// $file can be null for RuntimeException
if (null === $file) {
return '';
}
if (is_readable($file)) {
$replaceRegex = '/^(?:<pre><code(?: [^>]+)?>|<code><span(?: [^>]+)?>\s*)(.*?)(?:<\/code><\/pre>|\s*<\/span>\s*<\/code>)$/s';
$splitRegex = '/(\r\n|\n|\r|<br \/>)/';
$content = preg_split($splitRegex, preg_replace($replaceRegex, '$1', highlight_file($file, true)));
$lines = [];
for ($i = max($line - 3, 1), $max = min($line + 3, count($content)); $i <= $max; ++$i) {
$lines[] = '<li'.($i == $line ? ' class="selected"' : '').'>'.$content[$i - 1].'</li>';
}
return '<ol start="'.max($line - 3, 1).'">'.implode("\n", $lines).'</ol>';
}
}
/**
* Formats an array as a string.
*
* @param array $args The argument array
* @param bool $single
* @param string $format The format string (html or txt)
*
* @return string
*/
protected static function formatArgs($args, $single = false, $format = 'html')
{
$result = [];
$single and $args = [$args];
foreach ($args as $key => $value) {
if (is_object($value)) {
$formattedValue = ('html' == $format ? '<em>object</em>' : 'object').sprintf("('%s')", get_class($value));
} elseif (is_array($value)) {
$formattedValue = ('html' == $format ? '<em>array</em>' : 'array').sprintf('(%s)', self::formatArgs($value));
} elseif (is_string($value)) {
$formattedValue = ('html' == $format ? sprintf("'%s'", self::escape($value)) : "'{$value}'");
} elseif (null === $value) {
$formattedValue = ('html' == $format ? '<em>null</em>' : 'null');
} else {
$formattedValue = $value;
}
$result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", self::escape($key), $formattedValue);
}
return implode(', ', $result);
}
/**
* Formats a file path.
*
* @param string $file An absolute file path
* @param int $line The line number
* @param string $format The output format (txt or html)
* @param string $text Use this text for the link rather than the file path
*
* @return string
*/
protected static function formatFile($file, $line, $format = 'html', $text = null)
{
if (null === $text) {
$text = $file;
}
if ('html' == $format && $file && $line && $linkFormat = sfConfig::get('sf_file_link_format', ini_get('xdebug.file_link_format'))) {
$link = strtr($linkFormat, ['%f' => $file, '%l' => $line]);
$text = sprintf('<a href="%s" title="Click to open this file" class="file_link">%s</a>', $link, $text);
}
return $text;
}
/**
* Escapes a string value with html entities.
*
* @param string $value
*
* @return string
*/
protected static function escape($value)
{
if (!is_string($value)) {
return $value;
}
return htmlspecialchars($value, ENT_QUOTES, sfConfig::get('sf_charset', 'UTF-8'));
}
}