|
|
|
@ -14,12 +14,10 @@ use Twig\Error\RuntimeError;
|
|
|
|
|
use Twig\Error\SyntaxError;
|
|
|
|
|
use Twig\Loader\FilesystemLoader;
|
|
|
|
|
|
|
|
|
|
class FrontController
|
|
|
|
|
{
|
|
|
|
|
class FrontController {
|
|
|
|
|
private AltoRouter $router;
|
|
|
|
|
|
|
|
|
|
public function __construct(string $basePath)
|
|
|
|
|
{
|
|
|
|
|
public function __construct(string $basePath) {
|
|
|
|
|
$this->router = $this->createRouter($basePath);
|
|
|
|
|
$this->initializeRouterMap();
|
|
|
|
|
}
|
|
|
|
@ -29,8 +27,7 @@ class FrontController
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function run(): void
|
|
|
|
|
{
|
|
|
|
|
public function run(): void {
|
|
|
|
|
$match = $this->router->match();
|
|
|
|
|
if ($match != null) {
|
|
|
|
|
$this->handleMatch($match);
|
|
|
|
@ -45,8 +42,7 @@ class FrontController
|
|
|
|
|
* @param string $basePath
|
|
|
|
|
* @return AltoRouter
|
|
|
|
|
*/
|
|
|
|
|
public function createRouter(string $basePath): AltoRouter
|
|
|
|
|
{
|
|
|
|
|
public function createRouter(string $basePath): AltoRouter {
|
|
|
|
|
$router = new AltoRouter();
|
|
|
|
|
$router->setBasePath($basePath);
|
|
|
|
|
return $router;
|
|
|
|
@ -57,8 +53,7 @@ class FrontController
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function initializeRouterMap(): void
|
|
|
|
|
{
|
|
|
|
|
private function initializeRouterMap(): void {
|
|
|
|
|
$this->router->map("GET", "/", "UserController");
|
|
|
|
|
$this->router->map("GET|POST", "/[a:action]?/[i:id]", "UserController");
|
|
|
|
|
$this->router->map("GET|POST", "/tactic/[a:action]/[i:idTactic]?", "UserController");
|
|
|
|
@ -68,8 +63,7 @@ class FrontController
|
|
|
|
|
* @param array<string, mixed> $match
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function handleMatch(array $match): void
|
|
|
|
|
{
|
|
|
|
|
private function handleMatch(array $match): void {
|
|
|
|
|
$tag = $match['target'];
|
|
|
|
|
|
|
|
|
|
$action = $this->getAction($match);
|
|
|
|
@ -84,8 +78,7 @@ class FrontController
|
|
|
|
|
* @param array<int, mixed> $params
|
|
|
|
|
* @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);
|
|
|
|
|
if (is_callable([$controller, $action])) {
|
|
|
|
|
return call_user_func_array([$controller, $action], $params);
|
|
|
|
@ -100,8 +93,7 @@ class FrontController
|
|
|
|
|
* @param array<string, mixed> $match
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function getAction(array $match): string
|
|
|
|
|
{
|
|
|
|
|
private function getAction(array $match): string {
|
|
|
|
|
if (isset($match["params"]["action"])) {
|
|
|
|
|
return $match["params"]["action"];
|
|
|
|
|
}
|
|
|
|
@ -114,8 +106,7 @@ class FrontController
|
|
|
|
|
* @param string $controller
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
private function getController(string $controller)
|
|
|
|
|
{
|
|
|
|
|
private function getController(string $controller) {
|
|
|
|
|
$namespace = "\\App\\Controller\\";
|
|
|
|
|
$controller = $namespace . $controller;
|
|
|
|
|
return new $controller();
|
|
|
|
@ -127,8 +118,7 @@ class FrontController
|
|
|
|
|
* @param HttpResponse $response
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function handleResponseByType(HttpResponse $response): void
|
|
|
|
|
{
|
|
|
|
|
private function handleResponseByType(HttpResponse $response): void {
|
|
|
|
|
http_response_code($response->getCode());
|
|
|
|
|
if ($response instanceof ViewHttpResponse) {
|
|
|
|
|
$this->displayViewByKind($response);
|
|
|
|
@ -144,8 +134,7 @@ class FrontController
|
|
|
|
|
* @param ViewHttpResponse $response
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function displayViewByKind(ViewHttpResponse $response): void
|
|
|
|
|
{
|
|
|
|
|
private function displayViewByKind(ViewHttpResponse $response): void {
|
|
|
|
|
$file = $response->getFile();
|
|
|
|
|
$args = $response->getArguments();
|
|
|
|
|
|
|
|
|
|