Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| UnhandledHandler | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| exceptionHandler | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| errorHandler | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| shutdownHandler | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS; |
| 16 | |
| 17 | use phpOMS\Log\FileLogger; |
| 18 | |
| 19 | /** |
| 20 | * Default exception and error handler. |
| 21 | * |
| 22 | * @package phpOMS |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | final class UnhandledHandler |
| 28 | { |
| 29 | /** |
| 30 | * Exception handler. |
| 31 | * |
| 32 | * @param \Throwable $e Exception |
| 33 | * |
| 34 | * @return void |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | * @codeCoverageIgnore |
| 38 | */ |
| 39 | public static function exceptionHandler(\Throwable $e) : void |
| 40 | { |
| 41 | $logger = FileLogger::getInstance(__DIR__ . '/../Logs'); |
| 42 | $logger->critical(FileLogger::MSG_FULL, [ |
| 43 | 'message' => $e->getMessage(), |
| 44 | 'line' => $e->getLine(), |
| 45 | 'file' => $e->getFile(), |
| 46 | ]); |
| 47 | |
| 48 | $_SERVER = []; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Error handler. |
| 53 | * |
| 54 | * @param int $errno Error number |
| 55 | * @param string $errstr Error message |
| 56 | * @param string $errfile Error file |
| 57 | * @param int $errline Error line |
| 58 | * |
| 59 | * @return bool Returns true if the error could be logged otherwise false is returned |
| 60 | * |
| 61 | * @since 1.0.0 |
| 62 | */ |
| 63 | public static function errorHandler(int $errno, string $errstr, string $errfile, int $errline) : bool |
| 64 | { |
| 65 | $logger = FileLogger::getInstance(__DIR__ . '/../Logs'); |
| 66 | |
| 67 | if ((\error_reporting() & $errno) === 0) { |
| 68 | \error_clear_last(); |
| 69 | |
| 70 | $_SERVER = []; |
| 71 | |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | \error_clear_last(); |
| 76 | |
| 77 | $logger->error(FileLogger::MSG_FULL, [ |
| 78 | 'message' => 'Undefined error', |
| 79 | 'str' => $errstr, |
| 80 | 'line' => $errline, |
| 81 | 'file' => $errfile, |
| 82 | ]); |
| 83 | |
| 84 | $_SERVER = []; |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Shutdown handler. |
| 91 | * |
| 92 | * @return void |
| 93 | * |
| 94 | * @since 1.0.0 |
| 95 | * @codeCoverageIgnore |
| 96 | */ |
| 97 | public static function shutdownHandler() : void |
| 98 | { |
| 99 | $e = \error_get_last(); |
| 100 | \error_clear_last(); |
| 101 | |
| 102 | if ($e === null) { |
| 103 | $_SERVER = []; |
| 104 | |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | $logger = FileLogger::getInstance(__DIR__ . '/../Logs'); |
| 109 | $logger->warning(FileLogger::MSG_FULL, [ |
| 110 | 'message' => $e['message'], |
| 111 | 'line' => $e['line'], |
| 112 | 'file' => $e['file'], |
| 113 | ]); |
| 114 | |
| 115 | $_SERVER = []; |
| 116 | } |
| 117 | } |