function render(ServerRequestInterface $request, ResponseInterface $response, $error = null) { if ($error) { // On debug mode, a debugging error popup is displayed for Exceptions/Errors. if ($this->devEnv && Http::clientAccepts($request, 'text/html')) { return ErrorConsole::display($error, $this->responseFactory->makeHtmlResponse()); } $status = $error instanceof HttpException ? $error->getCode() : 500; // Errors may contain an additional `getTitle()` method. if (method_exists($error, 'getTitle')) { // The title is assumed to be a plain, one-line string (no formatting). If not, make it so. $title = $error->getTitle(); $message = $error->getMessage(); } else { list($title, $message) = array_pad(explode(PHP_EOL, $error->getMessage(), 2), 2, ''); } $response = $response->withStatus($status); } else { $status = $response->getStatusCode(); $title = $response->getReasonPhrase(); $message = strval($response->getBody()); } /** @var ResponseInterface $response */ $response = $response->withBody($body = $this->responseFactory->makeBody()); // Otherwise, errors are rendered into a format accepted by the HTML client. if (Http::clientAccepts($request, 'text/html')) { $response = $response->withHeader('Content-Type', 'text/html'); $customRenderer = $this->settings->getCustomRenderer($status); if ($customRenderer) { if ($customRenderer instanceof RenderableInterface) { $class = $customRenderer->getContextClass(); $customRenderer->setContext($this->injector->make($class)); } $response = $customRenderer($request, $response, nop()); } else { ob_start(); $this->htmlTemplate($status, $title, $message); $body->write(ob_get_clean()); } } else { $title = strip_tags($title); $message = strip_tags($message); if (Http::clientAccepts($request, 'text/plain') || Http::clientAccepts($request, '*/*')) { $response = $response->withHeader('Content-Type', 'text/plain'); $body->write("{$title}\n{$message}"); } elseif (Http::clientAccepts($request, 'application/json')) { $response = $response->withHeader('Content-Type', 'application/json'); $body->write(json_encode(['error' => ['code' => $status, 'message' => $title, 'info' => $message]], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); } elseif (Http::clientAccepts($request, 'application/xml')) { $response = $response->withHeader('Content-Type', 'application/xml'); $body->write("<?xml version=\"1.0\"?><error><code>{$status}</code><message>{$title}</message><info>{$message}</info></error>"); } } // else render nothing return $response; }
function &gen(&$var) { (yield nop($var)); }