fix routes in views by adding global variable basePath in twig
continuous-integration/drone/push Build is passing Details

pull/24/head
Override-6 1 year ago
parent 1bf3dfa3b6
commit 1147ad114f
Signed by untrusted user who does not match committer: maxime.batista
GPG Key ID: 8002CC4B4DD9ECA5

@ -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()));

@ -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());

@ -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.");
}
}

@ -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;

@ -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;

@ -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;

@ -1,12 +1,10 @@
<?php
namespace IQBall\Core\Route;
namespace IQBall\App;
use IQBall\App\ViewHttpResponse;
use IQBall\Core\Http\HttpCodes;
use IQBall\Core\Http\HttpRequest;
use IQBall\Core\Http\HttpResponse;
use IQBall\Core\Http\JsonHttpResponse;
use IQBall\Core\Validation\ValidationFail;
use IQBall\Core\Validation\Validator;

@ -1,24 +1,23 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Paramètres</title>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Paramètres</title>
<style>
body {
padding-left: 10%;
padding-right: 10%;
}
</style>
</head>
<style>
body {
padding-left: 10%;
padding-right: 10%;
}
</style>
</head>
<body>
<button onclick="location.pathname='/home'">Retour</button>
<h1>Paramètres</h1>
</body>
<body>
<button onclick="location.pathname='{{ basePath }}/home'">Retour</button>
<h1>Paramètres</h1>
</body>
</html>

@ -59,11 +59,11 @@
background-color: #0056b3;
}
.role{
.role {
margin-top: 10px;
}
.radio{
.radio {
display: flex;
justify-content: space-between;
}
@ -73,7 +73,7 @@
<div class="container">
<h2>Ajouter un membre à votre équipe</h2>
<form action="/team/members/add" method="POST">
<form action="{{ basePath }}/team/members/add" method="POST">
<div class="form-group">
<label for="team">Team où ajouter le membre :</label>
<input type="text" id="team" name="team" required>
@ -81,7 +81,7 @@
<input type="text" id="mail" name="mail" required>
<fieldset class="role">
<legend >Rôle du membre dans l'équipe :</legend>
<legend>Rôle du membre dans l'équipe :</legend>
<div class="radio">
<label for="P">Joueur</label>
<input type="radio" id="P" name="role" value="P" checked />

@ -56,7 +56,7 @@
<div class="container">
<h2>Supprimez un membre de votre équipe</h2>
<form action="/team/members/remove" method="POST">
<form action="{{ basePath }}/team/members/remove" method="POST">
<div class="form-group">
<label for="team">Team où supprimer le membre :</label>
<input type="text" id="team" name="team" required>

@ -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 %}
</style>
<div class="container">
<center><h2>Se connecter</h2></center>
<form action="login" method="post">
<form action="{{ basePath }}/login" method="post">
<div class="form-group">
{% for name in fails %}
<label class="error-messages"> {{ name.getFieldName() }} : {{ name.getMessage()}} </label>
<label class="error-messages"> {{ name.getFieldName() }} : {{ name.getMessage() }} </label>
{% endfor %}
<label for="email">Email :</label>
<input type="text" id="email" name="email" required>
<label for= "password">Mot de passe :</label>
<label for="password">Mot de passe :</label>
<input type="password" id="password" name="password" required>
</div>

@ -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 %}
</style>
<div class="container">
<center><h2>S'enregistrer</h2></center>
<form action="register" method="post">
<form action="{{ basePath }}/register" method="post">
<div class="form-group">
{% for name in fails %}
<label class = "error-messages"> {{ name.getFieldName() }} : {{ name.getMessage() }} </label>
<label class="error-messages"> {{ name.getFieldName() }} : {{ name.getMessage() }} </label>
{% endfor %}
<label for="username">Nom d'utilisateur :</label>
<input type="text" id="username" name="username" required>
<label for= "password">Mot de passe :</label>
<label for="password">Mot de passe :</label>
<input type="password" id="password" name="password" required>
<label for="confirmpassword">Confirmer le mot de passe :</label>
<input type="password" id="confirmpassword" name="confirmpassword" required>

