|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPClassic; |
| 4 | + |
| 5 | +use Exception; |
| 6 | + |
| 7 | +/** |
| 8 | + * This class allows you to use override default php exception handler |
| 9 | + * |
| 10 | + * @author Jonas Sciangula Street <joni2back {at} gmail.com> |
| 11 | + * @todo Method to set view template and remove html code from draw() method |
| 12 | + * @todo Avoid the use of highlight_string() to make php syntax highlight |
| 13 | + */ |
| 14 | + |
| 15 | +abstract class ExceptionCatcher |
| 16 | +{ |
| 17 | + |
| 18 | + /** |
| 19 | + * @param Exception $oExp |
| 20 | + */ |
| 21 | + public static function handle(Exception $oExp) |
| 22 | + { |
| 23 | + echo static::draw($oExp); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @param Exception $oExp |
| 28 | + * @return string |
| 29 | + */ |
| 30 | + public static function draw(Exception $oExp) |
| 31 | + { |
| 32 | + $details = static::buildDetails($oExp); |
| 33 | + $content = sprintf('<h3>Exception: %s</h3>', $oExp->getMessage()); |
| 34 | + $content .= sprintf('<h5>At: %s:%s</h5>', $oExp->getFile(), $oExp->getLine()); |
| 35 | + $content .= sprintf('<h4>Trace</h4>'); |
| 36 | + foreach ($details as $items) { |
| 37 | + if (isset($items['trace'])) { |
| 38 | + $content .= sprintf("<hr />%s:%d<br />\n", $items['file'], $items['line']); |
| 39 | + $content .= static::getHighlightedCode($items['trace'], true); |
| 40 | + } |
| 41 | + } |
| 42 | + return $content; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param array $trace |
| 47 | + * @return string |
| 48 | + */ |
| 49 | + protected function getHighlightedCode(array $trace, $showLineNumber = false) |
| 50 | + { |
| 51 | + $content = ''; |
| 52 | + foreach ($trace as $line) { |
| 53 | + if (! isset($line['lineContent'])) { |
| 54 | + return; |
| 55 | + } |
| 56 | + $content .= $showLineNumber ? |
| 57 | + sprintf("%d %s\n", $line['lineNumber'], $line['lineContent']) : |
| 58 | + sprintf("%s\n", $line['lineContent']); |
| 59 | + } |
| 60 | + $str = highlight_string('<?php '. $content, true); |
| 61 | + return str_replace('<?php ', '', $str); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param Exception $oExp |
| 66 | + * @return array |
| 67 | + */ |
| 68 | + protected function buildDetails(Exception $oExp) |
| 69 | + { |
| 70 | + $arr = array(); |
| 71 | + $arr[] = static::getDetails($oExp->getFile(), $oExp->getLine()); |
| 72 | + foreach ($oExp->getTrace() as $files) { |
| 73 | + $arr[] = static::getDetails($files['file'], $files['line']); |
| 74 | + } |
| 75 | + return $arr; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param string $file |
| 80 | + * @param int $line |
| 81 | + * @return array |
| 82 | + */ |
| 83 | + protected function getDetails($file, $line) |
| 84 | + { |
| 85 | + $lines = file_get_contents($file); //validate |
| 86 | + $lines = explode("\n", $lines); |
| 87 | + $trace = array(); |
| 88 | + foreach (range($line - 4, $line + 2) as $fetchLine) { |
| 89 | + if (isset($lines[$fetchLine])) { |
| 90 | + $n = strlen($fetchLine + 10); |
| 91 | + $lineKey = sprintf('%0'.$n.'d', $fetchLine + 1); |
| 92 | + $trace[$lineKey] = array( |
| 93 | + 'lineContent' => $lines[$fetchLine], |
| 94 | + 'lineNumber' => $lineKey |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + return array( |
| 99 | + 'file' => $file, |
| 100 | + 'line' => $line, |
| 101 | + 'trace' => $trace, |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return void |
| 107 | + */ |
| 108 | + public static function register() |
| 109 | + { |
| 110 | + set_exception_handler(array(__CLASS__, 'handle')); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return void |
| 115 | + */ |
| 116 | + public static function unregister() |
| 117 | + { |
| 118 | + restore_exception_handler(); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | + |
0 commit comments