diff --git a/.idea/php.xml b/.idea/php.xml index 7341688557427cb3965e7b2a4796a00419f40cbd..4bf49799ffc240ff0a23a5c49508865a5b75a9c6 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 0000000000000000000000000000000000000000..5200165ceea1a9ce0dc91c381b1043a26518932f --- /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 0000000000000000000000000000000000000000..c4a9e47d21a9f3faace1afdc5a6ccdbcd20c5f29 --- /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 0000000000000000000000000000000000000000..b284f36eb5c641609549817fc7c400b6c7315d0e --- /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 3c66dc58e5288edc29dead6cca1fd342891c304b..570f4a95f13f261ef74675375eb89a45d95a3348 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 0000000000000000000000000000000000000000..2459f6103e26a37b7acbb09c1334c84ba5df6561 --- /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 97c40407667dc9c9c5da29ce8da0c06dad7bf9da..0000000000000000000000000000000000000000 --- 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 4d19a797563984853245d4ab13566540675bb6b8..4b9c23098c254283740def79387fbe131281c9eb 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 387bdcc9652f3756ad5689d3f7e7339d9aab18af..32e6eb1e814cb4bd724fa810bb5a37132961b796 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 b8ac9ab9e44c9a1e05ba36441a4dc15792b574c8..c6fc74d48b3c36d414574d020dfa4e8c4083f669 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>