@ -1,85 +1,91 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Twig view</title>
<style>
body{
background-color: #f1f1f1;
display: flex;
flex-direction: column;
align-items: center;
}
<head>
<meta charset="UTF-8">
<title>Twig view</title>
<style>
body {
background-color: #f1f1f1;
display: flex;
flex-direction: column;
align-items: center;
}
section{
width: 60%;
}
section {
width: 60%;
}
.square{
width:50px;
height:50px;
}
.square {
width: 50px;
height: 50px;
}
#main_color {
border: solid;
background-color: {{ team.getInfo().getMainColor().getValue() }};
}
#second_color{
background-color: {{ team.getInfo().getSecondColor().getValue() }};
border: solid;
}
#main_color {
border: solid;
background-color: {{ team.getInfo().getMainColor().getValue() }};
}
.container{
background-color: #fff;
display: flex;
flex-direction: column;
align-items: center;
}
.team{
border-color: darkgrey;
border-radius: 20px;
#second_color {
background-color: {{ team.getInfo().getSecondColor().getValue() }};
border: solid;
}
}
.container {
background-color: #fff;
display: flex;
flex-direction: column;
align-items: center;
}
.color{
flex-direction: row;
justify-content: space-between;
}
.team {
border-color: darkgrey;
border-radius: 20px;
.logo{
height: 80px;
width: 80px;
}
}
</style>
</head>
<body>
<header>
<h1><a href="/">IQBall</a></h1>
</header>
.color {
flex-direction: row;
justify-content: space-between;
}
<section class="container">
.logo {
height: 80px;
width: 80px;
}
<div class="team container">
<div>
<h1>{{ team.getInfo().getName() }}</h1>
<img src="{{ team.getInfo().getPicture() }}" alt="Logo d'équipe" class="logo">
</div>
<div>
<div class="color"><p>Couleur principale : </p><div class="square" id="main_color"></div> </div>
<div class="color"><p>Couleur secondaire : </p><div class="square" id="second_color"></div></div>
</div>
</style>
</head>
<body>
<header>
<h1><a href="{{ basePath }}/">IQBall</a></h1>
</header>
{% for m in team.listMembers() %}
<p> {{ m.getUserId() }} </p>
{% if m.getRole().isCoach() %}
<p> : Coach</p>
{% else %}
<p> : Joueur</p>
{% endif %}
{% endfor %}
<section class="container">
<div class="team container">
<div>
<h1>{{ team.getInfo().getName() }}</h1>
<img src="{{ team.getInfo().getPicture() }}" alt="Logo d'équipe" class="logo">
</div>
<div>
<div class="color"><p>Couleur principale : </p>
<div class="square" id="main_color"></div>
</div>
<div class="color"><p>Couleur secondaire : </p>
<div class="square" id="second_color"></div>
</div>
</div>
{% for m in team.listMembers() %}
<p> {{ m.getUserId() }} </p>
{% if m.getRole().isCoach() %}
<p> : Coach</p>
{% else %}
<p> : Joueur</p>
{% endif %}
{% endfor %}
</div>
</section>
</body>
</section>
</body>
</html>

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

@ -1,57 +1,57 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Error</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
h1 {
color: #da6110;
text-align: center;
margin-bottom: 15px
}
h2 {
text-align: center;
margin-bottom: 15px;
margin-top: 15px
}
.button {
display: block;
cursor : pointer;
background-color: white;
color : black;
text-align: center;
font-size: 20px;
border-radius: 12px;
border : 2px solid #da6110;
margin-top: 15px;
}
.button:hover {
background-color: #da6110
}
</style>
</head>
<head>
<meta charset="UTF-8">
<title>Error</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
h1 {
color: #da6110;
text-align: center;
margin-bottom: 15px
}
h2 {
text-align: center;
margin-bottom: 15px;
margin-top: 15px
}
.button {
display: block;
cursor: pointer;
background-color: white;
color: black;
text-align: center;
font-size: 20px;
border-radius: 12px;
border: 2px solid #da6110;
margin-top: 15px;
}
.button:hover {
background-color: #da6110
}
</style>
</head>
<body>
<h1>IQBall</h1>
{% for fail in failures %}
<h2>{{ fail.getKind() }} : {{ fail.getMessage() }}</h2>
{% endfor %}
{% for fail in failures %}
<h2>{{ fail.getKind() }} : {{ fail.getMessage() }}</h2>
{% endfor %}
<button class="button" onclick="location.href='/home'" type="button">Retour à la page d'accueil</button>
<button class="button" onclick="location.href='{{ basePath }}/home'" type="button">Retour à la page d'accueil</button>
</body>
</html>

