From 2eb5a432d2229788ce2fdb09f36c6f4bebdea813 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 7 Feb 2020 09:44:59 +0100 Subject: Initial commit --- src/Main.php | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/Main.php (limited to 'src/Main.php') diff --git a/src/Main.php b/src/Main.php new file mode 100644 index 0000000..443960b --- /dev/null +++ b/src/Main.php @@ -0,0 +1,58 @@ +addRoute(["GET"], "/", Index::class); + $r->addRoute(["GET", "POST"], "/new-note", NewNote::class); + $r->addRoute(["GET"], "/search", Search::class); + $r->addRoute(["GET", "POST"], "/n/{id}", NoteHandler::class); + $r->addRoute(["GET"], "/api/tags", ApiTagsHandler::class); + } + + public function run(Env $env) + { + $disp = simpleDispatcher(Closure::fromCallable([self::class, "buildRoutes"])); + + $uri = preg_replace('/\?.*$/', "", $_SERVER["REQUEST_URI"]); + $result = $disp->dispatch($_SERVER["REQUEST_METHOD"], $uri); + switch ($result[0]) { + case Dispatcher::NOT_FOUND: + $handlerCls = NotFoundHandler::class; + $vars = []; + break; + case Dispatcher::FOUND: + [, $handlerCls, $vars] = $result; + break; + case Dispatcher::METHOD_NOT_ALLOWED: + $handlerCls = MethodNotAllowedHandler::class; + $vars = ["allowed" => $result[1]]; + break; + default: + throw new \DomainException("Unexpected routing result: {$result[0]}"); + } + + $handler = new $handlerCls(); + if (!($handler instanceof Handler)) { + throw new \DomainException("handler is not an instance of ".Handler::class); + } + + $handler->handle($env, $vars); + } +} -- cgit v1.2.3-70-g09d2