From 5737bd6e6b31631984c75211820ba8848e3148ff Mon Sep 17 00:00:00 2001 From: DahmaneYanis Date: Fri, 10 Nov 2023 15:26:32 +0100 Subject: [PATCH 01/10] Add start of homepage and FrontControler (don't run it) --- Documentation/php.puml | 8 ++++++ public/index.php | 23 ++++++------------ src/Controller/FrontController.php | 39 ++++++++++++++++++++++++++++++ src/Controller/UserController.php | 0 4 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 Documentation/php.puml create mode 100644 src/Controller/FrontController.php create mode 100644 src/Controller/UserController.php 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 4c5290b..d1fee44 100644 --- a/public/index.php +++ b/public/index.php @@ -34,19 +34,12 @@ $con = new Connexion(get_database()); $router = new AltoRouter(); $router->setBasePath($basePath); -$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); -$router->map("GET", "/", fn() => $sampleFormController->displayForm()); -$router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST)); -$router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); -$router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST)); - -$match = $router->match(); - -if ($match == null) { - // TODO redirect to a 404 not found page instead (issue #1) - http_response_code(404); - echo "Page non trouvée"; - exit(1); -} +$frontController = new FrontController($router); + +//$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); + + + + + -call_user_func($match['target']); diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php new file mode 100644 index 0000000..dcee0b1 --- /dev/null +++ b/src/Controller/FrontController.php @@ -0,0 +1,39 @@ +router = $router; + } + + public function main() { + $this->toRoute(); + + } + + private function toRoute(){ + $this->router->map("GET", "/", fn() => $this->userControler->home()); +// $this->router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST)); +// $this->router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); +// $this->router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST)); + + $match = $router->match(); + + // À remplacer par l'appel du contrôler d'erreur + if ($match == null) { + // TODO redirect to a 404 not found page instead (issue #1) + http_response_code(404); + echo "Page non trouvée"; + exit(1); + } + + call_user_func($match['target']); + } + + +} \ 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 -- 2.36.3 From ec474617035ab589fdc5e6ee2fe385c40b58b78f Mon Sep 17 00:00:00 2001 From: d_yanis Date: Tue, 14 Nov 2023 15:57:24 +0100 Subject: [PATCH 02/10] wip FRONT CONTROLLER --- composer.json | 1 + public/index.php | 11 +++-------- src/Controller/FrontController.php | 8 +++++--- src/Controller/UserController.php | 13 +++++++++++++ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index c78fb15..0f5e1f2 100644 --- a/composer.json +++ b/composer.json @@ -11,4 +11,5 @@ "ext-pdo_sqlite": "*", "twig/twig":"^2.0" } + } \ No newline at end of file diff --git a/public/index.php b/public/index.php index d1fee44..d4e227e 100644 --- a/public/index.php +++ b/public/index.php @@ -3,6 +3,7 @@ require "../vendor/autoload.php"; require "../config.php"; require "../sql/database.php"; +require "../src/Controller/FrontController.php"; use \Twig\Loader\FilesystemLoader; use App\Connexion; @@ -34,12 +35,6 @@ $con = new Connexion(get_database()); $router = new AltoRouter(); $router->setBasePath($basePath); -$frontController = new FrontController($router); - -//$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); - - - - - +$frontController = new FrontController($router, new UserController()); +//$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); \ No newline at end of file diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php index dcee0b1..7a1179b 100644 --- a/src/Controller/FrontController.php +++ b/src/Controller/FrontController.php @@ -1,14 +1,16 @@ router = $router; + $this->userControler = $userControler; } public function main() { @@ -22,7 +24,7 @@ class FrontController{ // $this->router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); // $this->router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST)); - $match = $router->match(); + $match = $this->router->match(); // À remplacer par l'appel du contrôler d'erreur if ($match == null) { diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index e69de29..0934a04 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -0,0 +1,13 @@ +Welcome!"; + } + +} +?> \ No newline at end of file -- 2.36.3 From 02dbf269f966f6a0ded0cd01748342b9bd3e3b8d Mon Sep 17 00:00:00 2001 From: DahmaneYanis Date: Fri, 10 Nov 2023 15:26:32 +0100 Subject: [PATCH 03/10] Add start of homepage and FrontControler (don't run it) Rebase modification --- Documentation/php.puml | 8 ++++ public/index.php | 51 ++------------------- src/Controller/FrontController.php | 72 ++++++++++++++++++++++++++++++ src/Controller/UserController.php | 0 4 files changed, 84 insertions(+), 47 deletions(-) create mode 100644 Documentation/php.puml create mode 100644 src/Controller/FrontController.php create mode 100644 src/Controller/UserController.php 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 -- 2.36.3 From 26a27c291d4d643f94f79b6fd22b5b48784b3b73 Mon Sep 17 00:00:00 2001 From: DahmaneYanis Date: Tue, 14 Nov 2023 17:35:01 +0100 Subject: [PATCH 04/10] Add FrontController and UserController --- public/index.php | 7 +-- src/Controller/FrontController.php | 72 +++++++++++++++++++----------- src/Controller/UserController.php | 17 +++++++ src/Views/home.twig | 13 ++++++ 4 files changed, 80 insertions(+), 29 deletions(-) create mode 100644 src/Views/home.twig diff --git a/public/index.php b/public/index.php index 9b36d85..4ccdf44 100644 --- a/public/index.php +++ b/public/index.php @@ -6,6 +6,7 @@ require "../sql/database.php"; require "utils.php"; use App\Connexion; +use App\Controller\FrontController; use App\Controller\EditorController; use App\Controller\SampleFormController; use App\Gateway\FormResultGateway; @@ -18,8 +19,6 @@ use App\Validation\ValidationFail; use App\Controller\ErrorController; - - $basePath = get_public_path(); $con = new Connexion(get_database()); @@ -27,7 +26,9 @@ $con = new Connexion(get_database()); $router = new AltoRouter(); $router->setBasePath($basePath); +$frontController = new FrontController($router); +$frontController->route(); + //$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); //$editorController = new EditorController(new TacticModel(new TacticInfoGateway($con))); -$frontController = new FrontController($router); \ No newline at end of file diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php index 5963292..b43023e 100644 --- a/src/Controller/FrontController.php +++ b/src/Controller/FrontController.php @@ -1,27 +1,37 @@ router = $router; + $this->userController = new UserController(); } - public function main() { - $this->toRoute(); + public function run() { + $this->route(); } - private function toRoute(){ - $this->router->map("GET", "/", fn() => $this->userControler->home()); + public function route() + { + $this->router->map("GET", "/", fn() => $this->userController->home()); // $router->map("GET", "/", fn() => $sampleFormController->displayFormReact()); // $router->map("POST", "/submit", fn() => $sampleFormController->submitFormReact($_POST)); // $router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); @@ -33,39 +43,49 @@ class FrontController{ $match = $this->router->match(); if ($match == null) { - http_response_code(404); + $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; } + $this->routeByResponseType($match); + } + private function routeByResponseType(array $match) + { $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 { - $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; - } + + $this->displayByViewKind($response); } else if ($response instanceof JsonHttpResponse) { header('Content-type: application/json'); echo $response->getJson(); } + } - http_response_code($response->getCode()); + private function displayByViewKind(ViewHttpResponse $response){ + $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; + } } diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index e69de29..5a7d85a 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -0,0 +1,17 @@ + \ No newline at end of file diff --git a/src/Views/home.twig b/src/Views/home.twig new file mode 100644 index 0000000..8b5ce94 --- /dev/null +++ b/src/Views/home.twig @@ -0,0 +1,13 @@ + + + + + + + Document + + +

