X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=app%2FWeb.php;h=0353a996c2b75e97a9a653c9495395a1684ce458;hb=b0a2ea11d6b820abe4758aaf56e6d226ded72942;hp=c9659b68231f276ffd4951b15173c196b2b37acd;hpb=930d06a96fa24317f053fecb8569aacaa69a0778;p=pharext%2Fpharext.org diff --git a/app/Web.php b/app/Web.php index c9659b6..0353a99 100644 --- a/app/Web.php +++ b/app/Web.php @@ -29,28 +29,36 @@ class Web switch ($route[0]) { case Dispatcher::NOT_FOUND: - $this->response->setResponseCode(404); - $this->response->getBody()->append($this->view->render("404")); + $this->display(404, null, 404); break; case Dispatcher::METHOD_NOT_ALLOWED: - $this->response->setResponseCode(405); - $this->response->setHeader("Allowed", $route[1]); - $this->response->getBody()->append($this->view->render("405")); + $this->display(405, null, 405, ["Allowed" => $route[1]]); break; case Dispatcher::FOUND: list(, $handler, $args) = $route; - $handler(array_map("urldecode", $args)); + try { + $handler(array_map("urldecode", $args)); + } catch (\Exception $exception) { + self::cleanBuffers(); + $this->display(500, compact("exception"), 500, ["X-Exception", get_class($exception)]); + } break; } $this->response->send(); } - function display($view, array $data = []) { + function display($view, array $data = null, $code = null, array $headers = []) { + if (isset($code)) { + $this->response->setResponseCode($code); + } + if ($headers) { + $this->response->addHeaders($headers); + } $this->response->getBody()->append( - $this->view->render($view, $data)); + $this->view->render($view, (array) $data)); } function redirect($url, $code = 302) { @@ -73,4 +81,12 @@ class Web function getResponse() { return $this->response; } + + static function cleanBuffers() { + while (ob_get_level()) { + if (!@ob_end_clean()) { + break; + } + } + } }