From eb3e310b0a4daed11d0dd42f37bad19707226927 Mon Sep 17 00:00:00 2001 From: Maarten Date: Tue, 26 Nov 2024 19:53:35 +0100 Subject: [PATCH] Update exception handler --- src/Exceptions/ExceptionHandler.php | 53 ++++++++++++++++++++--------- src/Factory/BootstrapFactory.php | 14 ++++---- src/Helpers/helpers.php | 17 +++++++-- src/Routing/RouteDispatcher.php | 3 +- 4 files changed, 59 insertions(+), 28 deletions(-) diff --git a/src/Exceptions/ExceptionHandler.php b/src/Exceptions/ExceptionHandler.php index 77f5355..29524fc 100644 --- a/src/Exceptions/ExceptionHandler.php +++ b/src/Exceptions/ExceptionHandler.php @@ -2,7 +2,6 @@ namespace Core\Exceptions; -use Core\Http\Request; use Throwable; use Whoops\Handler\Handler; use Whoops\Handler\JsonResponseHandler; @@ -13,30 +12,50 @@ use Whoops\Run as Whoops; class ExceptionHandler { /** - * Exceptions handler instance + * Exceptions reporter instance * * @var Whoops */ - private static Whoops $instance; + private static Whoops $reporter; /** - * Get exceptions handler instance + * Exceptions handler instance * - * @param \Core\Http\Request|null $request - * @return \Whoops\Run + * @var \Core\Exceptions\ExceptionHandler */ - public static function instance(Request|null $request = null): Whoops + private static ExceptionHandler $instance; + + /** + * Get instance of exceptions handler + * + * @return \Core\Exceptions\ExceptionHandler + */ + public static function instance(): ExceptionHandler { if (!isset(self::$instance)) { - $instance = new Whoops(); - $instance->pushHandler(self::handler($request)); - - self::$instance = $instance; + self::$instance = new self(); } return self::$instance; } + /** + * Get exceptions reporter instance + * + * @return \Whoops\Run + */ + public static function reporter(): Whoops + { + if (!isset(self::$reporter)) { + $reporter = new Whoops(); + $reporter->pushHandler(self::handler()); + + self::$reporter = $reporter; + } + + return self::$reporter; + } + /** * Get correct handler * @@ -60,9 +79,9 @@ class ExceptionHandler * * @return void */ - public static function catch(): void + public function register(): void { - self::instance()->register(); + self::reporter()->register(); } /** @@ -71,9 +90,9 @@ class ExceptionHandler * @param \Throwable $exception * @return never */ - public static function catchOne(Throwable $exception): never + public function handle(Throwable $exception): never { - self::instance()->handleException($exception); + self::reporter()->handleException($exception); exit(0); } @@ -85,14 +104,14 @@ class ExceptionHandler * @param string|null $message * @return never */ - public static function make(mixed $abstract = null, string|null $message = null): never + public function make(mixed $abstract = null, string|null $message = null): never { if(is_string($abstract)) { $abstract = app()->make($abstract, $message); } if(is_subclass_of($abstract, 'Exception')) { - self::catchOne($abstract); + $this->handle($abstract); } exit(0); diff --git a/src/Factory/BootstrapFactory.php b/src/Factory/BootstrapFactory.php index 08b02b7..fecf650 100644 --- a/src/Factory/BootstrapFactory.php +++ b/src/Factory/BootstrapFactory.php @@ -24,8 +24,8 @@ class BootstrapFactory // Create request $this->request = app()->make(Request::class, [$_POST + $_FILES]); - // Capture all exceptions - ExceptionHandler::catch(); + // Register exceptions handler for error reporting + app()->make(ExceptionHandler::class)->register(); // Load routes require '../config/routes.php'; @@ -37,13 +37,13 @@ class BootstrapFactory // Dispatch router app()->make(RouteDispatcher::class)->dispatch($this->request); } catch (\Exception $e) { - ExceptionHandler::catchOne($e); + exceptions()->handle($e); } } /** * @param string $abstract - * @param array $arguments + * @param mixed $arguments * @return mixed */ public function make(string $abstract, mixed $arguments = []): mixed @@ -53,7 +53,7 @@ class BootstrapFactory /** * @param string $abstract - * @param array $arguments + * @param mixed $arguments * @return mixed|null */ private function resolve(string $abstract, mixed $arguments = []): mixed @@ -63,11 +63,11 @@ class BootstrapFactory $reflection = new \ReflectionClass($abstract); return $reflection->newInstanceArgs($arguments); } catch (\ReflectionException $e) { - ExceptionHandler::catchOne($e); + exceptions()->handle($e); } } - ExceptionHandler::make(ClassNotFoundException::class, sprintf("Class '%s' not found", $abstract)); + exceptions()->make(ClassNotFoundException::class, sprintf("Class '%s' not found", $abstract)); } /** diff --git a/src/Helpers/helpers.php b/src/Helpers/helpers.php index 9d3c5c4..4475eb5 100644 --- a/src/Helpers/helpers.php +++ b/src/Helpers/helpers.php @@ -2,6 +2,7 @@ use Core\Bootstrap; use Core\Env\Env; +use Core\Exceptions\ExceptionHandler; use Core\Http\Request; @@ -11,10 +12,10 @@ if(!function_exists('app')) * Make abstract of instance or get application instance * * @param mixed $abstract - * @param array $arguments + * @param mixed $arguments * @return \Core\Factory\BootstrapFactory|mixed */ - function app(mixed $abstract = null, array $arguments = []): mixed + function app(mixed $abstract = null, mixed $arguments = []): mixed { if (is_null($abstract)) { return Bootstrap::getInstance(); @@ -49,3 +50,15 @@ if (!function_exists('request')) { return app()->request(); } } + +if (!function_exists('exceptions')) { + /** + * Get error handler instance + * + * @return \Core\Exceptions\ExceptionHandler + */ + function exceptions(): ExceptionHandler + { + return ExceptionHandler::instance(); + } +} diff --git a/src/Routing/RouteDispatcher.php b/src/Routing/RouteDispatcher.php index 7b8c320..13dc276 100644 --- a/src/Routing/RouteDispatcher.php +++ b/src/Routing/RouteDispatcher.php @@ -5,7 +5,6 @@ namespace Core\Routing; use Core\Exceptions\ExceptionHandler; use Core\Exceptions\Exceptions\NotFoundHttpException; use Core\Http\Request; -use Core\Http\View\Engine; use Core\Http\View\Render; use Exception; @@ -136,7 +135,7 @@ class RouteDispatcher private function handleException(Exception $e, int $statusCode, string $message): void { if (env('debug')) { - ExceptionHandler::catchOne($e); + exceptions()->handle($e); } http_response_code($statusCode);