@ -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;
}
</style>
</head>
<body>
<div id="bandeau">
<h1>IQ Ball</h1>
<div id="account" onclick="location.pathname='/settings'">
<img
src="../../../front/assets/icon/account.svg"
<div id="bandeau">
<h1>IQ Ball</h1>
<div id="account" onclick="location.pathname='/settings'">
<img
src="{{ basePath }}/front/assets/icon/account.svg"
alt="Account logo"
/>
<p>Mon profil<p>
</div>
/>
<p>Mon profil
<p>
</div>
</div>
<h2>Mes équipes</h2>
<h2>Mes équipes</h2>
<button onclick="location.pathname='/team/new'"> Créer une nouvelle équipe </button>
<button onclick="location.pathname='{{ basePath }}/team/new'"> Créer une nouvelle équipe</button>
{% if recentTeam != null %}
{% for team in recentTeam %}
<div>
<p> {{team.name}} </p>
</div>
{% endfor %}
{% else %}
<p>Aucune équipe créé !</p>
{% endif %}
{% if recentTeam != null %}
{% for team in recentTeam %}
<div>
<p> {{ team.name }} </p>
</div>
{% endfor %}
{% else %}
<p>Aucune équipe créé !</p>
{% endif %}
<h2> Mes strategies </h2>
<h2> Mes strategies </h2>
<button onclick="location.pathname='/tactic/new'"> Créer une nouvelle tactique </button>
<button onclick="location.pathname='{{ basePath }}/tactic/new'"> Créer une nouvelle tactique</button>
{% if recentTactic != null %}
{% for tactic in recentTactic %}
<div onclick="location.pathname=/tactic/{{ strategie.id }}/edit">
<p> {{tactic.id}} - {{tactic.name}} - {{tactic.creation_date}} </p>
<button onclick="location.pathname='/tactic/{{ tactic.id }}/edit'"> Editer la stratégie {{tactic.id}} </button>
</div>
{% endfor %}
{% else %}
<p> Aucune tactique créé !</p>
{% endif %}
{% if recentTactic != null %}
{% for tactic in recentTactic %}
<div onclick="location.pathname={{ basePath }}/tactic/{{ strategie.id }}/edit">
<p> {{ tactic.id }} - {{ tactic.name }} - {{ tactic.creation_date }} </p>
<button onclick="location.pathname='{{ basePath }}/tactic/{{ tactic.id }}/edit'"> Editer la
stratégie {{ tactic.id }} </button>
</div>
{% endfor %}
{% else %}
<p> Aucune tactique créé !</p>
{% endif %}
</body>
</html>

@ -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 @@
<div class="container">
<h2>Créer une équipe</h2>
<form action="/team/new" method="post">
<form action="{{ basePath }}/team/new" method="post">
<div class="form-group">
<label for="name">Nom de l'équipe :</label>
<input type="text" id="name" name="name" required>
<label for= "picture">Logo:</label>
<input type="text" id="picture" name="picture" required >
<label for="picture">Logo:</label>
<input type="text" id="picture" name="picture" required>
<label for="main_color">Couleur principale</label>
<input type="color" id="main_color" name="main_color" required>
<label for="second_color">Couleur secondaire</label>

@ -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 @@
<div class="container">
<h2>Chercher une équipe</h2>
<form action="/team/search" method="post">
<form action="{{ basePath }}/team/search" method="post">
<div class="form-group">
<label for="name">Nom de l'équipe :</label>
<input type="text" id="name" name="name" required>

@ -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) {

Loading…
Cancel
Save