Exception.php

Go to the documentation of this file.
00001 <?php 00002 00013 class Exception 00014 { 00016 var $code; 00017 var $message; 00018 var $context; 00019 00020 var $class; 00021 var $file; 00022 var $line; 00023 var $method; 00024 00025 // debug backtrace 00026 var $backtrace; 00027 00034 function Exception($code, $message, $backtrace = null) 00035 { 00036 $this->code = $code; 00037 $this->message = $message; 00038 $this->backtrace = debug_backtrace(); 00039 00040 $bc = $this->backtrace[count($this->backtrace)-1]; 00041 $this->file = @$bc['file']; 00042 $this->line = @$bc['line']; 00043 $this->class = @$bc['class']; 00044 $this->method= @$bc['function']; 00045 00046 trigger_error($this->toString(), E_USER_NOTICE); 00047 } 00048 00049 function getMessage() 00050 { 00051 return $this->message; 00052 } 00053 00054 function getCode() 00055 { 00056 return $this->code; 00057 } 00058 00066 function toString() 00067 { 00068 $result = "[Exception, message=\"" . $this->message . "\", " . 00069 "code=\"" . $this->code. "\", " . 00070 "file=\"" . $this->file . "\", " . 00071 "line=\"" . $this->line . "\"]"; 00072 return $result; 00073 } 00074 00079 function getFile() 00080 { 00081 return $this->file; 00082 } 00083 00084 function getLine() 00085 { 00086 return $this->line; 00087 } 00088 00089 function getContext($html = false) 00090 { 00091 $str = ($html ? "<h3>[Context]</h3>\n" : "[Context]\n"); 00092 00093 if (! file_exists($this->file)) { 00094 $str .= "Context cannot be shown - ($this->file) does not exist\n"; 00095 return $str; 00096 } 00097 if ((! is_int($this->line)) || ($this->line <= 0)) { 00098 $str .= "Context cannot be shown - ($this->line) is an invalid line number"; 00099 return $str; 00100 } 00101 00102 $lines = file($this->file); 00103 // get the source ## core dump in windows, scrap colour highlighting :-( 00104 // $source = highlight_file($this->file, true); 00105 // $this->lines = split("<br />", $source); 00106 // get line numbers 00107 $start = $this->line - 6; // -1 including error line 00108 $finish = $this->line + 5; 00109 // get lines 00110 if ($start < 0) { 00111 $start = 0; 00112 } 00113 if ($start >= count($lines)) { 00114 $start = count($lines) -1; 00115 } 00116 for ($i = $start; $i < $finish; $i++) { 00117 // highlight line in question 00118 if ($i == ($this->line -1)) { 00119 $context_lines[] = '<font color="red"><b>' . ($i + 1) . 00120 "\t" . strip_tags($lines[$this->line -1]) . '</b></font>'; 00121 } else { 00122 $context_lines[] = '<font color="black"><b>' . ($i + 1) . 00123 "</b></font>\t" . @$lines[$i]; 00124 } 00125 } 00126 00127 $str .= trim(join("<br />\n", $context_lines)) . "<br />\n"; 00128 return $str; 00129 } 00130 00131 function getBacktrace($html = false) 00132 { 00133 $str = ($html ? "<h3>[Backtrace]</h3>\n" : "[Backtrace]\n"); 00134 00135 foreach($this->backtrace as $bc) 00136 { 00137 if (isset($bc['class'])) { 00138 $s = ($html ? "<b>%s</b>" : "%s") . "::"; 00139 $str .= sprintf($s, $bc['class']); 00140 } 00141 if (isset($bc['function'])) { 00142 $s = ($html ? "<b>%s</b>" : "%s"); 00143 $str .= sprintf($s, $bc['function']); 00144 } 00145 00146 $str .= ' ('; 00147 00148 if (isset($bc['args'])) 00149 { 00150 foreach($bc['args'] as $arg) 00151 { 00152 $s = ($html ? "<i>%s</i>, " : "%s, "); 00153 $str .= sprintf($s, gettype($arg)); 00154 } 00155 $str = substr($str, 0, -2); 00156 } 00157 00158 $str .= ')'; 00159 $str .= ': '; 00160 $str .= '[ '; 00161 if (isset($bc['file'])) { 00162 $dir = substr(dirname($bc['file']), strrpos(dirname($bc['file']), '/') + 1); 00163 $file = basename($bc['file']); 00164 if ($html) $str .= "<a href=\"file:/" . $bc['file'] . "\">"; 00165 $str .= $dir . '/' . $file; 00166 if ($html) $str .= "</a>"; 00167 } 00168 $str .= isset($bc['line']) ? ', ' . $bc['line'] : ''; 00169 $str .= ' ] '; 00170 $str .= ($html ? "<br />\n" : "\n"); 00171 } 00172 00173 return $str; 00174 } 00175 00176 } 00177

This file is part of the Creole[php4] library.


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS