From 0a4ef194361dd3b71634006c1b1de2c6cd69e67e Mon Sep 17 00:00:00 2001 From: Override-6 Date: Tue, 21 Nov 2023 21:27:27 +0100 Subject: [PATCH] fix errors, format, phpstan --- public/index.php | 7 --- src/Controller/EditorController.php | 4 -- src/Controller/FrontController.php | 94 ++++++++++------------------- src/Controller/UserController.php | 7 ++- 4 files changed, 38 insertions(+), 74 deletions(-) diff --git a/public/index.php b/public/index.php index ea12e5f..f898d7f 100644 --- a/public/index.php +++ b/public/index.php @@ -6,15 +6,8 @@ require "../sql/database.php"; require "utils.php"; require "../src/react-display.php"; -use App\Connexion; use App\Controller\FrontController; -session_start(); - $basePath = get_public_path(); - - - $frontController = new FrontController($basePath); $frontController->run(); - diff --git a/src/Controller/EditorController.php b/src/Controller/EditorController.php index 32a0fd5..eb07184 100644 --- a/src/Controller/EditorController.php +++ b/src/Controller/EditorController.php @@ -6,7 +6,6 @@ use App\Connexion; use App\Data\TacticInfo; use App\Gateway\TacticInfoGateway; use App\Http\HttpCodes; -use App\Http\HttpRequest; use App\Http\HttpResponse; use App\Http\JsonHttpResponse; use App\Http\ViewHttpResponse; @@ -15,9 +14,6 @@ use App\Model\TacticModel; class EditorController { private TacticModel $model; - /** - * @param TacticModel $model - */ public function __construct() { $this->model = new TacticModel(new TacticInfoGateway(new Connexion(get_database()))); } diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php index 491323e..66d5d4f 100644 --- a/src/Controller/FrontController.php +++ b/src/Controller/FrontController.php @@ -3,17 +3,18 @@ namespace App\Controller; use AltoRouter; -use App\Gateway\FormResultGateway; use App\Http\HttpCodes; use App\Http\HttpResponse; use App\Http\JsonHttpResponse; use App\Http\ViewHttpResponse; use Exception; +use Twig\Environment; +use Twig\Error\LoaderError; +use Twig\Error\RuntimeError; +use Twig\Error\SyntaxError; use Twig\Loader\FilesystemLoader; - class FrontController { - private AltoRouter $router; public function __construct(string $basePath) { @@ -27,16 +28,12 @@ class FrontController { * @return void */ public function run(): void { - $this->initializeRouterMap(); - $match = $this->router->match(); if ($match != null) { $this->handleMatch($match); } else { - $this->diplayViewByKind(ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND)); + $this->displayViewByKind(ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND)); } - // $this->handleMatch($match["target"]); - // $this->handleByResponseType($this->matchRoute()); } /** @@ -60,34 +57,32 @@ class FrontController { $this->router->map("GET", "/", "UserController"); $this->router->map("GET", "/[a:action]?", "UserController"); $this->router->map("GET", "/tactic/[a:action]/[i:idTactic]?", "EditorController"); - // $this->router->map("GET", "/tactic/[i:id]/edit", "EditorController"); - // $this->router->map("GET", "/", fn() => (new UserController())->home()); - // $this->router->map("GET", "/tactic/new", fn() => (new EditorController(new TacticModel(new TacticInfoGateway($this->con))))->makeNew()); - // $this->router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => (new EditorController(new TacticModel(new TacticInfoGateway($this->con))))->openEditorFor($id)); } /** - * Call - * - * @return ViewHttpResponse + * @param array $match + * @return void */ - private function handleMatch($match) { + private function handleMatch(array $match): void { $tag = $match['target']; $action = $this->getAction($match); - $this->handleResponseByType($this->tryToCall($tag, $action, $match["params"])); + $params = $match["params"]; + unset($params['action']); + $this->handleResponseByType($this->tryToCall($tag, $action, array_values($params))); } - // private function sanitizeParam($params) { - // foreach ($key, $value : ) - // } - - private function tryToCall($controller, $action, array $params) { - unset($params["action"]); - $controller = $this->initControllerByRole($controller); + /** + * @param string $controller + * @param string $action + * @param array $params + * @return HttpResponse + */ + private function tryToCall(string $controller, string $action, array $params): HttpResponse { + $controller = $this->getController($controller); try { - if (is_callable(array($controller, $action))) { - return call_user_func_array(array($controller, $action), $params); + if (is_callable([$controller, $action])) { + return call_user_func_array([$controller, $action], $params); } else { return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND); } @@ -99,62 +94,39 @@ class FrontController { /** * Get the right method to call to do an action * - * @param array $match + * @param array $match * @return string */ private function getAction(array $match): string { if (isset($match["params"]["action"])) { return $match["params"]["action"]; } - return "home"; + return "default"; } /** * Initialize the right controller by the user's role * * @param string $controller - * @return void + * @return mixed */ - private function initControllerByRole(string $controller) { - - $index = $controller; + private function getController(string $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/remplacer quand méthode de connexion disponible - - // $connected = (new UserController())->login($this->dictControllerRole[$controller]); - // if (!$connected){ - // return "null"; - // } - - $_SESSION['role'] = 'public'; // Remplacer par appel de la méthode de connexion - $controller = new $controller(); - - return $controller; + return new $controller(); } /** * Redirect the return by the response's type * - * @param array $match + * @param HttpResponse $response * @return 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->diplayViewByKind($response); - - } else if ($response instanceof JsonHttpResponse) { + $this->displayViewByKind($response); + } elseif ($response instanceof JsonHttpResponse) { header('Content-type: application/json'); echo $response->getJson(); } @@ -166,7 +138,7 @@ class FrontController { * @param ViewHttpResponse $response * @return void */ - private function diplayViewByKind(ViewHttpResponse $response): void { + private function displayViewByKind(ViewHttpResponse $response): void { $file = $response->getFile(); $args = $response->getArguments(); @@ -177,9 +149,9 @@ class FrontController { case ViewHttpResponse::TWIG_VIEW: try { $loader = new FilesystemLoader('../src/Views/'); - $twig = new \Twig\Environment($loader); + $twig = new Environment($loader); $twig->display($file, $args); - } catch (\Twig\Error\RuntimeError|\Twig\Error\SyntaxError $e) { + } catch (RuntimeError|SyntaxError|LoaderError $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; @@ -187,4 +159,4 @@ class FrontController { break; } } -} \ No newline at end of file +} diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 4246e66..930cd67 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -6,8 +6,11 @@ use App\Http\HttpResponse; use App\Http\ViewHttpResponse; class UserController { - - public function home() : HttpResponse { + public function home(): HttpResponse { return ViewHttpResponse::twig("home.twig", []); } + + public function default(): HttpResponse { + return self::home(); + } }