exceptions.php
11.2 KB
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
<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
namespace
{
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* exceptions
*
* Prints exception/errors backtraces using a pretty visualization
*/
class exceptions
{
/**
* Print the backtrace
*/
protected $_showBackTrace = true;
/**
* Show the application's code
*/
protected $_showFiles = true;
/**
* Show only the related part of the application
*/
protected $_showFileFragment = false;
/**
* CSS theme
*/
protected $_theme = 'exceptions';
/**
* Pretty Exceptions
*/
protected $_uri = '/css/';
/**
* Flag to control that only one exception/error is show at time
*/
static protected $_showActive = false;
/**
* Set if the application's files must be opened an showed as part of the backtrace
*
* @param boolean $showFiles
*/
public function showFiles($showFiles)
{
$this->_showFiles = $showFiles;
}
/**
* Set if only the file fragment related to the exception must be shown instead of the complete file
*
* @param boolean $showFileFragment
*/
public function showFileFragment($showFileFragment)
{
$this->_showFileFragment = $showFileFragment;
}
/**
* Change the base uri for css/javascript sources
*
* @param string $uri
*/
public function setBaseUri($uri)
{
$this->_uri = $uri;
}
/**
* Change the CSS theme
*
* @param string $theme
*/
public function setTheme($theme)
{
$this->_theme = $theme;
}
/**
* Set if the exception/error backtrace must be shown
*
* @param boolean $showBackTrace
*/
public function showBackTrace($showBackTrace)
{
$this->_showBackTrace = $showBackTrace;
}
/**
* Returns the css sources
*
* @return string
*/
public function getCssSources()
{
return '<link href="' . $this->_uri . $this->_theme . '.css" type="text/css" rel="stylesheet" />';
}
/**
* Returns the javascript sources
*
* @return string
*/
public function getJsSources()
{
return '<script type="text/javascript" src="/js/exceptions.js"></script>';
}
/**
* Returns the current framework version
*/
public function getVersion()
{
$version = \Phalcon\Version::get();
$parts = explode(' ', $version);
return '<div class="version">Phalcon Framework <a target="_new" href="http://docs.phalconphp.com/en/' . $parts[0] . '/">' . $version . '</a></div>';
}
protected function _escapeString($value)
{
$value = str_replace("\n", "\\n", $value);
$value = htmlentities($value, ENT_COMPAT, 'utf-8');
return $value;
}
protected function _getArrayDump($argument, $n = 0)
{
if ($n < 3 && count($argument) > 0 && count($argument) < 8) {
$dump = [];
foreach ($argument as $k => $v) {
if (is_scalar($v)) {
if ($v === '') {
$dump[] = $k . ' => (empty string)';
} else {
$dump[] = $k . ' => ' . $this->_escapeString($v);
}
} else {
if (is_array($v)) {
$dump[] = $k . ' => Array(' . $this->_getArrayDump($v, $n + 1) . ')';
continue;
}
if (is_object($v)) {
$dump[] = $k . ' => Object(' . get_class($v) . ')';
continue;
}
if (is_null($v)) {
$dump[] = $k . ' => null';
continue;
}
$dump[] = $k . ' => '.$v;
}
}
return join(', ', $dump);
}
return count($argument);
}
/**
* Shows a backtrace item
*
* @param int $n
* @param array $trace
*/
protected function _showTraceItem($n, $trace)
{
echo '<tr><td align="right" valign="top" class="error-number">#', $n, '</td><td>';
if (isset($trace['class'])) {
if (preg_match('/^Phalcon/', $trace['class'])) {
echo '<span class="error-class"><a target="_new" href="http://docs.phalconphp.com/en/latest/api/', str_replace('\\', '_', $trace['class']), '.html">', $trace['class'], '</a></span>';
} else {
$classReflection = new \ReflectionClass($trace['class']);
if ($classReflection->isInternal()) {
echo '<span class="error-class"><a target="_new" href="http://php.net/manual/en/class.', str_replace('_', '-', strtolower($trace['class'])), '.php">', $trace['class'], '</a></span>';
} else {
echo '<span class="error-class">', $trace['class'], '</span>';
}
}
echo $trace['type'];
}
if (isset($trace['class'])) {
echo '<span class="error-function">', $trace['function'], '</span>';
} else {
if (function_exists($trace['function'])) {
$functionReflection = new \ReflectionFunction($trace['function']);
if ($functionReflection->isInternal()) {
echo '<span class="error-function"><a target="_new" href="http://php.net/manual/en/function.', str_replace('_', '-', $trace['function']), '.php">', $trace['function'], '</a></span>';
} else {
echo '<span class="error-function">', $trace['function'], '</span>';
}
} else {
echo '<span class="error-function">', $trace['function'], '</span>';
}
}
if (isset($trace['args'])) {
$arguments = [];
foreach ($trace['args'] as $argument) {
if (is_scalar($argument)) {
if (is_bool($argument)) {
if ($argument) {
$arguments[] = '<span class="error-parameter">true</span>';
} else {
$arguments[] = '<span class="error-parameter">null</span>';
}
continue;
}
if (is_string($argument)) {
$argument = $this->_escapeString($argument);
}
$arguments[] = '<span class="error-parameter">' . $argument . '</span>';
} else {
if (is_object($argument)) {
if (method_exists($argument, 'dump')) {
$arguments[] = '<span class="error-parameter">Object(' . get_class($argument) . ': ' . $this->_getArrayDump($argument->dump()) . ')</span>';
} else {
$arguments[] = '<span class="error-parameter">Object(' . get_class($argument) . ')</span>';
}
} else {
if (is_array($argument)) {
$arguments[] = '<span class="error-parameter">Array(' . $this->_getArrayDump($argument) . ')</span>';
} else {
if (is_null($argument)) {
$arguments[] = '<span class="error-parameter">null</span>';
continue;
}
}
}
}
}
echo '(' . join(', ', $arguments) . ')';
}
if (isset($trace['file'])) {
echo '<br/><span class="error-file">', $trace['file'], ' (', $trace['line'], ')</span>';
}
echo '</td></tr>';
if ($this->_showFiles) {
if (isset($trace['file'])) {
echo '</table>';
$line = $trace['line'];
$lines = file($trace['file']);
if ($this->_showFileFragment) {
$numberLines = count($lines);
$firstLine = ($line - 7) < 1 ? 1 : $line - 7;
$lastLine = ($line + 5 > $numberLines ? $numberLines : $line + 5);
echo "<pre class='prettyprint highlight:" . $firstLine . ":" . $line . " linenums:" . $firstLine . "'>";
} else {
$firstLine = 1;
$lastLine = count($lines) - 1;
echo "<pre class='prettyprint highlight:" . $firstLine . ":" . $line . " linenums error-scroll'>";
}
for ($i = $firstLine; $i <= $lastLine; ++$i) {
if ($this->_showFileFragment) {
if ($i == $firstLine) {
if (preg_match('#\*\/$#', rtrim($lines[$i - 1]))) {
$lines[$i-1] = str_replace("* /", " ", $lines[$i - 1]);
}
}
}
if ($lines[$i - 1] != PHP_EOL) {
$lines[$i - 1] = str_replace("\t", " ", $lines[$i - 1]);
echo htmlentities($lines[$i - 1], ENT_COMPAT, 'UTF-8');
} else {
echo ' ' . "\n";
}
}
echo '</pre>';
echo '<table cellspacing="0">';
}
}
}
/**
* Handles exceptions
*
* @param Exception $e
* @return boolean
*/
public function handle($e)
{
if (ob_get_level() > 0) {
ob_end_clean();
}
if (self::$_showActive) {
echo $e->getMessage();
return;
}
self::$_showActive = true;
echo '<html><head><title>Exception - ', get_class($e), ': ', $e->getMessage(), '</title>', $this->getCssSources(), '</head><body>';
echo '<div class="error-main">
', get_class($e), ': ', $e->getMessage(), '
<br/><span class="error-file">', $e->getFile(), ' (', $e->getLine(), ')</span>
</div>';
if ($this->_showBackTrace) {
echo '<div class="error-backtrace"><table cellspacing="0">';
foreach ($e->getTrace() as $n => $trace) {
$this->_showTraceItem($n, $trace);
}
echo '</table></div>';
}
echo $this->getVersion();
echo $this->getJsSources() . '</body></html>';
// STOP
die();
self::$_showActive = false;
#return true;
}
/**
* Handles errors/warnings/notices
*
* @param int $errorCode
* @param string $errorMessage
* @param string $errorFile
* @param int $errorLine
*/
public function handleError($errorCode, $errorMessage, $errorFile, $errorLine)
{
if (ob_get_level() > 0) {
ob_end_clean();
}
if (self::$_showActive) {
echo $errorMessage;
return false;
}
if (!(error_reporting() & $errorCode)) {
return false;
}
self::$_showActive = true;
echo '<html><head><title>Exception - ', $errorMessage, '</title>', $this->getCssSources(), '</head><body>';
echo '<div class="error-main">
', $errorMessage, '
<br/><span class="error-file">', $errorFile, ' (', $errorLine, ')</span>
</div>';
if ($this->_showBackTrace) {
echo '<div class="error-backtrace"><table cellspacing="0">';
foreach (debug_backtrace() as $n => $trace) {
if ($n == 0) {
continue;
}
$this->_showTraceItem($n, $trace);
}
echo '</table></div>';
}
echo $this->getVersion();
echo $this->getJsSources() . '</body></html>';
self::$_showActive = false;
// STOP
die();
#return true;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////