1+ <?php
2+ /*
3+ * ErrorHandler - Error and Exception hanlder with syntax highlighting
4+ *
5+ * @package Debug
6+ * @version 1.0
7+ * @link http://github.com/surikat/Debug/
8+ * @author Jo Surikat <jo@surikat.pro>
9+ * @website http://wildsurikat.com
10+ */
11+
12+ namespace Wild \Debug ;
13+ class ErrorHandler{
14+ private static $ errorType ;
15+ private $ handle ;
16+ private $ registeredErrorHandler ;
17+ private $ debugLines ;
18+ private $ debugStyle ;
19+ public $ debugWrapInlineCSS ;
20+ public $ html_errors ;
21+ public $ loadFunctions ;
22+ function __construct (
23+ $ html_errors =false ,
24+ $ debugLines =5 ,
25+ $ debugStyle ='<style>code br{line-height:0.1em;}pre.error{display:block;position:relative;z-index:99999;}pre.error span:first-child{color:#d00;}</style> ' ,
26+ $ debugWrapInlineCSS ='margin:4px;padding:4px;border:solid 1px #ccc;border-radius:5px;overflow-x:auto;background-color:#fff; ' ,
27+ $ loadFunctions =true
28+ ){
29+ $ this ->html_errors = $ html_errors ;
30+ $ this ->debugLines = $ debugLines ;
31+ $ this ->debugStyle = $ debugStyle ;
32+ $ this ->debugWrapInlineCSS = $ debugWrapInlineCSS ;
33+ $ this ->loadFunctions = $ loadFunctions ;
34+ }
35+ function handle (){
36+ $ this ->handle = true ;
37+ error_reporting (-1 );
38+ ini_set ('display_startup_errors ' ,true );
39+ ini_set ('display_errors ' ,'stdout ' );
40+ ini_set ('html_errors ' ,$ this ->html_errors );
41+ if (!$ this ->registeredErrorHandler ){
42+ $ this ->registeredErrorHandler = true ;
43+ set_error_handler ([$ this ,'errorHandle ' ]);
44+ register_shutdown_function ([$ this ,'fatalErrorHandle ' ]);
45+ set_exception_handler ([$ this ,'catchException ' ]);
46+ if ($ this ->loadFunctions )
47+ include_once __DIR__ .'/functions.inc.php ' ;
48+ }
49+ }
50+ function catchException ($ e ){
51+ $ html = false ;
52+ if (!headers_sent ()){
53+ header ("Content-Type: text/html; charset=utf-8 " );
54+ $ html = true ;
55+ }
56+ $ msg = 'Exception: ' .htmlentities ($ e ->getMessage ());
57+ if ($ html ){
58+ echo $ this ->debugStyle ;
59+ echo '<pre class="error" style=" ' .$ this ->debugWrapInlineCSS .'"><span> ' .$ msg ."</span> \nStackTrace: \n" ;
60+ echo '# ' .get_class ($ e );
61+ if (method_exists ($ e ,'getData ' )){
62+ echo ': ' ;
63+ var_dump ($ e ->getData ());
64+ }
65+ echo htmlentities ($ e ->getTraceAsString ());
66+ echo '</pre> ' ;
67+ }
68+ else {
69+ echo strip_tags ($ msg );
70+ }
71+ return false ;
72+ }
73+ function errorHandle ($ code , $ message , $ file , $ line ){
74+ if (!$ this ->handle ||!ini_get ('error_reporting ' ))
75+ return ;
76+ $ html = false ;
77+ if (!headers_sent ()){
78+ header ("Content-Type: text/html; charset=utf-8 " );
79+ $ html = true ;
80+ }
81+ $ msg = self ::$ errorType [$ code ]."\t$ message \nFile \t$ file \nLine \t$ line " ;
82+ if (is_file ($ file )){
83+ if ($ html ){
84+ echo $ this ->debugStyle ;
85+ echo "<pre class= \"error \" style= \"" .$ this ->debugWrapInlineCSS ."\"><span> " .$ msg ."</span> \nContext: \n" ;
86+ $ f = explode ("\n" ,str_replace (["\r\n" ,"\r" ],"\n" ,file_get_contents ($ file )));
87+ foreach ($ f as &$ x )
88+ $ x .= "\n" ;
89+ $ c = count ($ f );
90+ $ start = $ line -$ this ->debugLines ;
91+ $ end = $ line +$ this ->debugLines ;
92+ if ($ start <0 )
93+ $ start = 0 ;
94+ if ($ end >($ c -1 ))
95+ $ end = $ c -1 ;
96+ $ e = '' ;
97+ for ($ i =$ start ;$ i <=$ end ;$ i ++){
98+ $ e .= $ f [$ i ];
99+ }
100+ $ e = highlight_string ('<?php ' .$ e ,true );
101+ $ e = str_replace ('<br /> ' ,"\n" ,$ e );
102+ $ e = substr ($ e ,35 );
103+ $ x = explode ("\n" ,$ e );
104+ $ e = '<code><span style="color: #000000"> ' ;
105+ $ count = count ($ x );
106+ for ($ i =0 ;$ i <$ count ;$ i ++){
107+ $ y = $ start +$ i ;
108+ $ e .= '<span style="color:# ' .($ y ==$ line ?'d00 ' :'070 ' ).';"> ' .$ y ."\t</span> " ;
109+ $ e .= $ x [$ i ]."\n" ;
110+ }
111+ $ p = strpos ($ e ,'<?php ' );
112+ $ e = substr ($ e ,0 ,$ p ).substr ($ e ,$ p +8 );
113+ echo $ e ;
114+ echo '</pre> ' ;
115+ }
116+ else {
117+ echo strip_tags ($ msg );
118+ }
119+ }
120+ //else{
121+ //echo "$message in $file on line $line";
122+ //}
123+ return true ;
124+ }
125+ function fatalErrorHandle (){
126+ if (!$ this ->handle )
127+ return ;
128+ $ error = error_get_last ();
129+ if ($ error ['type ' ]===E_ERROR ){
130+ self ::errorHandle (E_ERROR ,$ error ['message ' ],$ error ['file ' ],$ error ['line ' ]);
131+ }
132+ }
133+ static function initialize (){
134+ self ::$ errorType = [
135+ E_ERROR => 'error ' ,
136+ E_WARNING => 'warning ' ,
137+ E_PARSE => 'parsing error ' ,
138+ E_NOTICE => 'notice ' ,
139+ E_CORE_ERROR => 'core error ' ,
140+ E_CORE_WARNING => 'core warning ' ,
141+ E_COMPILE_ERROR => 'compile error ' ,
142+ E_COMPILE_WARNING => 'compile warning ' ,
143+ E_USER_ERROR => 'user error ' ,
144+ E_USER_WARNING => 'user warning ' ,
145+ E_USER_NOTICE => 'user notice ' ,
146+ E_STRICT => 'strict standard error ' ,
147+ E_RECOVERABLE_ERROR => 'recoverable error ' ,
148+ E_DEPRECATED => 'deprecated error ' ,
149+ E_USER_DEPRECATED => 'user deprecated error ' ,
150+ ];
151+ if (defined ('E_STRICT ' ))
152+ self ::$ errorType [E_STRICT ] = 'runtime notice ' ;
153+ }
154+ static function errorType ($ code ){
155+ return isset (self ::$ errorType [$ code ])?self ::$ errorType [$ code ]:null ;
156+ }
157+ }
158+ ErrorHandler::initialize ();
0 commit comments