public function doRoute() { $klein = new Klein(); $this->config = new Config("../app/config/app.php"); $klein->respond("GET", $this->homePath, function () { $controller = new Home(new View\Home()); $controller->index(); }); $klein->respond("GET", $this->loginPath . "?", function ($request, $response) { $url = $this->auth->index(); $response->redirect($url); }); $klein->respond("GET", "/github-callback/?", function ($request, $response) { if ($request->code) { $this->auth->getToken($request->code); return $response->redirect($this->userPath); } return $response->redirect($this->homePath); }); $klein->respond("GET", $this->userPath . "?", function ($request, $response) { $session = new Session(); $token = $session->get($this->auth->getTokenSessionName()); if ($token) { return $this->auth->loggedInUser($token); } return $response->redirect($this->homePath); }); $klein->respond("GET", $this->logoutPath . "?", function ($request, $response) { $session = new Session(); $session->destroy($this->auth->getTokenSessionName()); return $response->redirect($this->homePath); }); $klein->with($this->usersPath, function () use($klein) { $klein->respond("GET", "/?", function ($request, $response) { $controller = new Users(); $controller->index($request->fetchFromID); }); $klein->respond("GET", "/[:username]/?", function ($request, $response) { $controller = new Users(); $controller->show($request->username); }); }); $klein->respond("GET", $this->searchPath . "?", function ($request, $response) { $controller = new Users(); $controller->search(); }); $klein->respond("GET", $this->followPath . "[:username]/?", function ($request, $response) { $controller = new Users(); $controller->follow($request->username); }); $klein->respond("GET", $this->unFollowPath . "[:username]/?", function ($request, $response) { $controller = new Users(); $controller->unFollow($request->username); }); $klein->respond("404", function ($request) { $page = $request->uri(); $controller = new Error(new View\Error()); $controller->pageNotFoundError($page); }); $klein->onError(function ($klein, $err_msg) { if ($this->config->get("DEBUG") === true) { throw new \Exception($err_msg); } else { $controller = new Error(new View\Error()); $controller->serverError(); error_log($err_msg, 3, $this->config->get("ERROR_LOG")); } }); $klein->dispatch(); }
$module_root = __DIR__ . '/modules/'; $module_files = utilities::glob_recursive($module_root . "*.php", GLOB_NOSORT); foreach ($module_files as $mfile) { $modules[] = (include $mfile); } foreach ($modules as $mj) { if (utilities::module_valid($mj)) { $app->respond($mj['methods'], $mj['route'], function (\Klein\Request $request, \Klein\Response $response) use($mj) { /* TODO: check callback access */ $answer = $mj['callback']($mj, $request, $response); if ($mj['response_type'] === "object") { $response_json = array("result" => array("code" => $answer['code'], "object" => $answer['object']), "data" => $answer['data']); return $response->json($response_json); } else { if ($mj['response_type'] === "array") { $response_json = array("result" => array("code" => $answer['code'], "object" => $answer['object']), "pagination" => array("window" => $answer['window'], "current" => $answer['current'], "next" => $answer['next'], "total" => $answer['total'], "pages" => $answer['pages']), "data" => $answer['data']); return $response->json($response_json); } else { if ($mj['response_type'] == "file") { } } } }); } else { } } $app->onError(function (\Klein\Klein $app, $message, $type, jsonui_exception $ex) { /** TODO: save report */ return $app->response()->json(array("result" => array("code" => $ex->getCode(), "message" => $ex->getMessage()))); }); $app->dispatch();