/** * Send response. * * @param string $code * @param string $message * @param string $file * @param string $line * @param null $context * @param array|null $backtrace */ public static function render($code, $message, $file, $line, $context = null, array $backtrace = []) { ob_clean(); $data = ['message' => $message, 'code' => $code, 'file' => $file, 'line' => $line, 'trace' => $backtrace, 'context' => $context]; if (!empty($data['trace'])) { foreach ($data['trace'] as $k => $item) { if (isset($item['type'])) { switch ($item['type']) { case '->': $data['trace'][$k]['type'] = 'method'; break; case '::': $data['trace'][$k]['type'] = 'static method'; break; default: $data['trace'][$k]['type'] = 'function'; } } } } $template = new Native(dirname(__FILE__) . '/', null, true); $response = new Response(); $response->headerStatus(418); $response->setContent($template->getContent('catch.html.php', $data)); $response->send(); exit; }
public function testResponse() { $response = new Response(); $this->assertEmpty($response->getContent()); $response->setContent('test'); $this->assertEquals('test', $response->getContent()); }
public function testSetResponse() { $controller = new BaseController('', new Request()); $this->assertNull($controller->getResponse()); $response = new Response(); $response->setContent('test'); $controller->setResponse($response); $this->assertInstanceOf('Dspbee\\Core\\Response', $controller->getResponse()); $this->assertEquals('test', $controller->getResponse()->getContent()); }
/** * Process request. * * @param array $languageList * @param array $packageList * @param array $routeClassList * @param null|string $url * * @return Response */ public function run(array $languageList, array $packageList, array $routeClassList, $url = null) : Response { $request = new Request($languageList, $packageList, $url); $this->packageRoot .= $request->package() . '/'; /** * Process request. */ if (isset($routeClassList[$request->package()])) { /** * Custom routing. */ /** * Path to router class. */ $path = $this->packageRoot . $routeClassList[$request->package()] . '.php'; if (file_exists($path)) { require $path; /** * Name of router class. */ $route = $request->package() . '\\' . $routeClassList[$request->package()]; /** * @var IRoute $route */ $route = new $route(); $response = $route->getResponse($this->packageRoot, $request); if (null !== $response) { return $response; } } else { throw new \RuntimeException(sprintf('The file "%s" does not exist', $path)); } } $response = (new DefaultRoute())->getResponse($this->packageRoot, $request); if (null !== $response) { return $response; } /** * If not found. */ $response = new Response(); $response->headerStatus(404); $content = '404 Not Found'; if (file_exists($this->packageRoot . '/view/404.html.php')) { $content = (new Native($this->packageRoot))->getContent('404.html.php'); } $response->setContent($content); return $response; }