/** * Invoke the application. * * - Convert the PSR request/response into CakePHP equivalents. * - Create the controller that will handle this request. * - Invoke the controller. * * @param \Psr\Http\Message\ServerRequestInterface $request The request * @param \Psr\Http\Message\ResponseInterface $response The response * @param callable $next The next middleware * @return \Psr\Http\Message\ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) { // Convert the request/response to CakePHP equivalents. $cakeRequest = RequestTransformer::toCake($request); $cakeResponse = ResponseTransformer::toCake($response); // Dispatch the request/response to CakePHP $cakeResponse = $this->getDispatcher()->dispatch($cakeRequest, $cakeResponse); // Convert the response back into a PSR7 object. return ResponseTransformer::toPsr($cakeResponse); }
/** * Handle an exception and generate an error response * * @param \Exception $exception The exception to handle. * @param \Psr\Http\Message\ServerRequestInterface $request The request. * @param \Psr\Http\Message\ResponseInterface $response The response. * @return \Psr\Http\Message\ResponseInterface A response */ public function handleException($exception, $request, $response) { $renderer = $this->getRenderer($exception); try { $res = $renderer->render(); $this->logException($request, $exception); return ResponseTransformer::toPsr($res); } catch (\Exception $e) { $this->logException($request, $e); $body = $response->getBody(); $body->write('An Internal Server Error Occurred'); $response = $response->withStatus(500)->withBody($body); } return $response; }