/** * Builds a response for an exception * @param \Exception $exception * @return \PHPixie\HTTP\Responses\Response */ public function process($exception) { $templateName = $this->configData->getRequired('template'); $trace = $this->debug->exceptionTrace($exception); $body = $this->template->render($templateName, array('exception' => $exception, 'trace' => $trace, 'logger' => $this->debug->logger())); return $this->http->responses()->response($body, array(), 500); }
/** * @param $data * @param int $code */ private function response($data, $code = 200) { $serializer = new \Zumba\Util\JsonSerializer(); $json = $serializer->serialize($data); /** @var Responses $responses */ $responses = $this->http->responses(); $response = $responses->string($json); $response->headers()->set('Content-Type', 'application/json; charset=utf-8'); $response->setStatus($code); $this->http->output($response); die; }
/** * Convert data to a HTTP response * @param mixed $value * @return \PHPixie\HTTP\Responses\Response * @throws \PHPixie\HTTPProcessors\Exception If data type is not supported */ public function process($value) { if ($value instanceof \PHPixie\HTTP\Responses\Response) { return $value; } if ($value instanceof \Psr\Http\Message\ResponseInterface) { return $value; } if ($value instanceof \PHPixie\Template\Container) { $value = $value->render(); } if (is_string($value)) { return $this->http->responses()->string($value); } if (is_object($value) || is_array($value)) { return $this->http->responses()->json($value); } $type = gettype($value); throw new \PHPixie\HTTPProcessors\Exception("Cannot convert type '{$type}' into a response"); }
/** * Build a response for when a uri does not exist * @param \PHPixie\HTTP\Request $request * @return \PHPixie\HTTP\Responses\Response */ public function process($request) { $templateName = $this->configData->getRequired('template'); $body = $this->template->render($templateName, array('request' => $request)); return $this->http->responses()->response($body, array(), 404); }