diff --git a/Documentation/php.puml b/Documentation/php.puml new file mode 100644 index 0000000..8641900 --- /dev/null +++ b/Documentation/php.puml @@ -0,0 +1,8 @@ +@startuml + +class FrontController { + - router : AltoRouter + +} + +@enduml \ No newline at end of file diff --git a/public/index.php b/public/index.php index ba9d7c0..9b36d85 100644 --- a/public/index.php +++ b/public/index.php @@ -18,8 +18,7 @@ use App\Validation\ValidationFail; use App\Controller\ErrorController; -$loader = new FilesystemLoader('../src/Views/'); -$twig = new \Twig\Environment($loader); + $basePath = get_public_path(); $con = new Connexion(get_database()); @@ -28,49 +27,7 @@ $con = new Connexion(get_database()); $router = new AltoRouter(); $router->setBasePath($basePath); -$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); -$editorController = new EditorController(new TacticModel(new TacticInfoGateway($con))); - - -$router->map("GET", "/", fn() => $sampleFormController->displayFormReact()); -$router->map("POST", "/submit", fn() => $sampleFormController->submitFormReact($_POST)); -$router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); -$router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST)); -$router->map("GET", "/tactic/new", fn() => $editorController->makeNew()); -$router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => $editorController->openEditorFor($id)); - -$match = $router->match(); - -if ($match == null) { - http_response_code(404); - ErrorController::displayFailures([ValidationFail::notFound("Cette page n'existe pas")], $twig); - return; -} - -$response = call_user_func_array($match['target'], $match['params']); - -http_response_code($response->getCode()); - -if ($response instanceof ViewHttpResponse) { - $file = $response->getFile(); - $args = $response->getArguments(); - - switch ($response->getViewKind()) { - case ViewHttpResponse::REACT_VIEW: - send_react_front($file, $args); - break; - case ViewHttpResponse::TWIG_VIEW: - try { - $twig->display($file, $args); - } catch (\Twig\Error\RuntimeError|\Twig\Error\SyntaxError $e) { - http_response_code(500); - echo "There was an error rendering your view, please refer to an administrator.\nlogs date: " . date("YYYD, d M Y H:i:s"); - throw e; - } - break; - } +//$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); +//$editorController = new EditorController(new TacticModel(new TacticInfoGateway($con))); -} else if ($response instanceof JsonHttpResponse) { - header('Content-type: application/json'); - echo $response->getJson(); -} \ No newline at end of file +$frontController = new FrontController($router); \ No newline at end of file diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php new file mode 100644 index 0000000..5963292 --- /dev/null +++ b/src/Controller/FrontController.php @@ -0,0 +1,72 @@ +router = $router; + } + + public function main() { + $this->toRoute(); + + } + + private function toRoute(){ + $this->router->map("GET", "/", fn() => $this->userControler->home()); +// $router->map("GET", "/", fn() => $sampleFormController->displayFormReact()); +// $router->map("POST", "/submit", fn() => $sampleFormController->submitFormReact($_POST)); +// $router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); +// $router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST)); +// $router->map("GET", "/tactic/new", fn() => $editorController->makeNew()); +// $router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => $editorController->openEditorFor($id)); + + + $match = $this->router->match(); + + if ($match == null) { + http_response_code(404); + ErrorController::displayFailures([ValidationFail::notFound("Cette page n'existe pas")], $twig); + return; + } + + $response = call_user_func_array($match['target'], $match['params']); + if ($response instanceof ViewHttpResponse) { + $file = $response->getFile(); + $args = $response->getArguments(); + + switch ($response->getViewKind()) { + case ViewHttpResponse::REACT_VIEW: + send_react_front($file, $args); + break; + case ViewHttpResponse::TWIG_VIEW: + try { + $loader = new FilesystemLoader('../src/Views/'); + $twig = new \Twig\Environment($loader); + $twig->display($file, $args); + } catch (\Twig\Error\RuntimeError|\Twig\Error\SyntaxError $e) { + http_response_code(500); + echo "There was an error rendering your view, please refer to an administrator.\nlogs date: " . date("YYYD, d M Y H:i:s"); + throw e; + } + break; + } + + } else if ($response instanceof JsonHttpResponse) { + header('Content-type: application/json'); + echo $response->getJson(); + } + + http_response_code($response->getCode()); + } + + +} \ No newline at end of file diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php new file mode 100644 index 0000000..e69de29