From 63f9c2fce34a1c752038c594ed50b71ef7c433d3 Mon Sep 17 00:00:00 2001 From: Sofiane Lasri <alasri250@gmail.com> Date: Tue, 18 Oct 2022 12:06:20 +0200 Subject: [PATCH] =?UTF-8?q?Modification=20du=20syst=C3=A8me=20de=20vue=20-?= =?UTF-8?q?>=20Cr=C3=A9ation=20d'un=20syst=C3=A8me=20de=20concat=C3=A9nati?= =?UTF-8?q?on.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/php.xml | 2 +- TP4/ex1/.env | 4 ++ TP4/ex1/app/helpers/config.php | 19 +++++++++ TP4/ex1/app/helpers/views.php | 11 ++++++ TP4/ex1/{config => app/models}/Database.php | 10 ++--- TP4/ex1/app/models/View.php | 39 +++++++++++++++++++ TP4/ex1/helpers/views.php | 17 -------- TP4/ex1/index.php | 6 ++- .../resources/views/adherents/un-adherent.php | 2 +- TP4/ex1/resources/views/auteurs/un-auteur.php | 2 +- 10 files changed, 85 insertions(+), 27 deletions(-) create mode 100644 TP4/ex1/.env create mode 100644 TP4/ex1/app/helpers/config.php create mode 100644 TP4/ex1/app/helpers/views.php rename TP4/ex1/{config => app/models}/Database.php (78%) create mode 100644 TP4/ex1/app/models/View.php delete mode 100644 TP4/ex1/helpers/views.php diff --git a/.idea/php.xml b/.idea/php.xml index 7341688..4bf4979 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -1,4 +1,4 @@ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> - <component name="PhpProjectSharedConfiguration" php_language_level="7.4" /> + <component name="PhpProjectSharedConfiguration" php_language_level="8.0" /> </project> \ No newline at end of file diff --git a/TP4/ex1/.env b/TP4/ex1/.env new file mode 100644 index 0000000..5200165 --- /dev/null +++ b/TP4/ex1/.env @@ -0,0 +1,4 @@ +DB_HOST=localhost +DB_NAME=iut-dev +DB_LOGIN=iut-dev-user +DB_PASSWORD=p73i74KAV8lami2iyIpehE5ozic8GA diff --git a/TP4/ex1/app/helpers/config.php b/TP4/ex1/app/helpers/config.php new file mode 100644 index 0000000..c4a9e47 --- /dev/null +++ b/TP4/ex1/app/helpers/config.php @@ -0,0 +1,19 @@ +<?php +/** + * @param string $index + * @return string + */ +function config(string $index) : string +{ + $envPath = getcwd() . "/.env"; + if(file_exists($envPath)){ + $index = str_replace('_', '\\_', $index); + $envFile = file_get_contents($envPath); + preg_match("/" . $index . '=(.*?)\n/', $envFile, $matches); + $value = $matches[count($matches) - 1]; + $value = preg_replace('/[^A-Za-z0-9\-]/', '', $value); + return $value; + } else { + return ""; + } +} \ No newline at end of file diff --git a/TP4/ex1/app/helpers/views.php b/TP4/ex1/app/helpers/views.php new file mode 100644 index 0000000..b284f36 --- /dev/null +++ b/TP4/ex1/app/helpers/views.php @@ -0,0 +1,11 @@ +<?php +require_once("app/models/View.php"); +/** + * @param string $viewName + * @param array $args + * @return string + */ +function view(string $viewName, array $args = []): string +{ + return View::load($viewName, $args); +} \ No newline at end of file diff --git a/TP4/ex1/config/Database.php b/TP4/ex1/app/models/Database.php similarity index 78% rename from TP4/ex1/config/Database.php rename to TP4/ex1/app/models/Database.php index 3c66dc5..570f4a9 100644 --- a/TP4/ex1/config/Database.php +++ b/TP4/ex1/app/models/Database.php @@ -22,13 +22,13 @@ class Database // la fonction static de connexion qui initialise $pdo et lance la tentative de connexion static public function connect() { - $h = self::$hostname; - $d = self::$database; - $l = self::$login; - $p = self::$password; + $hostname = config("DB_HOST"); + $database = config("DB_NAME"); + $login = config("DB_LOGIN"); + $password = config("DB_PASSWORD"); $t = self::$tabUTF8; try { - self::$pdo = new PDO("mysql:host=$h;dbname=$d", $l, $p, $t); + self::$pdo = new PDO("mysql:host=$hostname;dbname=$database", $login, $password, $t); self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { echo "Erreur de connexion : " . $e->getMessage() . "<br>"; diff --git a/TP4/ex1/app/models/View.php b/TP4/ex1/app/models/View.php new file mode 100644 index 0000000..2459f61 --- /dev/null +++ b/TP4/ex1/app/models/View.php @@ -0,0 +1,39 @@ +<?php + +class View +{ + public static function load(string $viewName, array $args = []): string + { + $viewPathParts = explode(".", $viewName); + $viewPath = ""; + foreach ($viewPathParts as $pathPart){ + $viewPath .= "/" . $pathPart; + } + $viewPath = "resources/views" . $viewPath . ".php"; + if(file_exists($viewPath)){ + echo self::parse($viewPath, $args); + return ""; + }else{ + return "La vue " . $viewName . " est introuvable."; + } + } + + public static function parse(string $viewPath, array $args = []): string + { + extract($args); + $viewContent = file_get_contents($viewPath); + preg_match_all("/\{\{(.*?)\}\}/", $viewContent, $stringConcatMatches); + + foreach ($stringConcatMatches as $match){ + if(!empty($match) && !str_contains($match[0], "{{")){ + ob_start(); + eval("echo " . $match[0] . ";"); + $result = ob_get_clean(); + $viewContent = preg_replace("/\{\{" . preg_quote($match[0]) . "\}\}/", $result, $viewContent); + } + + } + return $viewContent; + + } +} \ No newline at end of file diff --git a/TP4/ex1/helpers/views.php b/TP4/ex1/helpers/views.php deleted file mode 100644 index 97c4040..0000000 --- a/TP4/ex1/helpers/views.php +++ /dev/null @@ -1,17 +0,0 @@ -<?php -function view(string $viewName, array $args = []): string -{ - $viewPathParts = explode(".", $viewName); - $viewPath = ""; - foreach ($viewPathParts as $pathPart){ - $viewPath .= "/" . $pathPart; - } - $viewPath = "resources/views" . $viewPath . ".php"; - if(file_exists($viewPath)){ - extract($args); - include $viewPath; - return ""; - }else{ - return "La vue " . $viewName . " est introuvable."; - } -} \ No newline at end of file diff --git a/TP4/ex1/index.php b/TP4/ex1/index.php index 4d19a79..4b9c230 100644 --- a/TP4/ex1/index.php +++ b/TP4/ex1/index.php @@ -3,8 +3,10 @@ ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); -require_once("config/Database.php"); -require_once("helpers/views.php"); +require_once("app/helpers/config.php"); +require_once("app/helpers/views.php"); +require_once("app/models/Database.php"); + require_once("controller/ControleurAuteur.php"); require_once("controller/ControleurAdherent.php"); diff --git a/TP4/ex1/resources/views/adherents/un-adherent.php b/TP4/ex1/resources/views/adherents/un-adherent.php index 387bdcc..32e6eb1 100644 --- a/TP4/ex1/resources/views/adherents/un-adherent.php +++ b/TP4/ex1/resources/views/adherents/un-adherent.php @@ -7,7 +7,7 @@ <body class=""> <div class="container"> <h1>Information de l'adhérent</h1> - <?=$adherent->afficher()?> + {{ $adherent->afficher() }} </div> <?=view('components.footer')?> </body> diff --git a/TP4/ex1/resources/views/auteurs/un-auteur.php b/TP4/ex1/resources/views/auteurs/un-auteur.php index b8ac9ab..c6fc74d 100644 --- a/TP4/ex1/resources/views/auteurs/un-auteur.php +++ b/TP4/ex1/resources/views/auteurs/un-auteur.php @@ -7,7 +7,7 @@ <body class=""> <div class="container"> <h1>Information de l'auteur</h1> - <?=$auteur->afficher()?> + {{ $auteur->afficher() }} </div> <?=view('components.footer')?> </body> -- GitLab