add salva-like routes and controller architecture
continuous-integration/drone/push Build is failing Details

pull/20/head
Override-6 1 year ago
parent 5d4bb84368
commit 53584a518e
Signed by untrusted user who does not match committer: maxime.batista
GPG Key ID: 8002CC4B4DD9ECA5

@ -1,26 +0,0 @@
<?php
namespace App\Controller;
require_once __DIR__ . "/../react-display.php";
use App\Validation\ValidationFail;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class ErrorController {
/**
* @param ValidationFail[] $failures
* @param Environment $twig
* @return void
*/
public static function displayFailures(array $failures, Environment $twig): void {
try {
$twig->display("error.html.twig", ['failures' => $failures]);
} catch (LoaderError|RuntimeError|SyntaxError $e) {
echo "Twig error: $e";
}
}
}

@ -56,7 +56,7 @@ class FrontController {
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", "/[a:action]?", "UserController");
$this->router->map("GET", "/tactic/[a:action]/[i:idTactic]?", "EditorController"); $this->router->map("GET", "/tactic/[a:action]/[i:idTactic]?", "UserController");
} }
/** /**

@ -1,64 +0,0 @@
<?php
namespace App\Controller;
require_once __DIR__ . "/../react-display.php";
use App\Gateway\FormResultGateway;
use App\Http\HttpRequest;
use App\Http\HttpResponse;
use App\Http\ViewHttpResponse;
use App\Validation\Validators;
class SampleFormController {
private FormResultGateway $gateway;
/**
* @param FormResultGateway $gateway
*/
public function __construct(FormResultGateway $gateway) {
$this->gateway = $gateway;
}
public function displayFormReact(): HttpResponse {
return ViewHttpResponse::react("views/SampleForm.tsx", []);
}
public function displayFormTwig(): HttpResponse {
return ViewHttpResponse::twig('sample_form.html.twig', []);
}
/**
* @param array<string, mixed> $form
* @param callable(array<array<string, string>>): ViewHttpResponse $response
* @return HttpResponse
*/
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);
}
/**
* @param array<string, mixed> $form
* @return HttpResponse
*/
public function submitFormTwig(array $form): HttpResponse {
return $this->submitForm($form, fn(array $results) => ViewHttpResponse::twig('display_results.html.twig', $results));
}
/**
* @param array<string, mixed> $form
* @return HttpResponse
*/
public function submitFormReact(array $form): HttpResponse {
return $this->submitForm($form, fn(array $results) => ViewHttpResponse::react('views/DisplayResults.tsx', $results));
}
}

@ -1,8 +1,7 @@
<?php <?php
namespace App\Controller; namespace App\Controller\Sub;
use App\Gateway\AuthGateway;
use App\Http\HttpRequest; use App\Http\HttpRequest;
use App\Http\HttpResponse; use App\Http\HttpResponse;
use App\Http\ViewHttpResponse; use App\Http\ViewHttpResponse;
@ -10,7 +9,6 @@ use App\Model\AuthModel;
use App\Validation\FieldValidationFail; use App\Validation\FieldValidationFail;
use App\Validation\ValidationFail; use App\Validation\ValidationFail;
use App\Validation\Validators; use App\Validation\Validators;
use Twig\Environment;
class AuthController { class AuthController {
private AuthModel $model; private AuthModel $model;

@ -1,6 +1,6 @@
<?php <?php
namespace App\Controller; namespace App\Controller\Sub;
use App\Connexion; use App\Connexion;
use App\Data\TacticInfo; use App\Data\TacticInfo;
@ -22,7 +22,7 @@ class EditorController {
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 create(): HttpResponse { public function createNew(): HttpResponse {
$tactic = $this->model->makeNewDefault(); $tactic = $this->model->makeNewDefault();
return $this->openEditor($tactic); return $this->openEditor($tactic);
} }

@ -1,6 +1,6 @@
<?php <?php
namespace App\Controller; namespace App\Controller\Sub;
use App\Http\HttpCodes; use App\Http\HttpCodes;
use App\Http\HttpResponse; use App\Http\HttpResponse;
@ -19,7 +19,7 @@ class VisualizerController {
$this->tacticModel = $tacticModel; $this->tacticModel = $tacticModel;
} }
public function openVisualizer(int $id): HttpResponse { public function visualize(int $id): HttpResponse {
$tactic = $this->tacticModel->get($id); $tactic = $this->tacticModel->get($id);
if ($tactic == null) { if ($tactic == null) {
@ -27,6 +27,5 @@ class VisualizerController {
} }
return ViewHttpResponse::react("views/Visualizer.tsx", ["name" => $tactic->getName()]); return ViewHttpResponse::react("views/Visualizer.tsx", ["name" => $tactic->getName()]);
} }
} }

@ -2,15 +2,47 @@
namespace App\Controller; namespace App\Controller;
use App\Connexion;
use App\Gateway\AuthGateway;
use App\Gateway\TacticInfoGateway;
use App\Http\HttpResponse; use App\Http\HttpResponse;
use App\Http\ViewHttpResponse; use App\Http\ViewHttpResponse;
use App\Model\AuthModel;
use App\Model\TacticModel;
class UserController { class UserController {
public function home(): HttpResponse { public function home(): HttpResponse {
return ViewHttpResponse::twig("home.twig", []); return ViewHttpResponse::twig("home.twig", []);
} }
public function default(): HttpResponse { public function register(): HttpResponse {
return self::home(); $model = new AuthModel(new AuthGateway(new Connexion(get_database())));
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
return (new Sub\AuthController($model))->displayRegister();
}
return (new Sub\AuthController($model))->confirmRegister($_POST);
}
public function login(): HttpResponse {
$model = new AuthModel(new AuthGateway(new Connexion(get_database())));
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
return (new Sub\AuthController($model))->displayLogin();
}
return (new Sub\AuthController($model))->confirmLogin($_POST);
}
public function open(int $id): HttpResponse {
$model = new TacticModel(new TacticInfoGateway(new Connexion(get_database())));
return (new Sub\VisualizerController($model))->visualize($id);
}
public function edit(int $id): HttpResponse {
$model = new TacticModel(new TacticInfoGateway(new Connexion(get_database())));
return (new Sub\EditorController($model))->edit($id);
}
public function create(): HttpResponse {
$model = new TacticModel(new TacticInfoGateway(new Connexion(get_database())));
return (new Sub\EditorController($model))->createNew();
} }
} }

@ -2,7 +2,6 @@
namespace App\Model; namespace App\Model;
use App\Controller\AuthController;
use App\Gateway\AuthGateway; use App\Gateway\AuthGateway;
use App\Validation\FieldValidationFail; use App\Validation\FieldValidationFail;
use App\Validation\ValidationFail; use App\Validation\ValidationFail;

Loading…
Cancel
Save