From b8edb27f1c7d96785d1f9eb27a62577bbac0428b Mon Sep 17 00:00:00 2001 From: "maxime.batista" Date: Tue, 7 Nov 2023 15:20:52 +0100 Subject: [PATCH 1/5] setup twig --- composer.json | 3 ++- public/index.php | 8 +++++++- src/Connexion.php | 2 +- src/Controller/SampleFormController.php | 26 ++++++++++++++++++++++++- src/Views/display_results.html.twig | 18 +++++++++++++++++ src/Views/sample_form.html.twig | 20 +++++++++++++++++++ 6 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 src/Views/display_results.html.twig create mode 100644 src/Views/sample_form.html.twig 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/src/Connexion.php b/src/Connexion.php index c8b4015..ef9c909 100644 --- a/src/Connexion.php +++ b/src/Connexion.php @@ -41,7 +41,7 @@ class Connexion { $stmnt->bindValue($name, $value[0], $value[1]); } $stmnt->execute(); - return $stmnt->fetchAll(); + return $stmnt->fetchAll(PDO::FETCH_ASSOC); } } \ 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..2ccabc1 --- /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 From edbd4705dc653e4272a96cd82a9218777177700f Mon Sep 17 00:00:00 2001 From: Override-6 Date: Wed, 8 Nov 2023 20:20:20 +0100 Subject: [PATCH 2/5] fix production server branch folders permissions --- ci/.drone.yml | 8 +++----- ci/deploy.sh | 9 +++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 ci/deploy.sh 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 From 947cda494aa9f096c47610a2a99657e6faf12c1b Mon Sep 17 00:00:00 2001 From: Override-6 Date: Wed, 8 Nov 2023 21:15:35 +0100 Subject: [PATCH 3/5] fix sample form twig submit action url --- src/Views/sample_form.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Views/sample_form.html.twig b/src/Views/sample_form.html.twig index 2ccabc1..bcb958e 100644 --- a/src/Views/sample_form.html.twig +++ b/src/Views/sample_form.html.twig @@ -8,7 +8,7 @@

Hello, this is a sample form made in Twig !

-
+ From 6aeefad5640e8bd92e8798ed229300b8f04420cf Mon Sep 17 00:00:00 2001 From: DahmaneYanis Date: Fri, 10 Nov 2023 09:54:55 +0100 Subject: [PATCH 4/5] Add some documentations... --- Documentation/conception.puml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Documentation/conception.puml 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 From b1ebd912df88faa28858907e4eee254840a5084f Mon Sep 17 00:00:00 2001 From: Override-6 Date: Fri, 10 Nov 2023 22:48:59 +0100 Subject: [PATCH 5/5] factorise database, set PDO::ERRMODE_EXCEPTION option --- sql/database.php | 2 +- src/Connexion.php | 14 ++++++++------ src/react-display-file.php | 1 - 3 files changed, 9 insertions(+), 8 deletions(-) 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 ef9c909..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(PDO::FETCH_ASSOC); + return $stmnt; } } \ 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 @@ "; } ?> - ">