Skip to content

Commit

Permalink
Add possibility to set custom error handler in Bootstrap.
Browse files Browse the repository at this point in the history
  • Loading branch information
ksucherek committed Sep 27, 2023
1 parent 9cc81a6 commit ca1a8b7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down
34 changes: 25 additions & 9 deletions src/Ouzo/Core/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Bootstrap
/** @var string[] */
private array $interceptors = [];
private bool $overrideMiddleware = false;
private ?ErrorHandler $errorHandler = null;

public function __construct(Environment $environment)
{
Expand Down Expand Up @@ -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) {
Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions test/src/Ouzo/Core/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

0 comments on commit ca1a8b7

Please sign in to comment.