|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace SetBased\ErrorHandler; |
| 5 | + |
| 6 | +use SetBased\Exception\ErrorException; |
| 7 | + |
| 8 | +/** |
| 9 | + * A class for handling PHP runtime errors and warnings in a uniform manner by throwing exceptions. |
| 10 | + */ |
| 11 | +class ErrorHandler |
| 12 | +{ |
| 13 | + //-------------------------------------------------------------------------------------------------------------------- |
| 14 | + /** |
| 15 | + * An error handler function that throws exceptions. |
| 16 | + * |
| 17 | + * @param int $errno The level of the error raised. |
| 18 | + * @param string $errstr The error message. |
| 19 | + * @param string|null $errfile The filename that the error was raised in. |
| 20 | + * @param int|null $errline The line number the error was raised at. |
| 21 | + * |
| 22 | + * @return bool |
| 23 | + * |
| 24 | + * See http://php.net/manual/en/function.set-error-handler.php. |
| 25 | + * |
| 26 | + * @throws ErrorException |
| 27 | + */ |
| 28 | + public function handleError(int $errno, string $errstr, ?string $errfile, ?int $errline): bool |
| 29 | + { |
| 30 | + if (error_reporting()===0) |
| 31 | + { |
| 32 | + // Error was suppressed with the @-operator. Don't throw an exception. |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + $exception = new ErrorException($errstr, $errno, $errno, $errfile, $errline); |
| 37 | + |
| 38 | + // In case error appeared in __toString method we can't throw any exception. |
| 39 | + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
| 40 | + array_shift($trace); |
| 41 | + foreach ($trace as $frame) |
| 42 | + { |
| 43 | + if ($frame['function']==='__toString') |
| 44 | + { |
| 45 | + $handler = set_exception_handler(null); |
| 46 | + restore_exception_handler(); |
| 47 | + |
| 48 | + if (is_callable($handler)) |
| 49 | + { |
| 50 | + $handler($exception); |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + error_log((string)$exception); |
| 55 | + } |
| 56 | + |
| 57 | + // If running unit tests return true. |
| 58 | + if (defined('PHPUNIT_ERROR_HANDLER_TEST_SUITE')) return true; |
| 59 | + |
| 60 | + exit(-1); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + throw $exception; |
| 65 | + } |
| 66 | + |
| 67 | + //-------------------------------------------------------------------------------------------------------------------- |
| 68 | + /** |
| 69 | + * Registers this error handler. |
| 70 | + * |
| 71 | + * @param null|int $errorTypes The mask for triggering this error handler. Defaults to E_ALL | E_STRICT. |
| 72 | + */ |
| 73 | + public function registerErrorHandler($errorTypes = E_ALL) |
| 74 | + { |
| 75 | + ini_set('display_errors', '0'); |
| 76 | + set_error_handler([$this, 'handleError'], $errorTypes); |
| 77 | + } |
| 78 | + |
| 79 | + //-------------------------------------------------------------------------------------------------------------------- |
| 80 | + /** |
| 81 | + * Unregisters this error handler by restoring the PHP error and exception handlers. |
| 82 | + */ |
| 83 | + public function unregisterErrorHandler() |
| 84 | + { |
| 85 | + restore_error_handler(); |
| 86 | + } |
| 87 | + |
| 88 | + //-------------------------------------------------------------------------------------------------------------------- |
| 89 | +} |
| 90 | + |
| 91 | +//---------------------------------------------------------------------------------------------------------------------- |
0 commit comments