33 * PHP library for handling exceptions and errors.
44 *
55 * @author Josantonius - info@josantonius.com
6- * @copyright Copyright (c) 2016 JST PHP Framework
6+ * @copyright Copyright (c) 2016-2017
77 * @license https://opensource.org/licenses/MIT - The MIT License (MIT)
8- * @link https://github.com/Josantonius/PHP- ErrorHandler
9- * @since File available since 1.0.0 - Update: 2017-02-14
8+ * @link https://github.com/Josantonius/ErrorHandler
9+ * @since File available since 1.0.0 - Update: 2017-02-21
1010 */
1111
1212namespace Josantonius \ErrorHandler ;
@@ -30,52 +30,25 @@ class ErrorHandler {
3030 protected static $ stack ;
3131
3232 /**
33- * Catch errors and exceptions and execute the method.
34- *
35- * @since 1.0.0
36- */
37- public function __construct () {
38-
39- /**
40- * Compatibility with exception and error handler for PHP7 and HHVM.
41- *
42- * @since 1.0.0
43- */
44- if (!defined ('HHVM_VERSION ' ) && version_compare (phpversion (), '7.0.0 ' , '>= ' )) {
45-
46- set_exception_handler (array ($ this , 'catchThrowable ' ));
47-
48- } else {
49-
50- set_exception_handler (array ($ this , 'catchException ' ));
51- }
52-
53- set_error_handler (array ($ this , 'catchError ' ));
54- }
55-
56- /**
57- * Handle exceptions catch.
33+ * Style load validator.
5834 *
5935 * @since 1.0.0
6036 *
61- * @param object $e Throwable object
37+ * @var bool
6238 */
63- public function catchThrowable (Throwable $ e ) {
64-
65- $ this ->prepareException ($ e );
66- }
39+ protected static $ styles = false ;
6740
6841 /**
69- * Handle exceptions catch .
42+ * Catch errors and exceptions and execute the method .
7043 *
7144 * @since 1.0.0
72- *
73- * @param object $e Exception object
7445 */
75- public function catchException (Exception $ e ) {
46+ public function __construct () {
47+
48+ set_exception_handler (array ($ this , 'exception ' ));
7649
77- $ this -> prepareException ( $ e );
78- }
50+ set_error_handler ( array ( $ this , ' error ' ) );
51+ }
7952
8053 /**
8154 * Handle error catch.
@@ -87,18 +60,21 @@ public function catchException(Exception $e) {
8760 * @param int $file → error file
8861 * @param int $line → error line
8962 */
90- public function catchError ($ code , $ msg , $ file , $ line ) {
63+ public function error ($ code , $ msg , $ file , $ line ) {
9164
9265 static ::$ stack = [
9366 'type ' => $ this ->getErrorType ($ code ),
9467 'message ' => $ msg ,
9568 'file ' => $ file ,
9669 'line ' => $ line ,
97- 'code ' => $ code ? "[ " . $ code . "] " : "" ,
98- 'trace ' => '' ,
70+ 'code ' => $ code . " · " ,
71+ 'trace ' => '' ,
72+ 'preview ' => '' ,
9973 ];
10074
101- $ this ->show ();
75+ static ::getPreviewCode ($ file , $ line );
76+
77+ $ this ->render ();
10278 }
10379
10480 /**
@@ -115,49 +91,41 @@ public function catchError($code, $msg, $file, $line) {
11591 * array $e->getTrace()
11692 * string $e->getTrace()[0]['file'] → file exception launcher
11793 * int $e->getTrace()[0]['line'] → line exception launcher
118- * string $e->getTrace()[0]['function'] → function exception launcher
94+ * string $e->getTrace()[0]['function'] → function exception
11995 * string $e->getTrace()[0]['class'] → class exception launcher
12096 * string $e->getTrace()[0]['type'] → type exception launcher
12197 * array $e->getTrace()[0]['args'] → args exception launcher
12298 *
12399 * Optionally for libraries used in JST PHP Framework:
124100 * int $e->statusCode → HTTP response status code
125101 */
126- public function prepareException ($ e ) {
102+ public function exception ($ e ) {
127103
128104 static ::$ stack = [
129105 'type ' => 'Exception ' ,
130- 'message ' => (null !== $ e ->getMessage ()) ? $ e ->getMessage () : '' ,
131- 'file ' => (null !== $ e ->getFile ()) ? $ e ->getFile () : 0 ,
132- 'line ' => (null !== $ e ->getLine ()) ? $ e ->getLine () : 0 ,
133- 'code ' => $ e ->getCode () ? "[ " . $ e ->getCode () . "] " : "" ,
134- 'trace ' => '' ,
106+ 'message ' => $ e ->getMessage (),
107+ 'file ' => $ e ->getFile (),
108+ 'line ' => $ e ->getLine (),
109+ 'code ' => $ e ->getCode () . " · " ,
110+ 'statusCode ' => (isset ($ e ->statusCode )) ? $ e ->statusCode : 0 ,
111+ 'trace ' => "\r\n<hr>BACKTRACE: \r\n" ,
112+ 'preview ' => "" ,
135113 ];
136114
137- if ( is_array ( $ e -> getTrace ()) && count ( $ e -> getTrace ()) > 0 ) {
115+ static :: getPreviewCode ( static :: $ stack [ ' file ' ], static :: $ stack [ ' line ' ]);
138116
139- $ tab = "\r\n" ;
117+ $ trace = preg_split ( " /#[\d]/ " , $ e -> getTraceAsString ()) ;
140118
141- foreach ( $ e -> getTrace () as $ key => $ value ) {
142-
143- $ tab .= " · · · · " ;
119+ unset( $ trace [ 0 ]);
120+
121+ array_pop ( $ trace ) ;
144122
145- $ statusCode = (isset ($ e ->statusCode )) ? $ e ->statusCode : 0 ;
146-
147- static ::$ stack ['trace ' ] = "\r\n\r\n<hr> " .
148-
149- $ tab . " [Trace " . ($ key + 1 ) . "] " .
150- $ tab . " FILE: " . $ value ['file ' ] .
151- $ tab . " HTTP CODE: " . $ statusCode .
152- $ tab . " FUNCTION: " . $ value ['function ' ] .
153- $ tab . " CLASS: " . $ value ['class ' ] .
154- $ tab . " ARGS: " . json_encode ($ value ['args ' ]) .
155- $ tab . " LINE: " . $ value ['line ' ] .
156- $ tab . " TYPE " . $ value ['type ' ] . "\r\n" ;
157- }
123+ foreach ($ trace as $ key => $ value ) {
124+
125+ static ::$ stack ['trace ' ] .= "\n" . $ key . " · " . $ value ;
158126 }
159127
160- $ this ->show ();
128+ $ this ->render ();
161129 }
162130
163131 /**
@@ -204,34 +172,65 @@ protected function getErrorType($code) {
204172 case E_USER_DEPRECATED :
205173 return static ::$ stack ['type ' ] = 'User Deprecated ' ; /* 16384 */
206174 default :
207- return static ::$ stack ['type ' ] = (string ) $ code ;
208-
175+ return static ::$ stack ['type ' ] = 'Error ' ;
209176 }
177+ }
178+
179+ /**
180+ * Get preview of the error line.
181+ *
182+ * @since 1.1.0
183+ *
184+ * @param string $file → filepath
185+ * @param string $line → error line
186+ */
187+ protected function getPreviewCode ($ file , $ line ) {
188+
189+ $ file = file ($ file );
190+
191+ $ start = ($ line - 5 >= 0 ) ? $ line - 5 : $ line - 1 ;
192+ $ end = ($ line - 5 >= 0 ) ? $ line + 4 : $ line + 8 ;
193+
194+ for ($ i = $ start ; $ i < $ end ; $ i ++) {
210195
196+ if (!isset ($ file [$ i ])) {
197+
198+ continue ;
199+ }
200+
201+ $ text = trim ($ file [$ i ]);
202+
203+ if ($ i == $ line - 1 ) {
204+
205+ static ::$ stack ['preview ' ] .=
206+ "<span class='jst-line'> " . ($ i + 1 ) . "</span> " .
207+ "<span class='jst-mark text'> " . $ text . "</span><br> " ;
208+
209+ continue ;
210+ }
211+
212+ static ::$ stack ['preview ' ] .=
213+ "<span class='jst-line'> " . ($ i + 1 ) . "</span> " .
214+ "<span class='text'> " . $ text . "</span><br> " ;
215+ }
211216 }
212217
213218 /**
214219 * Show alert in browser.
215220 *
216221 * @since 1.0.0
217- *
218- * @param string $type → error or exception
219222 */
220- protected function show () {
221-
222- $ css = __DIR__ . '/resources/styles.php ' ;
223+ protected function render () {
223224
224225 static ::$ stack ['mode ' ] = defined ('HHVM_VERSION ' ) ? 'HHVM ' : 'PHP ' ;
225226
226- ob_start ();
227+ if (! static :: $ styles ) {
227228
228- if (! isset ( static ::$ stack [ ' css ' ])) {
229+ static ::$ styles = true ;
229230
230- static ::$ stack ['css ' ] = require ($ css );
231+ static ::$ stack ['css ' ] = require (__DIR__ . ' /resources/styles.php ' );
231232 }
232233
233234 require (__DIR__ . '/resources/view.php ' );
234-
235- ob_end_flush ();
236235 }
237236}
0 commit comments