From b6417cba97234c83546eac492718556513a18a04 Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 5 Nov 2024 15:48:10 -0500 Subject: [PATCH] Refactor exception handling to Runtime class --- src/LogLib/Log.php | 74 +----------------------- src/LogLib/Runtime.php | 124 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 72 deletions(-) create mode 100644 src/LogLib/Runtime.php diff --git a/src/LogLib/Log.php b/src/LogLib/Log.php index da4c823..cd0cfd8 100644 --- a/src/LogLib/Log.php +++ b/src/LogLib/Log.php @@ -213,77 +213,7 @@ */ public static function registerExceptionHandler(): void { - - // Handle uncaught exceptions - set_exception_handler(static function(Throwable $exception): void - { - try - { - self::error('Runtime', $exception->getMessage(), $exception); - } - catch(Exception) - { - return; - } - }); - - // Handle warnings and notices - set_error_handler(static function(int $errno, string $errstr, string $errfile = '', int $errline = 0): bool - { - try - { - // Convert error to exception for consistent handling - $exception = new ErrorException($errstr, 0, $errno, $errfile, $errline); - - // Handle different error types - switch ($errno) - { - case E_ERROR: - case E_PARSE: - case E_CORE_ERROR: - case E_COMPILE_ERROR: - case E_USER_ERROR: - self::error('Runtime', $errstr, $exception); - break; - - case E_USER_DEPRECATED: - case E_DEPRECATED: - case E_USER_NOTICE: - case E_NOTICE: - case E_USER_WARNING: - case E_WARNING: - default: - self::warning('Runtime', $errstr, $exception); - break; - } - } - catch(Exception) - { - return false; - } - - // Return true to prevent PHP's internal error handler - return true; - }); - - // Handle fatal errors - register_shutdown_function(static function(): void - { - $error = error_get_last(); - - if ($error !== null && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR])) - { - try - { - $exception = new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']); - self::error('Fatal Error', $error['message'], $exception); - } - catch(Exception) - { - return; - } - } - }); + Runtime::registerExceptionHandler(); } /** @@ -293,6 +223,6 @@ */ public static function unregisterExceptionHandler(): void { - set_exception_handler(null); + Runtime::unregisterExceptionHandler(); } } \ No newline at end of file diff --git a/src/LogLib/Runtime.php b/src/LogLib/Runtime.php new file mode 100644 index 0000000..3c07ff6 --- /dev/null +++ b/src/LogLib/Runtime.php @@ -0,0 +1,124 @@ +getMessage(), $throwable); + } + catch(Exception) + { + return; + } + } + + /** + * Handles PHP errors by converting them to exceptions and logging appropriately. + * + * @param int $errno The level of the error raised. + * @param string $errstr The error message. + * @param string $errfile The filename that the error was raised in. + * @param int $errline The line number the error was raised at. + * @return bool True to prevent PHP's internal error handler from being invoked. + */ + public static function errorHandler(int $errno, string $errstr, string $errfile = '', int $errline = 0): bool + { + try + { + // Convert error to exception for consistent handling + $exception = new ErrorException($errstr, 0, $errno, $errfile, $errline); + + // Handle different error types + switch ($errno) + { + case E_ERROR: + case E_PARSE: + case E_CORE_ERROR: + case E_COMPILE_ERROR: + case E_USER_ERROR: + Log::error('Runtime', $errstr, $exception); + break; + + case E_USER_DEPRECATED: + case E_DEPRECATED: + case E_USER_NOTICE: + case E_NOTICE: + case E_USER_WARNING: + case E_WARNING: + default: + Log::warning('Runtime', $errstr, $exception); + break; + } + } + catch(Exception) + { + return false; + } + + // Return true to prevent PHP's internal error handler + return true; + } + + /** + * Handles script shutdown by checking for any fatal errors and logging them. + * + * This method is designed to be registered with the `register_shutdown_function`, + * and it inspects the last error that occurred using `error_get_last`. If a fatal + * error is detected, it logs the error details. + * + * @return void + */ + public static function shutdownHandler(): void + { + $error = error_get_last(); + + if ($error !== null && in_array($error['type'], [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR])) + { + try + { + $exception = new ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']); + Log::error('Fatal Error', $error['message'], $exception); + } + catch(Exception) + { + return; + } + } + } + + /** + * Unregisters the currently registered exception handler. + * + * @return void + */ + public static function unregisterExceptionHandler(): void + { + set_exception_handler(null); + } + } \ No newline at end of file