FrontController done
continuous-integration/drone/push Build is passing Details

pull/17/head
DahmaneYanis 1 year ago
parent 5314c3bd2d
commit ff9f41f18c

@ -8,31 +8,12 @@ require "../src/react-display.php";
use App\Connexion; use App\Connexion;
use App\Controller\FrontController; 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(); session_start();
$basePath = get_public_path(); $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(); $frontController->run();

@ -2,7 +2,9 @@
namespace App\Controller; namespace App\Controller;
use App\Connexion;
use App\Data\TacticInfo; use App\Data\TacticInfo;
use App\Gateway\TacticInfoGateway;
use App\Http\HttpCodes; use App\Http\HttpCodes;
use App\Http\HttpRequest; use App\Http\HttpRequest;
use App\Http\HttpResponse; use App\Http\HttpResponse;
@ -17,15 +19,15 @@ class EditorController {
/** /**
* @param TacticModel $model * @param TacticModel $model
*/ */
public function __construct(TacticModel $model) { public function __construct() {
$this->model = $model; $this->model = new TacticModel(new TacticInfoGateway(new Connexion(get_database())));
} }
private function openEditor(TacticInfo $tactic): HttpResponse { private function openEditor(TacticInfo $tactic): HttpResponse {
return ViewHttpResponse::react("views/Editor.tsx", ["name" => $tactic->getName(), "id" => $tactic->getId()]); return ViewHttpResponse::react("views/Editor.tsx", ["name" => $tactic->getName(), "id" => $tactic->getId()]);
} }
public function makeNew(): HttpResponse { public function create(): HttpResponse {
$tactic = $this->model->makeNewDefault(); $tactic = $this->model->makeNewDefault();
return $this->openEditor($tactic); return $this->openEditor($tactic);
} }
@ -35,7 +37,7 @@ class EditorController {
* @param int $id the targeted tactic identifier * @param int $id the targeted tactic identifier
* @return HttpResponse * @return HttpResponse
*/ */
public function openEditorFor(int $id): HttpResponse { public function edit(int $id): HttpResponse {
$tactic = $this->model->get($id); $tactic = $this->model->get($id);
if ($tactic == null) { if ($tactic == null) {

@ -2,7 +2,7 @@
namespace App\Controller; namespace App\Controller;
use App\Controller;
use App\Connexion; use App\Connexion;
use App\Controller\UserController; use App\Controller\UserController;
@ -16,6 +16,7 @@ use App\Http\JsonHttpResponse;
use App\Http\ViewHttpResponse; use App\Http\ViewHttpResponse;
use App\Model\TacticModel; use App\Model\TacticModel;
use App\Validation\ValidationFail; use App\Validation\ValidationFail;
use Exception;
use Twig\Loader\FilesystemLoader; use Twig\Loader\FilesystemLoader;
@ -25,10 +26,13 @@ class FrontController{
private Connexion $con; private Connexion $con;
private array $dictControllerRole; private array $dictControllerRole;
public function __construct(Connexion $con, string $basePath, array $dictControllerRole) { public function __construct(string $basePath) {
$this->con = $con; $this->con = new Connexion(get_database());;
$this->router = $this->createRouter($basePath); $this->router = $this->createRouter($basePath);
$this->dictControllerRole = $dictControllerRole; $this->dictControllerRole = [
"UserController" => "public",
"EditorController" => "public"
];
} }
/** /**
@ -40,11 +44,10 @@ class FrontController{
$this->initializeRouterMap(); $this->initializeRouterMap();
$match = $this->router->match(); $match = $this->router->match();
if ($this->validMatch($match)){ if ($match != null){
var_dump($match); $this->controlRoute($match);
$this->controlRoute($match['target']);
} else { } 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->controlRoute($match["target"]);
// $this->handleByResponseType($this->matchRoute()); // $this->handleByResponseType($this->matchRoute());
@ -78,53 +81,82 @@ class FrontController{
} }
private function validMatch($match){
return $match != null;
}
/** /**
* Initialize router's settings * Initialize router's settings
* *
* @return ViewHttpResponse * @return ViewHttpResponse
*/ */
private function controlRoute(string $tag){ private function controlRoute($match){
if (isset($_SESSION['role'])){ $tag = $match['target'];
if($_SESSION['role'] = $this->dictControllerRole[$tag]){
$controller = new $tag(); $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);
}
}
catch (Exception $e) {
return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND);
} }
else {
$_SESSION['role'] = 'public'; // Remplacer par appel de la méthode de connexion
} }
// // Page not found private function getAction($match) : string {
// if ($match == null) { if (isset($match["params"]["action"])){
// return new ViewHttpResponse(ViewHttpResponse::TWIG_VIEW, "Views/error.html.twig", [ValidationFail::notFound("Cette page n'existe pas")], HttpCodes::NOT_FOUND); return $match["params"]["action"];
// } }
// // $loader = new FilesystemLoader('../src/Views/'); return "home";
// // $twig = new \Twig\Environment($loader); }
// // http_response_code(HttpCodes::NOT_FOUND);
// // ErrorController::displayFailures([ValidationFail::notFound("Cette page n'existe pas")], $twig); private function initControllerByRole(string $controller) {
// // return;
// // } $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
// $connected = (new UserController())->login($this->dictControllerRole[$controller]);
// if (!$connected){
// return "null";
// }
private function controlRole($match){ $_SESSION['role'] = 'public'; // Remplacer par appel de la méthode de connexion
$controller = new $controller();
return $controller;
} }
/** /**
* Redirect the return of the response by the response's type * Redirect the return of the response by the response's type
* *
* @param array $match * @param array $match
* @return void * @return void
*/ */
private function handleByResponseType(HttpResponse $response) : void { private function handleResponseByType(HttpResponse $response) : void {
// $response = call_user_func_array($match['target'], $match['params']); // $response = call_user_func_array($match['target'], $match['params']);
http_response_code($response->getCode()); http_response_code($response->getCode());
if ($response instanceof ViewHttpResponse) { if ($response instanceof ViewHttpResponse) {
$this->displayByViewKind($response); $this->diplayViewByKind($response);
} else if ($response instanceof JsonHttpResponse) { } else if ($response instanceof JsonHttpResponse) {
header('Content-type: application/json'); header('Content-type: application/json');
@ -138,7 +170,7 @@ class FrontController{
* @param ViewHttpResponse $response * @param ViewHttpResponse $response
* @return void * @return void
*/ */
private function displayByViewKind(ViewHttpResponse $response) : void { private function diplayViewByKind(ViewHttpResponse $response) : void {
$file = $response->getFile(); $file = $response->getFile();
$args = $response->getArguments(); $args = $response->getArguments();

@ -9,7 +9,5 @@ class UserController {
public function home() : HttpResponse { public function home() : HttpResponse {
return ViewHttpResponse::twig("home.twig", []); return ViewHttpResponse::twig("home.twig", []);
} }
} }

Loading…
Cancel
Save