|
|
@ -7,6 +7,7 @@ use App\Http\HttpCodes;
|
|
|
|
use App\Http\HttpResponse;
|
|
|
|
use App\Http\HttpResponse;
|
|
|
|
use App\Http\JsonHttpResponse;
|
|
|
|
use App\Http\JsonHttpResponse;
|
|
|
|
use App\Http\ViewHttpResponse;
|
|
|
|
use App\Http\ViewHttpResponse;
|
|
|
|
|
|
|
|
use App\Session\MutableSessionHandle;
|
|
|
|
use Exception;
|
|
|
|
use Exception;
|
|
|
|
use Twig\Environment;
|
|
|
|
use Twig\Environment;
|
|
|
|
use Twig\Error\LoaderError;
|
|
|
|
use Twig\Error\LoaderError;
|
|
|
@ -14,12 +15,10 @@ 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -29,11 +28,10 @@ class FrontController
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function run(): void
|
|
|
|
public function run(MutableSessionHandle $session): void {
|
|
|
|
{
|
|
|
|
|
|
|
|
$match = $this->router->match();
|
|
|
|
$match = $this->router->match();
|
|
|
|
if ($match != null) {
|
|
|
|
if ($match != null) {
|
|
|
|
$this->handleMatch($match);
|
|
|
|
$this->handleMatch($match, $session);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
$this->displayViewByKind(ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND));
|
|
|
|
$this->displayViewByKind(ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -45,8 +43,7 @@ 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;
|
|
|
@ -57,8 +54,7 @@ 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|POST", "/[a:action]?", "UserController");
|
|
|
|
$this->router->map("GET|POST", "/[a:action]?", "UserController");
|
|
|
|
$this->router->map("GET|POST", "/tactic/[a:action]/[i:idTactic]?", "UserController");
|
|
|
|
$this->router->map("GET|POST", "/tactic/[a:action]/[i:idTactic]?", "UserController");
|
|
|
@ -68,30 +64,36 @@ class FrontController
|
|
|
|
* @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, MutableSessionHandle $session): void {
|
|
|
|
{
|
|
|
|
|
|
|
|
$tag = $match['target'];
|
|
|
|
$tag = $match['target'];
|
|
|
|
|
|
|
|
|
|
|
|
$action = $this->getAction($match);
|
|
|
|
$action = $this->getAction($match);
|
|
|
|
$params = $match["params"];
|
|
|
|
$params = $match["params"];
|
|
|
|
unset($params['action']);
|
|
|
|
unset($params['action']);
|
|
|
|
$this->handleResponseByType($this->tryToCall($tag, $action, array_values($params)));
|
|
|
|
$this->handleResponseByType($this->tryToCall($tag, $action, array_values($params), $session));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @param string $controller
|
|
|
|
* @param string $controller
|
|
|
|
* @param string $action
|
|
|
|
* @param string $action
|
|
|
|
* @param array<int, mixed> $params
|
|
|
|
* @param array<int, mixed> $params
|
|
|
|
|
|
|
|
* @param MutableSessionHandle $session
|
|
|
|
* @return HttpResponse
|
|
|
|
* @return HttpResponse
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private function tryToCall(string $controller, string $action, array $params): HttpResponse
|
|
|
|
|
|
|
|
{
|
|
|
|
private function tryToCall(string $controller, string $action, array $params, MutableSessionHandle $session): HttpResponse {
|
|
|
|
$controller = $this->getController($controller);
|
|
|
|
$controller = $this->getController($controller);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (is_callable([$controller, $action])) {
|
|
|
|
if (is_callable([$controller, $action])) {
|
|
|
|
|
|
|
|
// append the session as the last parameter of a controller function
|
|
|
|
|
|
|
|
$params[] = $session;
|
|
|
|
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);
|
|
|
|
return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
|
|
|
return ViewHttpResponse::twig("error.html.twig", [], HttpCodes::NOT_FOUND);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -100,8 +102,7 @@ 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"];
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -114,8 +115,7 @@ 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();
|
|
|
@ -127,8 +127,7 @@ 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);
|
|
|
@ -144,8 +143,7 @@ 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();
|
|
|
|
|
|
|
|
|
|
|
|