fix on routes
continuous-integration/drone/push Build is passing Details

pull/19/head^2
Vivien DUFOUR 1 year ago committed by maxime.batista
parent 038686e2ed
commit 851aa72e2c

@ -14,10 +14,12 @@ use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError; use Twig\Error\SyntaxError;
use Twig\Loader\FilesystemLoader; use Twig\Loader\FilesystemLoader;
class FrontController { class FrontController
{
private AltoRouter $router; private AltoRouter $router;
public function __construct(string $basePath) { public function __construct(string $basePath)
{
$this->router = $this->createRouter($basePath); $this->router = $this->createRouter($basePath);
$this->initializeRouterMap(); $this->initializeRouterMap();
} }
@ -27,7 +29,8 @@ class FrontController {
* *
* @return void * @return void
*/ */
public function run(): void { public function run(): void
{
$match = $this->router->match(); $match = $this->router->match();
if ($match != null) { if ($match != null) {
$this->handleMatch($match); $this->handleMatch($match);
@ -42,7 +45,8 @@ class FrontController {
* @param string $basePath * @param string $basePath
* @return AltoRouter * @return AltoRouter
*/ */
public function createRouter(string $basePath): AltoRouter { public function createRouter(string $basePath): AltoRouter
{
$router = new AltoRouter(); $router = new AltoRouter();
$router->setBasePath($basePath); $router->setBasePath($basePath);
return $router; return $router;
@ -53,17 +57,19 @@ class FrontController {
* *
* @return void * @return void
*/ */
private function initializeRouterMap(): void { private function initializeRouterMap(): void
{
$this->router->map("GET", "/", "UserController"); $this->router->map("GET", "/", "UserController");
$this->router->map("GET", "/[a:action]?", "UserController"); $this->router->map("GET|POST", "/[a:action]?/[i:id]", "UserController");
$this->router->map("GET", "/tactic/[a:action]/[i:idTactic]?", "UserController"); $this->router->map("GET|POST", "/tactic/[a:action]/[i:idTactic]?", "UserController");
} }
/** /**
* @param array<string, mixed> $match * @param array<string, mixed> $match
* @return void * @return void
*/ */
private function handleMatch(array $match): void { private function handleMatch(array $match): void
{
$tag = $match['target']; $tag = $match['target'];
$action = $this->getAction($match); $action = $this->getAction($match);
@ -78,15 +84,12 @@ class FrontController {
* @param array<int, mixed> $params * @param array<int, mixed> $params
* @return HttpResponse * @return HttpResponse
*/ */
private function tryToCall(string $controller, string $action, array $params): HttpResponse { private function tryToCall(string $controller, string $action, array $params): HttpResponse
{
$controller = $this->getController($controller); $controller = $this->getController($controller);
try { if (is_callable([$controller, $action])) {
if (is_callable([$controller, $action])) { return call_user_func_array([$controller, $action], $params);
return call_user_func_array([$controller, $action], $params); } else {
} else {
return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND);
}
} catch (Exception $e) {
return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND); return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND);
} }
} }
@ -97,7 +100,8 @@ class FrontController {
* @param array<string, mixed> $match * @param array<string, mixed> $match
* @return string * @return string
*/ */
private function getAction(array $match): string { private function getAction(array $match): string
{
if (isset($match["params"]["action"])) { if (isset($match["params"]["action"])) {
return $match["params"]["action"]; return $match["params"]["action"];
} }
@ -110,7 +114,8 @@ class FrontController {
* @param string $controller * @param string $controller
* @return mixed * @return mixed
*/ */
private function getController(string $controller) { private function getController(string $controller)
{
$namespace = "\\App\\Controller\\"; $namespace = "\\App\\Controller\\";
$controller = $namespace . $controller; $controller = $namespace . $controller;
return new $controller(); return new $controller();
@ -122,7 +127,8 @@ class FrontController {
* @param HttpResponse $response * @param HttpResponse $response
* @return void * @return void
*/ */
private function handleResponseByType(HttpResponse $response): void { private function handleResponseByType(HttpResponse $response): void
{
http_response_code($response->getCode()); http_response_code($response->getCode());
if ($response instanceof ViewHttpResponse) { if ($response instanceof ViewHttpResponse) {
$this->displayViewByKind($response); $this->displayViewByKind($response);
@ -138,7 +144,8 @@ class FrontController {
* @param ViewHttpResponse $response * @param ViewHttpResponse $response
* @return void * @return void
*/ */
private function displayViewByKind(ViewHttpResponse $response): void { private function displayViewByKind(ViewHttpResponse $response): void
{
$file = $response->getFile(); $file = $response->getFile();
$args = $response->getArguments(); $args = $response->getArguments();
@ -151,7 +158,7 @@ class FrontController {
$loader = new FilesystemLoader('../src/Views/'); $loader = new FilesystemLoader('../src/Views/');
$twig = new Environment($loader); $twig = new Environment($loader);
$twig->display($file, $args); $twig->display($file, $args);
} catch (RuntimeError|SyntaxError|LoaderError $e) { } catch (RuntimeError | SyntaxError | LoaderError $e) {
http_response_code(500); 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"); 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;

@ -14,8 +14,8 @@ use App\Model\TacticModel;
class EditorController { class EditorController {
private TacticModel $model; private TacticModel $model;
public function __construct() { public function __construct(TacticModel $model) {
$this->model = new TacticModel(new TacticInfoGateway(new Connexion(get_database()))); $this->model = $model;
} }
private function openEditor(TacticInfo $tactic): HttpResponse { private function openEditor(TacticInfo $tactic): HttpResponse {

@ -1,6 +1,6 @@
<?php <?php
namespace App\Controller; namespace App\Controller\Sub;
use App\Http\HttpRequest; use App\Http\HttpRequest;
use App\Http\HttpResponse; use App\Http\HttpResponse;

@ -5,10 +5,12 @@ namespace App\Controller;
use App\Connexion; use App\Connexion;
use App\Gateway\AuthGateway; use App\Gateway\AuthGateway;
use App\Gateway\TacticInfoGateway; use App\Gateway\TacticInfoGateway;
use App\Gateway\TeamGateway;
use App\Http\HttpResponse; use App\Http\HttpResponse;
use App\Http\ViewHttpResponse; use App\Http\ViewHttpResponse;
use App\Model\AuthModel; use App\Model\AuthModel;
use App\Model\TacticModel; use App\Model\TacticModel;
use App\Model\TeamModel;
class UserController { class UserController {
public function home(): HttpResponse { public function home(): HttpResponse {
@ -45,4 +47,28 @@ class UserController {
$model = new TacticModel(new TacticInfoGateway(new Connexion(get_database()))); $model = new TacticModel(new TacticInfoGateway(new Connexion(get_database())));
return (new Sub\EditorController($model))->createNew(); return (new Sub\EditorController($model))->createNew();
} }
public function createTeam(): HttpResponse {
$model = new TeamModel(new TeamGateway(new Connexion(get_database())));
$ctrl = new Sub\TeamController($model);
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
return $ctrl->displaySubmitTeam();
}
return $ctrl->submitTeam($_POST);
}
public function listTeams(): HttpResponse {
$model = new TeamModel(new TeamGateway(new Connexion(get_database())));
$ctrl = new Sub\TeamController($model);
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
return $ctrl->displayListTeamByName();
}
return $ctrl->listTeamByName($_POST);
}
public function getTeam(int $id): HttpResponse {
$model = new TeamModel(new TeamGateway(new Connexion(get_database())));
$ctrl = new Sub\TeamController($model);
return $ctrl->getTeam($id);
}
} }

@ -10,7 +10,7 @@
<p>Aucune équipe n'a été trouvée</p> <p>Aucune équipe n'a été trouvée</p>
<div class="container"> <div class="container">
<h2>Chercher une équipe</h2> <h2>Chercher une équipe</h2>
<form action="/team/list" method="post"> <form action="/listTea" method="post">
<div class="form-group"> <div class="form-group">
<label for="name">Nom de l'équipe :</label> <label for="name">Nom de l'équipe :</label>
<input type="text" id="name" name="name" required> <input type="text" id="name" name="name" required>
@ -22,7 +22,7 @@
</div> </div>
{% else %} {% else %}
{% for t in teams %} {% for t in teams %}
<div class="team" onclick="window.location.href = '/team/{{ t.id }}'"> <div class="team" onclick="window.location.href = '/getTeam/{{ t.id }}'">
<p>Nom de l'équipe : {{ t.name }}</p> <p>Nom de l'équipe : {{ t.name }}</p>
<img src="{{ t.picture }}" alt="logo de l'équipe"> <img src="{{ t.picture }}" alt="logo de l'équipe">
</div> </div>

@ -64,7 +64,7 @@
<div class="container"> <div class="container">
<h2>Créer une équipe</h2> <h2>Créer une équipe</h2>
<form action="/team/new" method="post"> <form action="/createTeam" method="post">
<div class="form-group"> <div class="form-group">
<label for="name">Nom de l'équipe :</label> <label for="name">Nom de l'équipe :</label>
<input type="text" id="name" name="name" required> <input type="text" id="name" name="name" required>

@ -62,7 +62,7 @@
<div class="container"> <div class="container">
<h2>Chercher une équipe</h2> <h2>Chercher une équipe</h2>
<form action="/team/list" method="post"> <form action="/listTeams" method="post">
<div class="form-group"> <div class="form-group">
<label for="name">Nom de l'équipe :</label> <label for="name">Nom de l'équipe :</label>
<input type="text" id="name" name="name" required> <input type="text" id="name" name="name" required>

Loading…
Cancel
Save