fix packaging
[pharext/pharext.org] / app / Web.php
index 39cfe8d8e4ccd877f9b59401d94f159b6a5939e3..b7bc23ceea1456fed244bb786a02e5a142081a62 100644 (file)
@@ -2,7 +2,6 @@
 
 namespace app;
 
-use http\Url;
 use http\Env\Request;
 use http\Env\Response;
 
@@ -25,33 +24,45 @@ class Web
        }
 
        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;
+                       }
+               }
+       }
 }