addFunction(new TwigFunction('path', fn(string $str) => "$basePath$str")); return $twig; } function getRoutes(): AltoRouter { global $basePath; $ar = new AltoRouter(); $ar->setBasePath($basePath); //authentication $ar->map("GET", "/login", Action::noAuth(fn() => getAuthController()->displayLogin())); $ar->map("GET", "/register", Action::noAuth(fn() => getAuthController()->displayRegister())); $ar->map("POST", "/login", Action::noAuth(fn(SessionHandle $s) => getAuthController()->login($_POST, $s))); $ar->map("POST", "/register", Action::noAuth(fn(SessionHandle $s) => getAuthController()->register($_POST, $s))); //user-related $ar->map("GET", "/", Action::auth(fn(SessionHandle $s) => getUserController()->home($s))); $ar->map("GET", "/home", Action::auth(fn(SessionHandle $s) => getUserController()->home($s))); $ar->map("GET", "/settings", Action::auth(fn(SessionHandle $s) => getUserController()->settings($s))); $ar->map("GET", "/disconnect", Action::auth(fn(MutableSessionHandle $s) => getUserController()->disconnect($s))); //tactic-related $ar->map("GET", "/tactic/[i:id]/view", Action::auth(fn(int $id, SessionHandle $s) => getVisualizerController()->openVisualizer($id, $s))); $ar->map("GET", "/tactic/[i:id]/edit", Action::auth(fn(int $id, SessionHandle $s) => getEditorController()->openEditor($id, $s))); // don't require an authentication to run this action. // If the user is not connected, the tactic will never save. $ar->map("GET", "/tactic/new", Action::noAuth(fn() => getEditorController()->createNew())); $ar->map("GET", "/tactic/new/plain", Action::noAuth(fn(SessionHandle $s) => getEditorController()->createNewOfKind(CourtType::plain(), $s))); $ar->map("GET", "/tactic/new/half", Action::noAuth(fn(SessionHandle $s) => getEditorController()->createNewOfKind(CourtType::half(), $s))); //team-related $ar->map("GET", "/team/new", Action::auth(fn(SessionHandle $s) => getTeamController()->displayCreateTeam($s))); $ar->map("POST", "/team/new", Action::auth(fn(SessionHandle $s) => getTeamController()->submitTeam($_POST, $s))); $ar->map("GET", "/team/search", Action::auth(fn(SessionHandle $s) => getTeamController()->displayListTeamByName($s))); $ar->map("POST", "/team/search", Action::auth(fn(SessionHandle $s) => getTeamController()->listTeamByName($_POST, $s))); $ar->map("GET", "/team/[i:id]", Action::auth(fn(int $id, SessionHandle $s) => getTeamController()->displayTeam($id, $s))); $ar->map("GET", "/team/members/add", Action::auth(fn(SessionHandle $s) => getTeamController()->displayAddMember($s))); $ar->map("POST", "/team/members/add", Action::auth(fn(SessionHandle $s) => getTeamController()->addMember($_POST, $s))); $ar->map("GET", "/team/members/remove", Action::auth(fn(SessionHandle $s) => getTeamController()->displayDeleteMember($s))); $ar->map("POST", "/team/members/remove", Action::auth(fn(SessionHandle $s) => getTeamController()->deleteMember($_POST, $s))); //temp $ar->map("GET", "/test", Action::auth(fn(SessionHandle $s) => getUserController()->homeTwig($s))); return $ar; } function runMatch($match, MutableSessionHandle $session): HttpResponse { global $basePath; if (!$match) { return ViewHttpResponse::twig("error.html.twig", [ 'failures' => [ValidationFail::notFound("Could not find page {$_SERVER['REQUEST_URI']}.")], ], HttpCodes::NOT_FOUND); } return App::runAction($basePath . '/login', $match['target'], $match['params'], $session); } //this is a global variable $basePath = get_public_path(__DIR__); App::render(runMatch(getRoutes()->match(), PhpSessionHandle::init()), fn() => getTwig());