diff --git a/public/index.php b/public/index.php index bc583c821..2d26a3490 100644 --- a/public/index.php +++ b/public/index.php @@ -7,6 +7,7 @@ use App\Application\ResponseEmitter\ResponseEmitter; use App\Application\Settings\SettingsInterface; use DI\ContainerBuilder; +use Psr\Log\LoggerInterface; use Slim\Factory\AppFactory; use Slim\Factory\ServerRequestCreatorFactory; @@ -16,7 +17,7 @@ $containerBuilder = new ContainerBuilder(); if (false) { // Should be set to true in production - $containerBuilder->enableCompilation(__DIR__ . '/../var/cache'); + $containerBuilder->enableCompilation(__DIR__ . '/../var/cache'); } // Set up settings @@ -50,8 +51,16 @@ /** @var SettingsInterface $settings */ $settings = $container->get(SettingsInterface::class); +/** @var LoggerInterface $logger */ +$logger = $container->get(LoggerInterface::class); + +/** @var bool $displayErrorDetails */ $displayErrorDetails = $settings->get('displayErrorDetails'); + +/** @var bool $logError */ $logError = $settings->get('logError'); + +/** @var bool $logErrorDetails */ $logErrorDetails = $settings->get('logErrorDetails'); // Create Request object from globals @@ -60,10 +69,20 @@ // Create Error Handler $responseFactory = $app->getResponseFactory(); -$errorHandler = new HttpErrorHandler($callableResolver, $responseFactory); - -// Create Shutdown Handler -$shutdownHandler = new ShutdownHandler($request, $errorHandler, $displayErrorDetails); +$errorHandler = new HttpErrorHandler( + $callableResolver, + $responseFactory, + $logger +); + +// Create and register Shutdown Handler +$shutdownHandler = new ShutdownHandler( + $request, + $errorHandler, + $displayErrorDetails, + $logError, + $logErrorDetails +); register_shutdown_function($shutdownHandler); // Add Routing Middleware @@ -73,7 +92,12 @@ $app->addBodyParsingMiddleware(); // Add Error Middleware -$errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, $logError, $logErrorDetails); +$errorMiddleware = $app->addErrorMiddleware( + $displayErrorDetails, + $logError, + $logErrorDetails, + $logger +); $errorMiddleware->setDefaultErrorHandler($errorHandler); // Run App & Emit Response diff --git a/src/Application/Actions/User/ViewUserAction.php b/src/Application/Actions/User/ViewUserAction.php index e18e9fcd0..9b46aa196 100644 --- a/src/Application/Actions/User/ViewUserAction.php +++ b/src/Application/Actions/User/ViewUserAction.php @@ -16,7 +16,7 @@ protected function action(): Response $userId = (int) $this->resolveArg('id'); $user = $this->userRepository->findUserOfId($userId); - $this->logger->info("User of id `{$userId}` was viewed."); + $this->logger->info("User of id " . $userId . " was viewed."); return $this->respondWithData($user); } diff --git a/src/Application/Handlers/ShutdownHandler.php b/src/Application/Handlers/ShutdownHandler.php index 72b6d46ea..fa7cdfedc 100644 --- a/src/Application/Handlers/ShutdownHandler.php +++ b/src/Application/Handlers/ShutdownHandler.php @@ -16,31 +16,41 @@ class ShutdownHandler private bool $displayErrorDetails; + private bool $logError; + + private bool $logErrorDetails; + public function __construct( Request $request, HttpErrorHandler $errorHandler, - bool $displayErrorDetails + bool $displayErrorDetails, + bool $logError, + bool $logErrorDetails ) { - $this->request = $request; - $this->errorHandler = $errorHandler; + $this->request = $request; + $this->errorHandler = $errorHandler; $this->displayErrorDetails = $displayErrorDetails; + $this->logError = $logError; + $this->logErrorDetails = $logErrorDetails; } public function __invoke() { $error = error_get_last(); + if (!$error) { return; } - $message = $this->getErrorMessage($error); + $message = $this->getErrorMessage($error); $exception = new HttpInternalServerErrorException($this->request, $message); + $response = $this->errorHandler->__invoke( $this->request, $exception, $this->displayErrorDetails, - false, - false, + $this->logError, + $this->logErrorDetails, ); $responseEmitter = new ResponseEmitter(); @@ -53,10 +63,10 @@ private function getErrorMessage(array $error): string return 'An error while processing your request. Please try again later.'; } - $errorFile = $error['file']; - $errorLine = $error['line']; + $errorFile = $error['file']; + $errorLine = $error['line']; $errorMessage = $error['message']; - $errorType = $error['type']; + $errorType = $error['type']; if ($errorType === E_USER_ERROR) { return "FATAL ERROR: {$errorMessage}. on line {$errorLine} in file {$errorFile}.";