|
|
@ -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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
catch (Exception $e) {
|
|
|
|
$_SESSION['role'] = 'public'; // Remplacer par appel de la méthode de connexion
|
|
|
|
return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// // 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);
|
|
|
|
|
|
|
|
// // return;
|
|
|
|
|
|
|
|
// // }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function controlRole($match){
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// $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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
|
|
|
|
|
|
|
|