From 1147ad114f78a23b73f062acd26d6a59deebb214 Mon Sep 17 00:00:00 2001 From: Override-6 Date: Sun, 26 Nov 2023 14:23:50 +0100 Subject: [PATCH 1/2] fix routes in views by adding global variable basePath in twig --- public/api/index.php | 2 +- public/index.php | 16 ++- src/Api/API.php | 2 - src/Api/Controller/APIAuthController.php | 2 +- src/Api/Controller/APITacticController.php | 2 +- src/App/App.php | 15 +-- src/App/Control.php | 4 +- src/App/Views/account_settings.twig | 35 +++-- src/App/Views/add_member.html.twig | 8 +- src/App/Views/delete_member.html.twig | 2 +- src/App/Views/display_login.html.twig | 19 ++- src/App/Views/display_register.html.twig | 20 +-- src/App/Views/display_team.html.twig | 144 +++++++++++---------- src/App/Views/display_teams.html.twig | 4 +- src/App/Views/error.html.twig | 92 ++++++------- src/App/Views/home.twig | 100 +++++++------- src/App/Views/insert_team.html.twig | 11 +- src/App/Views/list_team_by_name.html.twig | 7 +- src/index-utils.php | 4 +- 19 files changed, 253 insertions(+), 236 deletions(-) diff --git a/public/api/index.php b/public/api/index.php index 6f46638..fd39bfa 100644 --- a/public/api/index.php +++ b/public/api/index.php @@ -27,7 +27,7 @@ function getAuthController(): APIAuthController { function getRoutes(): AltoRouter { $router = new AltoRouter(); - $router->setBasePath(get_public_path() . "/api"); + $router->setBasePath(get_public_path(__DIR__)); $router->map("POST", "/tactic/[i:id]/edit/name", Action::auth(fn(int $id, Account $acc) => getTacticController()->updateName($id, $acc))); $router->map("POST", "/auth", Action::noAuth(fn() => getAuthController()->authorize())); diff --git a/public/index.php b/public/index.php index 01ba967..8b4acf7 100644 --- a/public/index.php +++ b/public/index.php @@ -29,6 +29,8 @@ use IQBall\Core\Model\AuthModel; use IQBall\Core\Model\TacticModel; use IQBall\Core\Model\TeamModel; use IQBall\Core\Validation\ValidationFail; +use Twig\Environment; +use Twig\Loader\FilesystemLoader; function getConnection(): Connection { return new Connection(get_database()); @@ -55,6 +57,16 @@ function getAuthController(): AuthController { return new AuthController(new AuthModel(new AccountGateway(getConnection()))); } +function getTwig(): Environment { + global $basePath; + $fl = new FilesystemLoader("../src/App/Views"); + $twig = new Environment($fl); + + $twig->addGlobal("basePath", $basePath); + + return $twig; +} + function getRoutes(): AltoRouter { global $basePath; @@ -104,6 +116,6 @@ function runMatch($match, MutableSessionHandle $session): HttpResponse { //this is a global variable -$basePath = get_public_path(); +$basePath = get_public_path(__DIR__); -App::render(runMatch(getRoutes()->match(), PhpSessionHandle::init()), "../src/App/Views/"); +App::render(runMatch(getRoutes()->match(), PhpSessionHandle::init()), fn() => getTwig()); diff --git a/src/Api/API.php b/src/Api/API.php index e96b6b6..03daf91 100644 --- a/src/Api/API.php +++ b/src/Api/API.php @@ -19,8 +19,6 @@ class API { if ($response instanceof JsonHttpResponse) { header('Content-type: application/json'); echo $response->getJson(); - } else { - throw new Exception("API returned a non-json response."); } } diff --git a/src/Api/Controller/APIAuthController.php b/src/Api/Controller/APIAuthController.php index 5e149a5..fc0eef6 100644 --- a/src/Api/Controller/APIAuthController.php +++ b/src/Api/Controller/APIAuthController.php @@ -2,8 +2,8 @@ namespace IQBall\Api\Controller; +use IQBall\App\Control; use IQBall\Core\Http\HttpCodes; -use IQBall\Core\Route\Control; use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\JsonHttpResponse; diff --git a/src/Api/Controller/APITacticController.php b/src/Api/Controller/APITacticController.php index 04f0a50..e8a1731 100644 --- a/src/Api/Controller/APITacticController.php +++ b/src/Api/Controller/APITacticController.php @@ -2,7 +2,7 @@ namespace IQBall\Api\Controller; -use IQBall\Core\Route\Control; +use IQBall\App\Control; use IQBall\Core\Data\Account; use IQBall\Core\Http\HttpCodes; use IQBall\Core\Http\HttpRequest; diff --git a/src/App/App.php b/src/App/App.php index de288e8..cd3c293 100644 --- a/src/App/App.php +++ b/src/App/App.php @@ -16,13 +16,13 @@ class App { /** * renders (prints out) given HttpResponse to the client * @param HttpResponse $response - * @param string $twigViewsFolder + * @param callable(): Environment $twigSupplier * @return void * @throws LoaderError * @throws RuntimeError * @throws SyntaxError */ - public static function render(HttpResponse $response, string $twigViewsFolder): void { + public static function render(HttpResponse $response, callable $twigSupplier): void { http_response_code($response->getCode()); foreach ($response->getHeaders() as $header => $value) { @@ -30,7 +30,7 @@ class App { } if ($response instanceof ViewHttpResponse) { - self::renderView($response, $twigViewsFolder); + self::renderView($response, $twigSupplier); } elseif ($response instanceof JsonHttpResponse) { header('Content-type: application/json'); echo $response->getJson(); @@ -40,13 +40,13 @@ class App { /** * renders (prints out) given ViewHttpResponse to the client * @param ViewHttpResponse $response - * @param string $twigViewsFolder + * @param callable(): Environment $twigSupplier * @return void * @throws LoaderError * @throws RuntimeError * @throws SyntaxError */ - private static function renderView(ViewHttpResponse $response, string $twigViewsFolder): void { + private static function renderView(ViewHttpResponse $response, callable $twigSupplier): void { $file = $response->getFile(); $args = $response->getArguments(); @@ -56,10 +56,9 @@ class App { break; case ViewHttpResponse::TWIG_VIEW: try { - $fl = new FilesystemLoader($twigViewsFolder); - $twig = new Environment($fl); + $twig = call_user_func($twigSupplier); $twig->display($file, $args); - } catch (RuntimeError | SyntaxError | LoaderError $e) { + } catch (RuntimeError|SyntaxError|LoaderError $e) { 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"); throw $e; diff --git a/src/App/Control.php b/src/App/Control.php index f3860ec..5c2fe0f 100644 --- a/src/App/Control.php +++ b/src/App/Control.php @@ -1,12 +1,10 @@ - - - - - Paramètres + + + + + Paramètres - - + + - - - -

Paramètres

- + + +

Paramètres

+ \ No newline at end of file diff --git a/src/App/Views/add_member.html.twig b/src/App/Views/add_member.html.twig index 6c5a3e3..e083a63 100644 --- a/src/App/Views/add_member.html.twig +++ b/src/App/Views/add_member.html.twig @@ -59,11 +59,11 @@ background-color: #0056b3; } - .role{ + .role { margin-top: 10px; } - .radio{ + .radio { display: flex; justify-content: space-between; } @@ -73,7 +73,7 @@

Ajouter un membre à votre équipe

-
+
@@ -81,7 +81,7 @@
- Rôle du membre dans l'équipe : + Rôle du membre dans l'équipe :
diff --git a/src/App/Views/delete_member.html.twig b/src/App/Views/delete_member.html.twig index b7d0d3b..5b161e3 100644 --- a/src/App/Views/delete_member.html.twig +++ b/src/App/Views/delete_member.html.twig @@ -56,7 +56,7 @@

Supprimez un membre de votre équipe

- +
diff --git a/src/App/Views/display_login.html.twig b/src/App/Views/display_login.html.twig index ca6890d..48842c3 100644 --- a/src/App/Views/display_login.html.twig +++ b/src/App/Views/display_login.html.twig @@ -53,30 +53,35 @@ background-color: #0056b3; } - .error-messages{ - color : #ff331a; + .error-messages { + color: #ff331a; font-style: italic; } {% for err in fails %} - .form-group #{{ err.getFieldName() }} { - border-color: red; + .form-group + + # + {{ err.getFieldName() }} + { + border-color: red + ; } {% endfor %}

Se connecter

- +
{% for name in fails %} - + {% endfor %} - +
diff --git a/src/App/Views/display_register.html.twig b/src/App/Views/display_register.html.twig index 2b24e23..d83ec9e 100644 --- a/src/App/Views/display_register.html.twig +++ b/src/App/Views/display_register.html.twig @@ -49,8 +49,8 @@ cursor: pointer; } - .error-messages{ - color : #ff331a; + .error-messages { + color: #ff331a; font-style: italic; } @@ -59,27 +59,31 @@ } {% for err in fails %} - .form-group #{{ err.getFieldName() }} { - border-color: red; + .form-group + + # + {{ err.getFieldName() }} + { + border-color: red + ; } {% endfor %} -

S'enregistrer

- +
{% for name in fails %} - + {% endfor %} - + diff --git a/src/App/Views/display_team.html.twig b/src/App/Views/display_team.html.twig index 6f0aec1..06d64fb 100644 --- a/src/App/Views/display_team.html.twig +++ b/src/App/Views/display_team.html.twig @@ -1,85 +1,91 @@ - - - Twig view - - - -
-

IQBall

-
+ .color { + flex-direction: row; + justify-content: space-between; + } -
+ .logo { + height: 80px; + width: 80px; + } -
-
-

{{ team.getInfo().getName() }}

- -
-
-

Couleur principale :

-

Couleur secondaire :

-
+ + + +
+

IQBall

+
- {% for m in team.listMembers() %} -

{{ m.getUserId() }}

- {% if m.getRole().isCoach() %} -

: Coach

- {% else %} -

: Joueur

- {% endif %} - {% endfor %} +
+ +
+
+

{{ team.getInfo().getName() }}

+ +
+
+

Couleur principale :

+
+
+

Couleur secondaire :

+
+
+ + {% for m in team.listMembers() %} +

{{ m.getUserId() }}

+ {% if m.getRole().isCoach() %} +

: Coach

+ {% else %} +

: Joueur

+ {% endif %} + {% endfor %} +
-
- +
+ \ No newline at end of file diff --git a/src/App/Views/display_teams.html.twig b/src/App/Views/display_teams.html.twig index e4da64e..be9353d 100644 --- a/src/App/Views/display_teams.html.twig +++ b/src/App/Views/display_teams.html.twig @@ -10,7 +10,7 @@

Aucune équipe n'a été trouvée

Chercher une équipe

- +
@@ -22,7 +22,7 @@
{% else %} {% for t in teams %} -
+

Nom de l'équipe : {{ t.name }}

logo de l'équipe
diff --git a/src/App/Views/error.html.twig b/src/App/Views/error.html.twig index e30c2fa..7be397a 100644 --- a/src/App/Views/error.html.twig +++ b/src/App/Views/error.html.twig @@ -1,57 +1,57 @@ - - - Error - - + + + Error + +

IQBall

- {% for fail in failures %} -

{{ fail.getKind() }} : {{ fail.getMessage() }}

- {% endfor %} +{% for fail in failures %} +

{{ fail.getKind() }} : {{ fail.getMessage() }}

+{% endfor %} - + \ No newline at end of file diff --git a/src/App/Views/home.twig b/src/App/Views/home.twig index acf6f6b..5515ac9 100644 --- a/src/App/Views/home.twig +++ b/src/App/Views/home.twig @@ -14,81 +14,83 @@ } #bandeau { - display : flex; - flex-direction : row; + display: flex; + flex-direction: row; } - #bandeau > h1 { - self-align : center; - padding : 0%; - margin : 0%; - justify-content : center; + #bandeau > h1 { + self-align: center; + padding: 0%; + margin: 0%; + justify-content: center; } - + #account { - display : flex; - flex-direction : column; - align-content : center; + display: flex; + flex-direction: column; + align-content: center; } #account:hover { - background-color : gray; + background-color: gray; } #account img { - width : 70%; - height : auto; - align-self : center; - padding : 5%; - margin : 0%; + width: 70%; + height: auto; + align-self: center; + padding: 5%; + margin: 0%; } #account p { - align-self : center; + align-self: center; } -
-

IQ Ball

-
- +

IQ Ball

+
+ Account logo -

Mon profil

-

+ /> +

Mon profil +

+
-

Mes équipes

+

Mes équipes

- + - {% if recentTeam != null %} - {% for team in recentTeam %} -
-

{{team.name}}

-
- {% endfor %} - {% else %} -

Aucune équipe créé !

- {% endif %} +{% if recentTeam != null %} + {% for team in recentTeam %} +
+

{{ team.name }}

+
+ {% endfor %} +{% else %} +

Aucune équipe créé !

+{% endif %} -

Mes strategies

+

Mes strategies

- + - {% if recentTactic != null %} - {% for tactic in recentTactic %} -
-

{{tactic.id}} - {{tactic.name}} - {{tactic.creation_date}}

- -
- {% endfor %} - {% else %} -

Aucune tactique créé !

- {% endif %} +{% if recentTactic != null %} + {% for tactic in recentTactic %} +
+

{{ tactic.id }} - {{ tactic.name }} - {{ tactic.creation_date }}

+ +
+ {% endfor %} +{% else %} +

Aucune tactique créé !

+{% endif %} \ No newline at end of file diff --git a/src/App/Views/insert_team.html.twig b/src/App/Views/insert_team.html.twig index 0eafb55..8174fe6 100644 --- a/src/App/Views/insert_team.html.twig +++ b/src/App/Views/insert_team.html.twig @@ -34,10 +34,7 @@ {% for item in bad_fields %} #{{ item }}{ border-color: red; - } - {% endfor %} - - input[type="text"], input[type="password"] { + }{% endfor %} input[type="text"], input[type="password"] { width: 100%; padding: 10px; border: 1px solid #ccc; @@ -64,12 +61,12 @@

Créer une équipe

- +
- - + + diff --git a/src/App/Views/list_team_by_name.html.twig b/src/App/Views/list_team_by_name.html.twig index 09768e6..858f4c6 100644 --- a/src/App/Views/list_team_by_name.html.twig +++ b/src/App/Views/list_team_by_name.html.twig @@ -34,10 +34,7 @@ {% for item in bad_fields %} #{{ item }}{ border-color: red; - } - {% endfor %} - - input[type="text"], input[type="password"] { + }{% endfor %} input[type="text"], input[type="password"] { width: 100%; padding: 10px; border: 1px solid #ccc; @@ -62,7 +59,7 @@

Chercher une équipe

- +
diff --git a/src/index-utils.php b/src/index-utils.php index 7151386..eb600bc 100644 --- a/src/index-utils.php +++ b/src/index-utils.php @@ -3,9 +3,9 @@ /** * relative path of the public directory from the server's document root. */ -function get_public_path(): string { +function get_public_path(string $public_dir): string { // find the server path of the index.php file - $basePath = substr(__DIR__, strlen($_SERVER['DOCUMENT_ROOT'])); + $basePath = substr($public_dir, strlen($_SERVER['DOCUMENT_ROOT'])); $basePathLen = strlen($basePath); if ($basePathLen == 0) { -- 2.36.3 From fffb520dbf1c9454ba22ba4ef45aec782d15c544 Mon Sep 17 00:00:00 2001 From: Override-6 Date: Sun, 26 Nov 2023 14:49:01 +0100 Subject: [PATCH 2/2] fix twig assets --- ci/build_react.msh | 3 +-- public/assets | 1 + public/index.php | 3 ++- src/Api/API.php | 2 ++ src/App/Views/account_settings.twig | 2 +- src/App/Views/add_member.html.twig | 2 +- src/App/Views/delete_member.html.twig | 2 +- src/App/Views/display_login.html.twig | 2 +- src/App/Views/display_register.html.twig | 2 +- src/App/Views/display_team.html.twig | 2 +- src/App/Views/display_teams.html.twig | 4 ++-- src/App/Views/error.html.twig | 2 +- src/App/Views/home.twig | 12 ++++++------ src/App/Views/insert_team.html.twig | 2 +- src/App/Views/list_team_by_name.html.twig | 2 +- 15 files changed, 23 insertions(+), 20 deletions(-) create mode 120000 public/assets diff --git a/ci/build_react.msh b/ci/build_react.msh index 64a0cb6..32a5923 100755 --- a/ci/build_react.msh +++ b/ci/build_react.msh @@ -30,6 +30,5 @@ echo "];" >> views-mappings.php chmod +r views-mappings.php -// moshell does not supports file patterns -bash <<< "mv dist/* public/* front/assets/ front/style/ /outputs/public/" +mv dist/* front/assets/ front/style/ public/* /outputs/public/ mv views-mappings.php /outputs/ diff --git a/public/assets b/public/assets new file mode 120000 index 0000000..7b299d9 --- /dev/null +++ b/public/assets @@ -0,0 +1 @@ +../front/assets \ No newline at end of file diff --git a/public/index.php b/public/index.php index 8b4acf7..c31e289 100644 --- a/public/index.php +++ b/public/index.php @@ -31,6 +31,7 @@ use IQBall\Core\Model\TeamModel; use IQBall\Core\Validation\ValidationFail; use Twig\Environment; use Twig\Loader\FilesystemLoader; +use Twig\TwigFunction; function getConnection(): Connection { return new Connection(get_database()); @@ -62,7 +63,7 @@ function getTwig(): Environment { $fl = new FilesystemLoader("../src/App/Views"); $twig = new Environment($fl); - $twig->addGlobal("basePath", $basePath); + $twig->addFunction(new TwigFunction('path', fn(string $str) => "$basePath$str")); return $twig; } diff --git a/src/Api/API.php b/src/Api/API.php index 03daf91..f79e1b5 100644 --- a/src/Api/API.php +++ b/src/Api/API.php @@ -19,6 +19,8 @@ class API { if ($response instanceof JsonHttpResponse) { header('Content-type: application/json'); echo $response->getJson(); + } else if (get_class($response) != HttpResponse::class) { + throw new Exception("API returned unknown Http Response"); } } diff --git a/src/App/Views/account_settings.twig b/src/App/Views/account_settings.twig index 2bdd853..04d7437 100644 --- a/src/App/Views/account_settings.twig +++ b/src/App/Views/account_settings.twig @@ -17,7 +17,7 @@ - +

Paramètres

\ No newline at end of file diff --git a/src/App/Views/add_member.html.twig b/src/App/Views/add_member.html.twig index e083a63..c6bae0e 100644 --- a/src/App/Views/add_member.html.twig +++ b/src/App/Views/add_member.html.twig @@ -73,7 +73,7 @@

Ajouter un membre à votre équipe

- +
diff --git a/src/App/Views/delete_member.html.twig b/src/App/Views/delete_member.html.twig index 5b161e3..3fa5ccd 100644 --- a/src/App/Views/delete_member.html.twig +++ b/src/App/Views/delete_member.html.twig @@ -56,7 +56,7 @@

Supprimez un membre de votre équipe

- +
diff --git a/src/App/Views/display_login.html.twig b/src/App/Views/display_login.html.twig index 48842c3..cdc11a5 100644 --- a/src/App/Views/display_login.html.twig +++ b/src/App/Views/display_login.html.twig @@ -72,7 +72,7 @@

Se connecter

- +
{% for name in fails %} diff --git a/src/App/Views/display_register.html.twig b/src/App/Views/display_register.html.twig index d83ec9e..8649de8 100644 --- a/src/App/Views/display_register.html.twig +++ b/src/App/Views/display_register.html.twig @@ -74,7 +74,7 @@

S'enregistrer

- +
{% for name in fails %} diff --git a/src/App/Views/display_team.html.twig b/src/App/Views/display_team.html.twig index 06d64fb..7f23b8b 100644 --- a/src/App/Views/display_team.html.twig +++ b/src/App/Views/display_team.html.twig @@ -57,7 +57,7 @@
-

IQBall

+

IQBall

diff --git a/src/App/Views/display_teams.html.twig b/src/App/Views/display_teams.html.twig index be9353d..1e1420a 100644 --- a/src/App/Views/display_teams.html.twig +++ b/src/App/Views/display_teams.html.twig @@ -10,7 +10,7 @@

Aucune équipe n'a été trouvée

Chercher une équipe

- +
@@ -22,7 +22,7 @@
{% else %} {% for t in teams %} -
+

Nom de l'équipe : {{ t.name }}

logo de l'équipe
diff --git a/src/App/Views/error.html.twig b/src/App/Views/error.html.twig index 7be397a..bf90319 100644 --- a/src/App/Views/error.html.twig +++ b/src/App/Views/error.html.twig @@ -51,7 +51,7 @@ {% endfor %} - + \ No newline at end of file diff --git a/src/App/Views/home.twig b/src/App/Views/home.twig index 5515ac9..5d9e8ae 100644 --- a/src/App/Views/home.twig +++ b/src/App/Views/home.twig @@ -52,9 +52,9 @@

IQ Ball

-
+
Account logo

Mon profil @@ -64,7 +64,7 @@

Mes équipes

- + {% if recentTeam != null %} {% for team in recentTeam %} @@ -78,13 +78,13 @@

Mes strategies

- + {% if recentTactic != null %} {% for tactic in recentTactic %} -
+

{{ tactic.id }} - {{ tactic.name }} - {{ tactic.creation_date }}

-
{% endfor %} diff --git a/src/App/Views/insert_team.html.twig b/src/App/Views/insert_team.html.twig index 8174fe6..65cd096 100644 --- a/src/App/Views/insert_team.html.twig +++ b/src/App/Views/insert_team.html.twig @@ -61,7 +61,7 @@

Créer une équipe

- +
diff --git a/src/App/Views/list_team_by_name.html.twig b/src/App/Views/list_team_by_name.html.twig index 858f4c6..eca5e19 100644 --- a/src/App/Views/list_team_by_name.html.twig +++ b/src/App/Views/list_team_by_name.html.twig @@ -59,7 +59,7 @@

Chercher une équipe

- +
-- 2.36.3