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 handleRequest() { $request = $this->request; // Merge route parameters. $rp = Http::getRouteParameters($request); if ($rp) { $o = []; setAt($o, $this->mainSubModelPath, $rp); $this->merge($o); } switch ($request->getMethod()) { case 'GET': $old = $this->session->getOldInput(); if ($old) { $this->merge($this->parseFormData($old)); } break; case 'POST': $data = $request->getParsedBody(); if (isset($data)) { $contentType = $request->getHeaderLine('Content-Type'); if ($contentType == 'application/x-www-form-urlencoded' || str_beginsWith($contentType, 'multipart/form-data')) { $data = $this->parseFormData($data); } unset($data[PlatformModule::ACTION_FIELD]); $this->merge($data); } } $this->runExtensions(); }
/** * Utility method for retrieving the value of a form field submitted via a `application/x-www-form-urlencoded` or a * `multipart/form-data` POST request. * * @param string $name * @param mixed [optional] $def * @return mixed */ protected function formField($name, $def = null) { return Http::field($this->request, $name, $def); }
function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $path = either($request->getAttribute('virtualUri', '<i>not set</i>'), '<i>empty</i>'); $realPath = $request->getUri()->getPath(); return Http::response($response, "<br><br><table align=center cellspacing=20 style='text-align:left'>\n<tr><th>Virtual URL:<td><kbd>{$path}</kbd>\n<tr><th>URL path:<td><kbd>{$realPath}</kbd>\n</table>", 'text/html', 404); }
/** * Returns a route to a controller method or function. * * <p>The callable will receive as arguments the route parameters, followed by the request and the response objects. * <p>It can return: * - a response object * - a string (sent as text/html) * - `null` to send an empty response * - arrays, objects or scalars will be sent as JSON. * * @param string|array|callable $ref Either a Closure, a 'Class::method' string or a ['Class', 'method'] array or an * [$instance, 'method'] array. * @return FactoryRoutable * @throws Fault If an invalid data type is returned from the controller. */ function controller($ref) { return new FactoryRoutable(function (InjectorInterface $injector) use($ref) { $ctrl = $injector->buildExecutable($ref); return function (ServerRequestInterface $request, ResponseInterface $response) use($ctrl) { $args = array_merge(array_values(Http::getRouteParameters($request)), [$request, $response]); $result = $ctrl(...$args); switch (true) { case $result instanceof ResponseInterface: return $result; case is_string($result): break; case is_null($result): $result = ''; break; case is_array($result): case is_object($result): case is_scalar($result): return Http::jsonResponse($response, $result); default: throw new Fault(Faults::INVALID_RESPONSE_TYPE); } return Http::response($response, $result); }; }); }