Compare commits
2 Commits
644197fae8
...
3805725d46
Author | SHA1 | Date | |
---|---|---|---|
3805725d46 | |||
51801d4228 |
@ -1,9 +1,12 @@
|
||||
<?php namespace SokoWeb\Response;
|
||||
|
||||
use ErrorException;
|
||||
use Exception;
|
||||
use SokoWeb\Interfaces\Response\IRedirect;
|
||||
use SokoWeb\Interfaces\Response\IContent;
|
||||
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
|
||||
use SokoWeb\Interfaces\Authorization\ISecured;
|
||||
use SokoWeb\Interfaces\Database\IConnection;
|
||||
use SokoWeb\Interfaces\Request\IRequest;
|
||||
use SokoWeb\Response\Redirect;
|
||||
use SokoWeb\Response\HtmlContent;
|
||||
@ -14,6 +17,8 @@ class HttpResponse
|
||||
{
|
||||
private IRequest $request;
|
||||
|
||||
private IConnection $dbConnection;
|
||||
|
||||
private RouteCollection $routeCollection;
|
||||
|
||||
private array $appConfig;
|
||||
@ -26,12 +31,16 @@ class HttpResponse
|
||||
|
||||
public function __construct(
|
||||
IRequest $request,
|
||||
IConnection $dbConnection,
|
||||
RouteCollection $routeCollection,
|
||||
array $appConfig,
|
||||
string $requestMethod,
|
||||
string $requestUrl
|
||||
) {
|
||||
set_error_handler([$this, 'exceptionsErrorHandler']);
|
||||
|
||||
$this->request = $request;
|
||||
$this->dbConnection = $dbConnection;
|
||||
$this->routeCollection = $routeCollection;
|
||||
$this->appConfig = $appConfig;
|
||||
$this->method = strtolower($requestMethod);
|
||||
@ -75,7 +84,16 @@ class HttpResponse
|
||||
return;
|
||||
}
|
||||
|
||||
$response = call_user_func([$controller, $handler[1]]);
|
||||
$this->dbConnection->startTransaction();
|
||||
try {
|
||||
$response = call_user_func([$controller, $handler[1]]);
|
||||
} catch (Exception $exception) {
|
||||
$this->dbConnection->rollback();
|
||||
$this->render500($exception);
|
||||
return;
|
||||
}
|
||||
$this->dbConnection->commit();
|
||||
|
||||
if ($response instanceof IContent) {
|
||||
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
|
||||
$response->render();
|
||||
@ -106,4 +124,21 @@ class HttpResponse
|
||||
header('Content-Type: text/html; charset=UTF-8', true, 404);
|
||||
$content->render();
|
||||
}
|
||||
|
||||
private function render500(Exception $exception): void
|
||||
{
|
||||
if (empty($_ENV['DEV'])) {
|
||||
$exceptionToPrint = null;
|
||||
} else {
|
||||
$exceptionToPrint = (string)$exception;
|
||||
}
|
||||
|
||||
$content = new HtmlContent($this->appConfig['error500View'], ['exceptionToPrint' => $exceptionToPrint]);
|
||||
header('Content-Type: text/html; charset=UTF-8', true, 500);
|
||||
$content->render();
|
||||
}
|
||||
|
||||
private function exceptionsErrorHandler($severity, $message, $filename, $lineno) {
|
||||
throw new ErrorException($message, 0, $severity, $filename, $lineno);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user