diff --git a/Documentation/conception.puml b/Documentation/conception.puml new file mode 100644 index 0000000..06ae256 --- /dev/null +++ b/Documentation/conception.puml @@ -0,0 +1,12 @@ +@startuml + +class Connexion + +class Modele + +class Account + +class AccountGateway + + +@enduml diff --git a/ci/.drone.yml b/ci/.drone.yml index 8a28ad7..e3ae556 100644 --- a/ci/.drone.yml +++ b/ci/.drone.yml @@ -44,8 +44,6 @@ steps: SERVER_PRIVATE_KEY: from_secret: SERVER_PRIVATE_KEY commands: - - mkdir ~/.ssh - - echo "$SERVER_PRIVATE_KEY" > ~/.ssh/id_rsa - - chmod 0600 ~/.ssh - - chmod 0500 ~/.ssh/id_rsa* - - rsync -avz -e "ssh -p 80 -o 'StrictHostKeyChecking=no'" --delete /outputs/* iqball@maxou.dev:/server/nginx/IQBall/$DRONE_BRANCH + - chmod +x ci/deploy.sh + - ci/deploy.sh + diff --git a/ci/deploy.sh b/ci/deploy.sh new file mode 100644 index 0000000..b870658 --- /dev/null +++ b/ci/deploy.sh @@ -0,0 +1,9 @@ +set -e + +mkdir ~/.ssh +echo "$SERVER_PRIVATE_KEY" > ~/.ssh/id_rsa +chmod 0600 ~/.ssh +chmod 0500 ~/.ssh/id_rsa* +ssh -p 80 -o 'StrictHostKeyChecking=no' iqball@maxou.dev mkdir -p /server/nginx/IQBall/$DRONE_BRANCH +rsync -avz -e "ssh -p 80 -o 'StrictHostKeyChecking=no'" --delete /outputs/* iqball@maxou.dev:/server/nginx/IQBall/$DRONE_BRANCH +ssh -p 80 -o 'StrictHostKeyChecking=no' iqball@maxou.dev "chmod 770 /server/nginx/IQBall/$DRONE_BRANCH; chgrp www-data /server/nginx/IQBall/$DRONE_BRANCH" \ No newline at end of file diff --git a/composer.json b/composer.json index c3bb579..c78fb15 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,7 @@ "altorouter/altorouter": "1.2.0", "ext-json": "*", "ext-pdo": "*", - "ext-pdo_sqlite": "*" + "ext-pdo_sqlite": "*", + "twig/twig":"^2.0" } } \ No newline at end of file diff --git a/public/index.php b/public/index.php index 14b0756..4c5290b 100644 --- a/public/index.php +++ b/public/index.php @@ -4,6 +4,7 @@ require "../vendor/autoload.php"; require "../config.php"; require "../sql/database.php"; +use \Twig\Loader\FilesystemLoader; use App\Connexion; use App\Controller\SampleFormController; use App\Gateway\FormResultGateway; @@ -23,6 +24,9 @@ function get_base_path() { return $basePath; } +$loader = new FilesystemLoader('../src/Views/'); +$twig = new \Twig\Environment($loader); + $basePath = get_base_path(); $con = new Connexion(get_database()); @@ -30,9 +34,11 @@ $con = new Connexion(get_database()); $router = new AltoRouter(); $router->setBasePath($basePath); -$sampleFormController = new SampleFormController(new FormResultGateway($con)); +$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig); $router->map("GET", "/", fn() => $sampleFormController->displayForm()); $router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST)); +$router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig()); +$router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST)); $match = $router->match(); diff --git a/sql/database.php b/sql/database.php index 6fae3ea..d49ddfd 100644 --- a/sql/database.php +++ b/sql/database.php @@ -6,7 +6,7 @@ function get_database(): PDO { // defined by profiles. global $data_source_name; - $pdo = new PDO($data_source_name, DATABASE_USER, DATABASE_PASSWORD); + $pdo = new PDO($data_source_name, DATABASE_USER, DATABASE_PASSWORD, [PDO::ERRMODE_EXCEPTION]); $database_exists = $pdo->query("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table'")->fetchColumn() > 0; diff --git a/src/Connexion.php b/src/Connexion.php index c8b4015..dc9beae 100644 --- a/src/Connexion.php +++ b/src/Connexion.php @@ -22,10 +22,7 @@ class Connexion { * @return void */ public function exec(string $query, array $args) { - $stmnt = $this->pdo->prepare($query); - foreach ($args as $name => $value) { - $stmnt->bindValue($name, $value[0], $value[1]); - } + $stmnt = $this->prepare($query, $args); $stmnt->execute(); } @@ -36,12 +33,17 @@ class Connexion { * @return array the returned rows of the request */ public function fetch(string $query, array $args): array { + $stmnt = $this->prepare($query, $args); + $stmnt->execute(); + return $stmnt->fetchAll(PDO::FETCH_ASSOC); + } + + private function prepare(string $query, array $args): \PDOStatement { $stmnt = $this->pdo->prepare($query); foreach ($args as $name => $value) { $stmnt->bindValue($name, $value[0], $value[1]); } - $stmnt->execute(); - return $stmnt->fetchAll(); + return $stmnt; } } \ No newline at end of file diff --git a/src/Controller/SampleFormController.php b/src/Controller/SampleFormController.php index 57625e9..ad77d62 100644 --- a/src/Controller/SampleFormController.php +++ b/src/Controller/SampleFormController.php @@ -4,17 +4,23 @@ namespace App\Controller; require_once __DIR__ . "/../react-display.php"; use App\Gateway\FormResultGateway; +use \Twig\Environment; +use Twig\Error\LoaderError; +use Twig\Error\RuntimeError; +use Twig\Error\SyntaxError; class SampleFormController { private FormResultGateway $gateway; + private Environment $twig; /** * @param FormResultGateway $gateway */ - public function __construct(FormResultGateway $gateway) + public function __construct(FormResultGateway $gateway, Environment $twig) { $this->gateway = $gateway; + $this->twig = $twig; } @@ -27,4 +33,22 @@ class SampleFormController { $results = ["results" => $this->gateway->listResults()]; send_react_front("views/DisplayResults.tsx", $results); } + + public function displayFormTwig() { + try { + echo $this->twig->render('sample_form.html.twig', []); + } catch (LoaderError | RuntimeError | SyntaxError $e) { + echo "Twig error: $e"; + } + } + + public function submitFormTwig(array $request) { + $this->gateway->insert($request["name"], $request["description"]); + try { + $results = $this->gateway->listResults(); + echo $this->twig->render('display_results.html.twig', ['results' => $results]); + } catch (LoaderError | RuntimeError | SyntaxError $e) { + echo "Twig error: $e"; + } + } } \ No newline at end of file diff --git a/src/Views/display_results.html.twig b/src/Views/display_results.html.twig new file mode 100644 index 0000000..6d2aef0 --- /dev/null +++ b/src/Views/display_results.html.twig @@ -0,0 +1,18 @@ + + + + + Twig view + + + +

Hello world

+ + +{% for v in results %} +

username: {{ v.name }}

+

description: {{ v.description }}

+{% endfor %} + + + \ No newline at end of file diff --git a/src/Views/sample_form.html.twig b/src/Views/sample_form.html.twig new file mode 100644 index 0000000..bcb958e --- /dev/null +++ b/src/Views/sample_form.html.twig @@ -0,0 +1,20 @@ + + + + + Twig view + + + +

Hello, this is a sample form made in Twig !

+ +
+ + + + + +
+ + + \ No newline at end of file diff --git a/src/react-display-file.php b/src/react-display-file.php index 55d9656..89ad7bb 100755 --- a/src/react-display-file.php +++ b/src/react-display-file.php @@ -14,7 +14,6 @@ "; } ?> - ">