Add ResponseInterface::prepare() step to pre-process responses according to the request

This commit is contained in:
Giuseppe Criscione 2024-10-21 22:16:11 +02:00
parent cb81de616f
commit 1f6937e54c
6 changed files with 20 additions and 4 deletions

View File

@ -138,9 +138,11 @@ final class App
DynamicFieldValue::$vars = $this->container->call(require $this->config()->get('system.fields.dynamic.vars.file'));
$request = $this->request();
$response = $this->router()->dispatch();
$response->send();
$response->prepare($request)->send();
return $response;
}

View File

@ -34,11 +34,11 @@ class ErrorHandlers
Response::cleanOutputBuffers();
if ($this->request->isXmlHttpRequest()) {
JsonResponse::error('Error', $responseStatus)->send();
JsonResponse::error('Error', $responseStatus)->prepare($this->request)->send();
} else {
$view = $this->viewFactory->make('errors.error', ['status' => $responseStatus->code(), 'message' => $responseStatus->message(), 'throwable' => $throwable]);
$response = new Response($view->render(), $responseStatus);
$response->send();
$response->prepare($this->request)->send();
// Don't exit, otherwise the error will not be logged
}
}

View File

@ -56,6 +56,14 @@ class Response implements ResponseInterface
return $this->headers;
}
/**
* Prepare response according to the given HTTP request
*/
public function prepare(Request $request): static
{
return $this;
}
/**
* Send HTTP status
*/

View File

@ -35,6 +35,11 @@ interface ResponseInterface extends ArraySerializable
*/
public function headers(): array;
/**
* Prepare response according to the given HTTP request
*/
public function prepare(Request $request): static;
/**
* Send HTTP status
*/

View File

@ -224,6 +224,7 @@ abstract class AbstractController extends BaseAbstractController
if (!$this->user()->permissions()->has($permission)) {
$this->container->build(ErrorsController::class)
->forbidden()
->prepare($this->request)
->send();
exit;
}

View File

@ -66,7 +66,7 @@ class PanelServiceLoader implements ResolutionAwareServiceLoaderInterface
if ($service->isLoggedIn() && $this->config->get('system.errors.setHandlers')) {
$errorsController = $this->container->build(ErrorsController::class);
set_exception_handler(function (Throwable $throwable) use ($errorsController): never {
$errorsController->internalServerError($throwable)->send();
$errorsController->internalServerError($throwable)->prepare($this->request)->send();
throw $throwable;
});
}