mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-26 07:15:30 +02:00
Extracted exception handler registration to a dedicated class
This commit is contained in:
@@ -2,13 +2,13 @@
|
||||
|
||||
namespace App\Bootstrap;
|
||||
|
||||
use App\Exceptions\ExceptionManager;
|
||||
use App\Middlewares;
|
||||
use App\Providers;
|
||||
use DI\Bridge\Slim\Bridge;
|
||||
use DI\Container;
|
||||
use Invoker\CallableResolver;
|
||||
use Middlewares as HttpMiddlewares;
|
||||
use PHLAK\Config\Config;
|
||||
use Slim\App;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
@@ -56,14 +56,7 @@ class AppManager
|
||||
$app = Bridge::create($this->container);
|
||||
$this->registerMiddlewares($app);
|
||||
|
||||
$this->container->call(function (App $app, Config $config): void {
|
||||
if (! $config->get('app.debug', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$errorMiddleware = $app->addErrorMiddleware(true, true, true);
|
||||
$errorMiddleware->setDefaultErrorHandler(ErrorHandler::class);
|
||||
});
|
||||
$this->container->call(ExceptionManager::class);
|
||||
|
||||
return $app;
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bootstrap;
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
42
app/src/Exceptions/ExceptionManager.php
Normal file
42
app/src/Exceptions/ExceptionManager.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use PHLAK\Config\Config;
|
||||
use Slim\App;
|
||||
|
||||
class ExceptionManager
|
||||
{
|
||||
/** @var App The application */
|
||||
protected $app;
|
||||
|
||||
/** @var Config The application config */
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* Create a new ExceptionManager object.
|
||||
*
|
||||
* @param \Slim\App $app
|
||||
* @param \PHLAK\Config\Config $config
|
||||
*/
|
||||
public function __construct(App $app, Config $config)
|
||||
{
|
||||
$this->app = $app;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up and configure exception handling.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(): void
|
||||
{
|
||||
if ($this->config->get('app.debug', false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$errorMiddleware = $this->app->addErrorMiddleware(true, true, true);
|
||||
$errorMiddleware->setDefaultErrorHandler(ErrorHandler::class);
|
||||
}
|
||||
}
|
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Tests\Bootstrap;
|
||||
|
||||
use App\Bootstrap\ErrorHandler;
|
||||
use App\Exceptions\ErrorHandler;
|
||||
use App\Providers\TwigProvider;
|
||||
use Exception;
|
||||
use Slim\Psr7\Request;
|
37
tests/Exceptions/ExceptionManagerTest.php
Normal file
37
tests/Exceptions/ExceptionManagerTest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Exceptions;
|
||||
|
||||
use App\Exceptions\ErrorHandler;
|
||||
use App\Exceptions\ExceptionManager;
|
||||
use Slim\App;
|
||||
use Slim\Middleware\ErrorMiddleware;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ExceptionManagerTest extends TestCase
|
||||
{
|
||||
public function test_it_sets_the_default_error_handler(): void
|
||||
{
|
||||
$errorMiddleware = $this->createMock(ErrorMiddleware::class);
|
||||
$errorMiddleware->expects($this->once())
|
||||
->method('setDefaultErrorHandler')
|
||||
->with(ErrorHandler::class);
|
||||
|
||||
$app = $this->createMock(App::class);
|
||||
$app->expects($this->once())
|
||||
->method('addErrorMiddleware')
|
||||
->willReturn($errorMiddleware);
|
||||
|
||||
(new ExceptionManager($app, $this->config))();
|
||||
}
|
||||
|
||||
public function test_it_does_not_set_the_default_error_handler_when_debug_is_enabled(): void
|
||||
{
|
||||
$this->config->set('app.debug', true);
|
||||
|
||||
$app = $this->createMock(App::class);
|
||||
$app->expects($this->never())->method('addErrorMiddleware');
|
||||
|
||||
(new ExceptionManager($app, $this->config))();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user