Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed "ERROR: Using ${var} in strings is deprecated, use {$var} instead" and added logError and logErrorDetails variables on the Shutdown handler #351

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Actions/User/ViewUserAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
28 changes: 19 additions & 9 deletions src/Application/Handlers/ShutdownHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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}.";
Expand Down
Loading