X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=app%2FWeb.php;h=b7bc23ceea1456fed244bb786a02e5a142081a62;hb=15de85bf59fbf8f3fa9b94bddba91ccc68b701b8;hp=e9d5d1659e9d59481b5c60239e4ac0bb2e2f74d5;hpb=ebc0d017c8a24bd16ca1f4347b39b07ba4349135;p=pharext%2Fpharext.org diff --git a/app/Web.php b/app/Web.php index e9d5d16..b7bc23c 100644 --- a/app/Web.php +++ b/app/Web.php @@ -2,7 +2,6 @@ namespace app; -use http\Url; use http\Env\Request; use http\Env\Response; @@ -20,38 +19,50 @@ class Web $this->baseUrl = $baseUrl; $this->request = $request; $this->response = $response; - $this->view = $view; + $this->view = $view->addData(["location" => null]); ob_start($response); } function __invoke(Dispatcher $dispatcher) { - $route = $dispatcher->dispatch($this->request->getRequestMethod(), - $this->baseUrl->pathinfo($this->request->getRequestUrl())); - - switch ($route[0]) { - case Dispatcher::NOT_FOUND: - $this->response->setResponseCode(404); - $this->response->getBody()->append($this->view->render("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")); - break; - - case Dispatcher::FOUND: - list(, $handler, $args) = $route; - $handler(array_map("urldecode", $args)); - break; + if (!file_exists("../config/maintenance")) { + $route = $dispatcher->dispatch($this->request->getRequestMethod(), + $this->baseUrl->pathinfo($this->request->getRequestUrl())); + + switch ($route[0]) { + case Dispatcher::NOT_FOUND: + $this->display(404, null, 404); + break; + + case Dispatcher::METHOD_NOT_ALLOWED: + $this->display(405, null, 405, ["Allowed" => $route[1]]); + break; + + case Dispatcher::FOUND: + list(, $handler, $args) = $route; + try { + $handler(array_map("urldecode", $args)); + } catch (\Exception $exception) { + self::cleanBuffers(); + $this->display(500, compact("exception"), 500, ["X-Exception", get_class($exception)]); + } + break; + } + } else { + $this->display(503, null, 503); } $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) { @@ -74,4 +85,12 @@ class Web function getResponse() { return $this->response; } + + static function cleanBuffers() { + while (ob_get_level()) { + if (!@ob_end_clean()) { + break; + } + } + } }