From ca1a8b7c3368f199510ff809989b151834b8f161 Mon Sep 17 00:00:00 2001 From: Krzysztof Sucherek Date: Wed, 27 Sep 2023 13:05:03 +0200 Subject: [PATCH] Add possibility to set custom error handler in Bootstrap. --- CHANGELOG.md | 3 ++- src/Ouzo/Core/Bootstrap.php | 34 ++++++++++++++++++++-------- test/src/Ouzo/Core/BootstrapTest.php | 17 ++++++++++++++ 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7694b51c..176d9bea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ Support for PHP 8.0 is dropped. Minimal PHP version required is 8.2. Enhancements: * [ORM] Added support for count query with `DISTINCT` in PostgreSQL dialect. -* [Utilities] `Strings::EMPTY_STRING` is deprecated in favour of `Strings::EMPTY`. +* [Utilities] `Strings::EMPTY_STRING` is deprecated in favour of `Strings::EMPTY`. +* [Core] `Bootstrap::withErrorHandler(ErroraHandler $errorHandler)` set custom error handler which is registered on `Bootstrap::runApplication()`. Release 2.0.0 -------- diff --git a/src/Ouzo/Core/Bootstrap.php b/src/Ouzo/Core/Bootstrap.php index aad34adf..8442410d 100644 --- a/src/Ouzo/Core/Bootstrap.php +++ b/src/Ouzo/Core/Bootstrap.php @@ -35,6 +35,7 @@ class Bootstrap /** @var string[] */ private array $interceptors = []; private bool $overrideMiddleware = false; + private ?ErrorHandler $errorHandler = null; public function __construct(Environment $environment) { @@ -81,6 +82,12 @@ public function overrideMiddleware(...$interceptors): static return $this; } + public function withErrorHandler(ErrorHandler $errorHandler): static + { + $this->errorHandler = $errorHandler; + return $this; + } + public function runApplication(): FrontController { if ($this->configRepository) { @@ -101,21 +108,27 @@ public function runApplication(): FrontController private function registerErrorHandlers(): void { + if (!is_null($this->errorHandler)) { + $this->errorHandler->register(); + return; + } + if (Config::getValue('debug')) { - $handler = new DebugErrorHandler(); - } else { - $handler = new ErrorHandler(); + (new DebugErrorHandler())->register(); + return; } - $handler->register(); + (new ErrorHandler())->register(); } - private function includeRoutes(): void + private + function includeRoutes(): void { $routesPath = Path::join(ROOT_PATH, 'config', 'routes.php'); Files::loadIfExists($routesPath); } - public function setupInjector(): Injector + public + function setupInjector(): Injector { $injector = $this->createInjector(); @@ -133,13 +146,15 @@ public function setupInjector(): Injector return $injector; } - private function createInjector(): Injector + private + function createInjector(): Injector { $injectorConfig = $this->injectorConfig ?: new InjectorConfig(); return $this->injector ?: new Injector($injectorConfig); } - private function createMiddlewareRepository(Injector $injector): MiddlewareRepository + private + function createMiddlewareRepository(Injector $injector): MiddlewareRepository { $middlewareRepository = new MiddlewareRepository(); @@ -162,7 +177,8 @@ private function createMiddlewareRepository(Injector $injector): MiddlewareRepos return $middlewareRepository; } - private function createInterceptor(Injector $injector): Closure + private + function createInterceptor(Injector $injector): Closure { return function ($interceptor) use ($injector) { $instance = $injector->getInstance($interceptor); diff --git a/test/src/Ouzo/Core/BootstrapTest.php b/test/src/Ouzo/Core/BootstrapTest.php index 16fdd0ef..4186ada1 100644 --- a/test/src/Ouzo/Core/BootstrapTest.php +++ b/test/src/Ouzo/Core/BootstrapTest.php @@ -10,6 +10,7 @@ use Ouzo\CookiesSetter; use Ouzo\DownloadHandler; use Ouzo\Environment; +use Ouzo\ExceptionHandling\ErrorHandler; use Ouzo\HeaderSender; use Ouzo\Injection\InjectorConfig; use Ouzo\Middleware\Interceptor\SessionStarter; @@ -115,4 +116,20 @@ public function shouldThrowExceptionWhenMiddlewareClassNotImplementingIntercepto //then CatchException::assertThat()->hasMessage('stdClass class is not implementing Interceptor interface'); } + + #[Test] + public function shouldRegisterCustomErrorHandler() + { + //given + /** @var ErrorHandler|MockInterface $errorHandler */ + $errorHandler = Mock::create(ErrorHandler::class); + + //when + $this->bootstrap + ->withErrorHandler($errorHandler) + ->runApplication(); + + //then + Mock::verify($errorHandler)->register(); + } }