|
2 | 2 |
|
3 | 3 | namespace JMSLBAM\RayPHPErrors; |
4 | 4 |
|
5 | | -class Ray { |
| 5 | +class Ray |
| 6 | +{ |
| 7 | + /** @var bool */ |
| 8 | + private $debug; |
6 | 9 |
|
7 | | - /** @var bool */ |
8 | | - private $debug; |
| 10 | + public function init($debug = false) |
| 11 | + { |
9 | 12 |
|
10 | | - public function init( $debug = false ) { |
| 13 | + $this->debug = $debug; |
11 | 14 |
|
12 | | - $this->debug = $debug; |
| 15 | + set_exception_handler([$this, 'setExceptionHandler']); // exception |
| 16 | + set_error_handler([$this, 'setErrorHandler']); // error |
| 17 | + register_shutdown_function([$this, 'registerShutdownFunction']); // fatal error |
| 18 | + } |
13 | 19 |
|
14 | | - set_exception_handler( [ $this, 'setExceptionHandler'] ); // exception |
15 | | - set_error_handler( [ $this, 'setErrorHandler'] ); // error |
16 | | - register_shutdown_function( [ $this, 'registerShutdownFunction'] ); // fatal error |
17 | | - } |
| 20 | + /** |
| 21 | + * Set up error handling |
| 22 | + * |
| 23 | + * @return void |
| 24 | + */ |
| 25 | + public function setErrorHandler($errorNumber, $message, $file, $lineNumber) |
| 26 | + { |
18 | 27 |
|
19 | | - /** |
20 | | - * Set up error handling |
21 | | - * |
22 | | - * @return void |
23 | | - */ |
24 | | - public function setErrorHandler( $errorNumber, $message, $file, $lineNumber ) { |
| 28 | + $payload = new ErrorPayload($message, $file.':'.$lineNumber); |
25 | 29 |
|
26 | | - $payload = new ErrorPayload($message, $file . ':' . $lineNumber ); |
| 30 | + $this->sendRequest($message, $file, $lineNumber); |
27 | 31 |
|
28 | | - $this->sendRequest( $message, $file, $lineNumber ); |
| 32 | + if ($this->debug) { |
| 33 | + dump($message, $file.':'.$lineNumber); |
| 34 | + } |
29 | 35 |
|
30 | | - if( $this->debug ) { |
31 | | - dump( $message, $file . ':' . $lineNumber ); |
32 | | - } |
| 36 | + return false; |
| 37 | + } |
33 | 38 |
|
34 | | - return false; |
35 | | - } |
| 39 | + /** |
| 40 | + * Catch fatal errors |
| 41 | + * |
| 42 | + * @return void |
| 43 | + */ |
| 44 | + public function registerShutdownFunction() |
| 45 | + { |
36 | 46 |
|
37 | | - /** |
38 | | - * Catch fatal errors |
39 | | - * |
40 | | - * @return void |
41 | | - */ |
42 | | - public function registerShutdownFunction() { |
43 | | - |
44 | | - $error = error_get_last(); |
| 47 | + $error = error_get_last(); |
45 | 48 |
|
46 | | - if( is_null( $error ) ) { |
47 | | - return; |
48 | | - } |
| 49 | + if (is_null($error)) { |
| 50 | + return; |
| 51 | + } |
49 | 52 |
|
50 | | - $this->sendRequest( $error['message'], $error['file'], $error['line'] ); |
| 53 | + $this->sendRequest($error['message'], $error['file'], $error['line']); |
51 | 54 |
|
52 | | - if( $this->debug ) { |
53 | | - dump( $error ); |
54 | | - } |
55 | | - } |
| 55 | + if ($this->debug) { |
| 56 | + dump($error); |
| 57 | + } |
| 58 | + } |
56 | 59 |
|
57 | | - /** |
58 | | - * Catch exceptions |
59 | | - * |
60 | | - * @return void |
61 | | - */ |
62 | | - public function setExceptionHandler( $exception ) { |
| 60 | + /** |
| 61 | + * Catch exceptions |
| 62 | + * |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + public function setExceptionHandler($exception) |
| 66 | + { |
63 | 67 |
|
64 | | - $this->sendRequest( $exception->getMessage(), $exception->getFile(), $exception->getLine() ); |
| 68 | + $this->sendRequest($exception->getMessage(), $exception->getFile(), $exception->getLine()); |
65 | 69 |
|
66 | | - if( $this->debug ) { |
67 | | - dump( $exception ); |
68 | | - } |
69 | | - } |
| 70 | + if ($this->debug) { |
| 71 | + dump($exception); |
| 72 | + } |
| 73 | + } |
70 | 74 |
|
71 | | - private function sendRequest( $message, $file, $lineNumber, $color = 'red' ) { |
| 75 | + private function sendRequest($message, $file, $lineNumber, $color = 'red') |
| 76 | + { |
72 | 77 |
|
73 | | - $payload = new ErrorPayload($message, $file . ':' . $lineNumber ); |
74 | | - |
75 | | - ray()->sendRequest( $payload )->color( $color ); |
| 78 | + $payload = new ErrorPayload($message, $file.':'.$lineNumber); |
76 | 79 |
|
77 | | - // Sprinkle a little WordPress Query Monitor here so that still shows the errors |
78 | | - if( function_exists('do_action') ) { |
79 | | - do_action( 'qm/error', $message ); |
80 | | - } |
81 | | - } |
| 80 | + ray()->sendRequest($payload)->color($color); |
| 81 | + |
| 82 | + // Sprinkle a little WordPress Query Monitor here so that still shows the errors |
| 83 | + if (function_exists('do_action')) { |
| 84 | + do_action('qm/error', $message); |
| 85 | + } |
| 86 | + } |
82 | 87 | } |
0 commit comments