Test

+ + \ No newline at end of file -- 2.36.3 From 051dbef9ed88597ea0baa94c2f10dae9c96beab2 Mon Sep 17 00:00:00 2001 From: DahmaneYanis Date: Tue, 14 Nov 2023 17:37:23 +0100 Subject: [PATCH 05/10] Add FrontController and UserController --- src/Controller/FrontController.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php index b43023e..c8b260d 100644 --- a/src/Controller/FrontController.php +++ b/src/Controller/FrontController.php @@ -26,7 +26,6 @@ class FrontController{ public function run() { $this->route(); - } public function route() @@ -49,10 +48,10 @@ class FrontController{ ErrorController::displayFailures([ValidationFail::notFound("Cette page n'existe pas")], $twig); return; } - $this->routeByResponseType($match); + $this->handleByResponseType($match); } - private function routeByResponseType(array $match) + private function handleByResponseType(array $match) { $response = call_user_func_array($match['target'], $match['params']); http_response_code($response->getCode()); -- 2.36.3 From 61a6b5afd5c3e72706981171a0bd0b9be8d7e1b2 Mon Sep 17 00:00:00 2001 From: DahmaneYanis Date: Fri, 17 Nov 2023 12:06:03 +0100 Subject: [PATCH 06/10] wip FrontController --- Documentation/http.puml | 2 +- public/index.php | 20 ++-- src/Controller/FrontController.php | 135 ++++++++++++++++++------ src/Controller/SampleFormController.php | 52 --------- src/Gateway/FormResultGateway.php | 33 ------ src/Views/home.twig | 2 +- src/Views/sample_form.html.twig | 20 ---- 7 files changed, 117 insertions(+), 147 deletions(-) delete mode 100644 src/Controller/SampleFormController.php delete mode 100644 src/Gateway/FormResultGateway.php delete mode 100644 src/Views/sample_form.html.twig diff --git a/Documentation/http.puml b/Documentation/http.puml index b41135d..9fbe606 100644 --- a/Documentation/http.puml +++ b/Documentation/http.puml @@ -35,7 +35,7 @@ class ViewHttpResponse extends HttpResponse { - arguments: array - kind: int - + __construct(kind: int, file: string, arguments: array, code: int = HttpCodes::OK) + - __construct(kind: int, file: string, arguments: array, code: int = HttpCodes::OK) + getViewKind(): int + getFile(): string + getArguments(): array diff --git a/public/index.php b/public/index.php index 77273f5..f95879f 100644 --- a/public/index.php +++ b/public/index.php @@ -4,6 +4,7 @@ require "../vendor/autoload.php"; require "../config.php"; require "../sql/database.php"; require "utils.php"; +require "../src/react-display.php"; use App\Connexion; @@ -19,18 +20,17 @@ use Twig\Loader\FilesystemLoader; use App\Validation\ValidationFail; use App\Controller\ErrorController; - +session_start(); $basePath = get_public_path(); +var_dump($basePath); $con = new Connexion(get_database()); -// routes initialization -$router = new AltoRouter(); -$router->setBasePath($basePath); - -$frontController = new FrontController($router); -$frontController->route(); - -//$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); -//$editorController = new EditorController(new TacticModel(new TacticInfoGateway($con))); +global $dictActionRole; +$dictActionRole = [ + "UserController" => "public", + "EditionUserController" => "editor" +]; +$frontController = new FrontController($con, $basePath, $dictActionRole); +$frontController->run(); diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php index 9a97466..337b3ca 100644 --- a/src/Controller/FrontController.php +++ b/src/Controller/FrontController.php @@ -3,13 +3,18 @@ namespace App\Controller; +use App\Connexion; use App\Controller\UserController; use AltoRouter; use App\Controller\ErrorController; +use App\Gateway\FormResultGateway; +use App\Gateway\TacticInfoGateway; use App\Http\HttpCodes; +use App\Http\HttpResponse; use App\Http\JsonHttpResponse; use App\Http\ViewHttpResponse; +use App\Model\TacticModel; use App\Validation\ValidationFail; use Twig\Loader\FilesystemLoader; @@ -17,44 +22,110 @@ use Twig\Loader\FilesystemLoader; class FrontController{ private AltoRouter $router; - private ?UserController $userController; + private Connexion $con; + private array $dictControllerRole; + public function __construct(Connexion $con, string $basePath, array $dictControllerRole) { + $this->con = $con; + $this->router = $this->createRouter($basePath); + $this->dictControllerRole = $dictControllerRole; + } + + /** + * Main behavior of the FrontController + * + * @return void + */ + public function run() : void { + $this->initializeRouterMap(); - public function __construct(AltoRouter $router){ - $this->router = $router; - $this->userController = new UserController(); + $match = $this->router->match(); + if ($this->validMatch($match)){ + $this->roleControl($match['target']); + } else { + $this->displayByViewKind(ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND)); + } + // $this->roleControl($match["target"]); + // $this->handleByResponseType($this->matchRoute()); } - public function run() { - $this->route(); + /** + * Create a new instance of an AltoRouter + * + * @param string $basePath + * @return AltoRouter + */ + public function createRouter(string $basePath) : AltoRouter { + $router = new AltoRouter(); + $router->setBasePath($basePath); + return $router; } - public function route() - { - $this->router->map("GET", "/", fn() => $this->userController->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)); + /** + * Initialize project's routes + * + * @return void + */ + private function initializeRouterMap() : void { + $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)); + } - $match = $this->router->match(); + private function validMatch($match){ + return $match != null; + } - if ($match == 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; + /** + * Initialize router's settings + * + * @return ViewHttpResponse + */ + private function roleControl(string $actionController){ + + if (isset($_SESSION['role'])){ + if($_SESSION['role'] = $this->dictControllerRole[$actionController]){ + echo "Là on redirige vers le bon controller"; + } + } + else { + $_SESSION['role'] + echo "session initialisé"; } - $this->handleByResponseType($match); + + + + + + + + // // 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); + // } + // // $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; + // // } + } - private function handleByResponseType(array $match) - { - $response = call_user_func_array($match['target'], $match['params']); + + /** + * Redirect the return of the response by the response's type + * + * @param array $match + * @return void + */ + private function handleByResponseType(HttpResponse $response) : void { + // $response = call_user_func_array($match['target'], $match['params']); http_response_code($response->getCode()); if ($response instanceof ViewHttpResponse) { @@ -66,7 +137,13 @@ class FrontController{ } } - private function displayByViewKind(ViewHttpResponse $response){ + /** + * Use the right method to display the response + * + * @param ViewHttpResponse $response + * @return void + */ + private function displayByViewKind(ViewHttpResponse $response) : void { $file = $response->getFile(); $args = $response->getArguments(); @@ -82,11 +159,9 @@ class FrontController{ } 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; + throw $e; } break; } } - - } \ No newline at end of file diff --git a/src/Controller/SampleFormController.php b/src/Controller/SampleFormController.php deleted file mode 100644 index 4241ad4..0000000 --- a/src/Controller/SampleFormController.php +++ /dev/null @@ -1,52 +0,0 @@ -gateway = $gateway; - } - - - public function displayFormReact(): HttpResponse { - return ViewHttpResponse::react("views/SampleForm.tsx", []); - } - - public function displayFormTwig(): HttpResponse { - return ViewHttpResponse::twig('sample_form.html.twig', []); - } - - private function submitForm(array $form, callable $response): HttpResponse { - return Control::runCheckedFrom($form, [ - "name" => [Validators::lenBetween(0, 32), Validators::name("Le nom ne peut contenir que des lettres, des chiffres et des accents")], - "description" => [Validators::lenBetween(0, 512)] - ], function (HttpRequest $req) use ($response) { - $description = htmlspecialchars($req["description"]); - $this->gateway->insert($req["name"], $description); - $results = ["results" => $this->gateway->listResults()]; - return call_user_func_array($response, [$results]); - }, false); - } - - public function submitFormTwig(array $form): HttpResponse { - return $this->submitForm($form, fn(array $results) => ViewHttpResponse::twig('display_results.html.twig', $results)); - } - - public function submitFormReact(array $form): HttpResponse { - return $this->submitForm($form, fn(array $results) => ViewHttpResponse::react('views/DisplayResults.tsx', $results)); - } -} \ No newline at end of file diff --git a/src/Gateway/FormResultGateway.php b/src/Gateway/FormResultGateway.php deleted file mode 100644 index fe0c601..0000000 --- a/src/Gateway/FormResultGateway.php +++ /dev/null @@ -1,33 +0,0 @@ -con = $con; - } - - - function insert(string $username, string $description) { - $this->con->exec( - "INSERT INTO FormEntries VALUES (:name, :description)", - [ - ":name" => [$username, PDO::PARAM_STR], - "description" => [$description, PDO::PARAM_STR] - ] - ); - } - - function listResults(): array { - return $this->con->fetch("SELECT * FROM FormEntries", []); - } -} \ No newline at end of file diff --git a/src/Views/home.twig b/src/Views/home.twig index 8b5ce94..b65af07 100644 --- a/src/Views/home.twig +++ b/src/Views/home.twig @@ -8,6 +8,6 @@ Document -

Test

+

Page Home à faire

\ No newline at end of file diff --git a/src/Views/sample_form.html.twig b/src/Views/sample_form.html.twig deleted file mode 100644 index bcb958e..0000000 --- a/src/Views/sample_form.html.twig +++ /dev/null @@ -1,20 +0,0 @@ - - - - - Twig view - - - -

Hello, this is a sample form made in Twig !

- -
- - - - - -
- - - \ No newline at end of file -- 2.36.3 From 5314c3bd2df2f27ce38298c7f4566e6c9244fe67 Mon Sep 17 00:00:00 2001 From: DahmaneYanis Date: Fri, 17 Nov 2023 18:49:38 +0100 Subject: [PATCH 07/10] WIP --- public/index.php | 12 +++++++----- src/Controller/FrontController.php | 25 ++++++++++--------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/public/index.php b/public/index.php index f95879f..eb580ce 100644 --- a/public/index.php +++ b/public/index.php @@ -6,7 +6,6 @@ require "../sql/database.php"; require "utils.php"; require "../src/react-display.php"; - use App\Connexion; use App\Controller\FrontController; use App\Controller\EditorController; @@ -21,16 +20,19 @@ use App\Validation\ValidationFail; use App\Controller\ErrorController; session_start(); + $basePath = get_public_path(); -var_dump($basePath); $con = new Connexion(get_database()); global $dictActionRole; $dictActionRole = [ "UserController" => "public", - "EditionUserController" => "editor" + "EditionUserController" => "public" ]; -$frontController = new FrontController($con, $basePath, $dictActionRole); -$frontController->run(); +$dict = [ + "new"; +] +$frontController = new FrontController($con, $basePath, $dictActionRole); +$frontController->run(); \ No newline at end of file diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php index 337b3ca..6d265f6 100644 --- a/src/Controller/FrontController.php +++ b/src/Controller/FrontController.php @@ -41,11 +41,12 @@ class FrontController{ $match = $this->router->match(); if ($this->validMatch($match)){ - $this->roleControl($match['target']); + var_dump($match); + $this->controlRoute($match['target']); } else { $this->displayByViewKind(ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND)); } - // $this->roleControl($match["target"]); + // $this->controlRoute($match["target"]); // $this->handleByResponseType($this->matchRoute()); } @@ -86,23 +87,15 @@ class FrontController{ * * @return ViewHttpResponse */ - private function roleControl(string $actionController){ - + private function controlRoute(string $tag){ if (isset($_SESSION['role'])){ - if($_SESSION['role'] = $this->dictControllerRole[$actionController]){ - echo "Là on redirige vers le bon controller"; + if($_SESSION['role'] = $this->dictControllerRole[$tag]){ + $controller = new $tag(); } } else { - $_SESSION['role'] - echo "session initialisé"; + $_SESSION['role'] = 'public'; // Remplacer par appel de la méthode de connexion } - - - - - - // // Page not found // if ($match == null) { @@ -117,7 +110,9 @@ class FrontController{ } - + private function controlRole($match){ + + } /** * Redirect the return of the response by the response's type * -- 2.36.3 From ff9f41f18c218177a8345efbb191a5cd5a517a95 Mon Sep 17 00:00:00 2001 From: DahmaneYanis Date: Tue, 21 Nov 2023 11:44:40 +0100 Subject: [PATCH 08/10] 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", []); - - } } -- 2.36.3 From 810477cf4cdfbf5a125cd32707b2db144169303f Mon Sep 17 00:00:00 2001 From: DahmaneYanis Date: Tue, 21 Nov 2023 12:02:04 +0100 Subject: [PATCH 09/10] Doc done --- src/Controller/FrontController.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Controller/FrontController.php b/src/Controller/FrontController.php index 98c5eaf..991d5c9 100644 --- a/src/Controller/FrontController.php +++ b/src/Controller/FrontController.php @@ -23,11 +23,9 @@ use Twig\Loader\FilesystemLoader; class FrontController{ private AltoRouter $router; - private Connexion $con; private array $dictControllerRole; public function __construct(string $basePath) { - $this->con = new Connexion(get_database());; $this->router = $this->createRouter($basePath); $this->dictControllerRole = [ "UserController" => "public", @@ -45,11 +43,11 @@ class FrontController{ $match = $this->router->match(); if ($match != null){ - $this->controlRoute($match); + $this->handleMatch($match); } else { $this->diplayViewByKind(ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND)); } - // $this->controlRoute($match["target"]); + // $this->handleMatch($match["target"]); // $this->handleByResponseType($this->matchRoute()); } @@ -80,13 +78,12 @@ class FrontController{ // $this->router->map("GET", "/tactic/[i:id]/edit", fn(int $id) => (new EditorController(new TacticModel(new TacticInfoGateway($this->con))))->openEditorFor($id)); } - /** - * Initialize router's settings + * Call * * @return ViewHttpResponse */ - private function controlRoute($match){ + private function handleMatch($match){ $tag = $match['target']; $action = $this->getAction($match); @@ -112,13 +109,25 @@ class FrontController{ } } - private function getAction($match) : string { + /** + * Get the right method to call to do an action + * + * @param array $match + * @return string + */ + private function getAction(array $match) : string { if (isset($match["params"]["action"])){ return $match["params"]["action"]; } return "home"; } + /** + * Initialize the right controller by the user's role + * + * @param string $controller + * @return void + */ private function initControllerByRole(string $controller) { $index = $controller; @@ -132,7 +141,7 @@ class FrontController{ return $controller; } } - // A décommenter quand méthode de connexion disponible + // A décommenter/remplacer quand méthode de connexion disponible // $connected = (new UserController())->login($this->dictControllerRole[$controller]); // if (!$connected){ @@ -146,7 +155,7 @@ class FrontController{ } /** - * Redirect the return of the response by the response's type + * Redirect the return by the response's type * * @param array $match * @return void -- 2.36.3 From 0a4ef194361dd3b71634006c1b1de2c6cd69e67e Mon Sep 17 00:00:00 2001 From: Override-6 Date: Tue, 21 Nov 2023 21:27:27 +0100 Subject: [PATCH 10/10] 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(); + } } -- 2.36.3