* @author Piotr Szymanski * @package TinyMVC * @version $Id$ * */ /** * core framework exception class * @package TinyMVC */ class TinyMVCException extends Exception { public function __construct($message, $code = 0) { parent::__construct($message, $code); } /** * display full html exception trace page * @param Exception $exception Exception object (optional) * @return string exception page */ protected function getHtmlTrace(Exception $exception = null) { if(is_object($this) && ($exception == null)) { $exception = $this; } $trace = ''; $trace .= '
'; $trace .= ''; $trace .= '
' . get_class($exception) . ': ' . $exception->getMessage() . '

Thrown by: ' . $exception->getFile() . ' at line: ' . $exception->getLine() . '

'; $backtrace = $exception->getTrace(); if(count($backtrace)) { $html = '
Backtrace:
'; foreach($backtrace as $item) { $html .= '
'; $html .= '
file: ' . $item['file'] . '
'; $html .= '
line: ' . $item['line'] . '
'; $html .= isset($item['function']) ? '
function: ' . $item['function'] . '
' : ''; $html .= isset($item['class']) ? '
class: ' . $item['class'] . '
' : ''; $html .= isset($item['type']) ? '
type: ' . $item['type'] . '
' : ''; if(count($item['args'])) { $html .= '
args:
'; $html .= "
";
					foreach($item['args'] as $key => $agr) {
						$html .= '
[' . $key . '] => ' . print_r($agr, true) . '
'; } $html .= "
"; } $source = $exception->dumpSource($item['file'], $item['line']); if($source) { $html .= '
source:'; $html .= '
'; $html .= $source; $html .= '
'; $html .= '
'; } $html .= '

'; } $trace .= $html; } $trace .= ''; return $trace; } /** * handle exception * @param Exception $exception Exception object (optional) */ public function handleException(Exception $exception = null) { $htmlTrace = $this->getHtmlTrace($exception); header('Content-Type: text/html; charset=utf-8'); print $htmlTrace; } protected function dumpSource($file, $line) { $source = explode("\n", file_get_contents($file, true)); $result = ''; for ($index = $line-4; $index <= $line + 4; $index++) { if(isset($source[$index-1])) { $result .= (($index == $line) ? '$index: "; $result .= trim(str_replace(' ', ' ', htmlentities($source[$index-1]))); $result .= ($index == $line) ? '' : ''; $result .= "
\n"; } } return $result; } }