From ff9f41f18c218177a8345efbb191a5cd5a517a95 Mon Sep 17 00:00:00 2001 From: DahmaneYanis Date: Tue, 21 Nov 2023 11:44:40 +0100 Subject: [PATCH] FrontController done --- public/index.php | 21 +----- src/Controller/EditorController.php | 10 +-- src/Controller/FrontController.php | 100 ++++++++++++++++++---------- src/Controller/UserController.php | 2 - 4 files changed, 73 insertions(+), 60 deletions(-) diff --git a/public/index.php b/public/index.php index eb580ce..5141aa8 100644 --- a/public/index.php +++ b/public/index.php @@ -8,31 +8,12 @@ require "../src/react-display.php"; use App\Connexion; use App\Controller\FrontController; -use App\Controller\EditorController; -use App\Controller\SampleFormController; -use App\Gateway\FormResultGateway; -use App\Gateway\TacticInfoGateway; -use App\Http\JsonHttpResponse; -use App\Http\ViewHttpResponse; -use App\Model\TacticModel; -use Twig\Loader\FilesystemLoader; -use App\Validation\ValidationFail; -use App\Controller\ErrorController; session_start(); $basePath = get_public_path(); -$con = new Connexion(get_database()); -global $dictActionRole; -$dictActionRole = [ - "UserController" => "public", - "EditionUserController" => "public" -]; -$dict = [ - "new"; -] -$frontController = new FrontController($con, $basePath, $dictActionRole); +$frontController = new FrontController($basePath); $frontController->run(); \ No newline at end of file diff --git a/src/Controller/EditorController.php b/src/Controller/EditorController.php index bf5dccc..2dd2cbb 100644 --- a/src/Controller/EditorController.php +++ b/src/Controller/EditorController.php @@ -2,7 +2,9 @@ namespace App\Controller; +use App\Connexion; use App\Data\TacticInfo; +use App\Gateway\TacticInfoGateway; use App\Http\HttpCodes; use App\Http\HttpRequest; use App\Http\HttpResponse; @@ -17,15 +19,15 @@ class EditorController { /** * @param TacticModel $model */ - public function __construct(TacticModel $model) { - $this->model = $model; + public function __construct() { + $this->model = new TacticModel(new TacticInfoGateway(new Connexion(get_database()))); } private function openEditor(TacticInfo $tactic): HttpResponse { return ViewHttpResponse::react("views/Editor.tsx", ["name" => $tactic->getName(), "id" => $tactic->getId()]); } - public function makeNew(): HttpResponse { + public function create(): HttpResponse { $tactic = $this->model->makeNewDefault(); return $this->openEditor($tactic); } @@ -35,7 +37,7 @@ class EditorController { * @param int $id the targeted tactic identifier * @return HttpResponse */ - public function openEditorFor(int $id): HttpResponse { + public function edit(int $id): HttpResponse { $tactic = $this->model->get($id); if ($tactic == null) { diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php index 6d265f6..98c5eaf 100644 --- a/src/Controller/FrontController.php +++ b/src/Controller/FrontController.php @@ -2,7 +2,7 @@ namespace App\Controller; - +use App\Controller; use App\Connexion; use App\Controller\UserController; @@ -16,6 +16,7 @@ use App\Http\JsonHttpResponse; use App\Http\ViewHttpResponse; use App\Model\TacticModel; use App\Validation\ValidationFail; +use Exception; use Twig\Loader\FilesystemLoader; @@ -25,10 +26,13 @@ class FrontController{ private Connexion $con; private array $dictControllerRole; - public function __construct(Connexion $con, string $basePath, array $dictControllerRole) { - $this->con = $con; + public function __construct(string $basePath) { + $this->con = new Connexion(get_database());; $this->router = $this->createRouter($basePath); - $this->dictControllerRole = $dictControllerRole; + $this->dictControllerRole = [ + "UserController" => "public", + "EditorController" => "public" + ]; } /** @@ -39,12 +43,11 @@ class FrontController{ public function run() : void { $this->initializeRouterMap(); - $match = $this->router->match(); - if ($this->validMatch($match)){ - var_dump($match); - $this->controlRoute($match['target']); + $match = $this->router->match(); + if ($match != null){ + $this->controlRoute($match); } else { - $this->displayByViewKind(ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND)); + $this->diplayViewByKind(ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND)); } // $this->controlRoute($match["target"]); // $this->handleByResponseType($this->matchRoute()); @@ -78,53 +81,82 @@ class FrontController{ } - private function validMatch($match){ - return $match != null; - } - /** * Initialize router's settings * * @return ViewHttpResponse */ - private function controlRoute(string $tag){ - if (isset($_SESSION['role'])){ - if($_SESSION['role'] = $this->dictControllerRole[$tag]){ - $controller = new $tag(); + private function controlRoute($match){ + $tag = $match['target']; + + $action = $this->getAction($match); + $this->handleResponseByType($this->tryToCall($tag, $action, $match["params"])); + } + + // private function sanitizeParam($params) { + // foreach ($key, $value : ) + // } + + private function tryToCall($controller, $action, array $params){ + unset($params["action"]); + $controller = $this->initControllerByRole($controller); + try { + if (is_callable(array($controller, $action))){ + return call_user_func_array(array($controller, $action), $params); + } else { + return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND); } } - else { - $_SESSION['role'] = 'public'; // Remplacer par appel de la méthode de connexion + catch (Exception $e) { + return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND); + } + } + + private function getAction($match) : string { + if (isset($match["params"]["action"])){ + return $match["params"]["action"]; + } + return "home"; + } + + private function initControllerByRole(string $controller) { + + $index = $controller; + $namespace = "\\App\\Controller\\"; + $controller = $namespace.$controller; + + + if (isset($_SESSION['role'])){ + if ($_SESSION['role'] == $this->dictControllerRole[$index]){ + $controller = new $controller(); + return $controller; + } } + // A décommenter quand méthode de connexion disponible - // // Page not found - // if ($match == null) { - // return new ViewHttpResponse(ViewHttpResponse::TWIG_VIEW, "Views/error.html.twig", [ValidationFail::notFound("Cette page n'existe pas")], HttpCodes::NOT_FOUND); + // $connected = (new UserController())->login($this->dictControllerRole[$controller]); + // if (!$connected){ + // return "null"; // } - // // $loader = new FilesystemLoader('../src/Views/'); - // // $twig = new \Twig\Environment($loader); - // // http_response_code(HttpCodes::NOT_FOUND); - // // ErrorController::displayFailures([ValidationFail::notFound("Cette page n'existe pas")], $twig); - // // return; - // // } - } + $_SESSION['role'] = 'public'; // Remplacer par appel de la méthode de connexion + $controller = new $controller(); - private function controlRole($match){ - + return $controller; } + /** * Redirect the return of the response by the response's type * * @param array $match * @return void */ - private function handleByResponseType(HttpResponse $response) : void { + private function handleResponseByType(HttpResponse $response) : void { // $response = call_user_func_array($match['target'], $match['params']); http_response_code($response->getCode()); if ($response instanceof ViewHttpResponse) { - $this->displayByViewKind($response); + $this->diplayViewByKind($response); } else if ($response instanceof JsonHttpResponse) { header('Content-type: application/json'); @@ -138,7 +170,7 @@ class FrontController{ * @param ViewHttpResponse $response * @return void */ - private function displayByViewKind(ViewHttpResponse $response) : void { + private function diplayViewByKind(ViewHttpResponse $response) : void { $file = $response->getFile(); $args = $response->getArguments(); diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 701fa0f..4246e66 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -9,7 +9,5 @@ class UserController { public function home() : HttpResponse { return ViewHttpResponse::twig("home.twig", []); - - } }