From b9426312344e1c55a1dbfaeddb3da3c422490686 Mon Sep 17 00:00:00 2001
From: Sofiane Lasri <alasri250@gmail.com>
Date: Sat, 22 Oct 2022 13:03:46 +0200
Subject: [PATCH] Modification de la classe View, ajout des boucles foreach.

---
 TP4/ex1/app/models/View.php                   | 44 ++++++++++++++-----
 .../views/adherents/les-adherents.php         | 36 +++++++--------
 .../resources/views/auteurs/les-auteurs.php   | 12 ++---
 TP4/ex1/resources/views/auteurs/un-auteur.php |  2 +-
 TP4/ex1/test.php                              |  4 ++
 5 files changed, 61 insertions(+), 37 deletions(-)
 create mode 100644 TP4/ex1/test.php

diff --git a/TP4/ex1/app/models/View.php b/TP4/ex1/app/models/View.php
index 2459f61..4bcc3b9 100644
--- a/TP4/ex1/app/models/View.php
+++ b/TP4/ex1/app/models/View.php
@@ -1,7 +1,16 @@
 <?php
 
+/**
+ * Tentative de créer un système de template.
+ */
 class View
 {
+    /**
+     * S'occupe d'ouvrir la template.
+     * @param string $viewName
+     * @param array $args
+     * @return string
+     */
     public static function load(string $viewName, array $args = []): string
     {
         $viewPathParts = explode(".", $viewName);
@@ -11,29 +20,40 @@ class View
         }
         $viewPath = "resources/views" . $viewPath . ".php";
         if(file_exists($viewPath)){
-            echo self::parse($viewPath, $args);
+            echo self::render(self::parse($viewPath), $args);
             return "";
         }else{
             return "La vue " . $viewName . " est introuvable.";
         }
     }
 
-    public static function parse(string $viewPath, array $args = []): string
+    /**
+     * S'occuper de remplir la template
+     * @param string $viewPath
+     * @param array $args
+     * @return string
+     */
+    public static function parse(string $viewPath): string
     {
-        extract($args);
         $viewContent = file_get_contents($viewPath);
-        preg_match_all("/\{\{(.*?)\}\}/", $viewContent, $stringConcatMatches);
+        // On va rechercher toutes les concaténations {{ $var }}
+        $viewContent = preg_replace("/\{\{(.*?)\}\}/", "<?=$1?>", $viewContent);
 
-        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);
-            }
 
-        }
+        // On va rechercher tous les @foreach($var1 as $var2) @endforeach
+        $viewContent = preg_replace("/@foreach[ ]{0,1}\((.*?) as (.*?)\)/",
+            "<?php foreach($1 as $2) { ?>", $viewContent);
+        $viewContent = preg_replace("/@endforeach/", "<?php } ?>", $viewContent);
+
         return $viewContent;
+    }
 
+    public static function render(string $viewCode, array $args = []): string
+    {
+        extract($args);
+        // On ouvre le buffer pour enregistrer le résultat de l'echo.
+        ob_start();
+        eval("?>" . $viewCode . "<?php");
+        return ob_get_clean();
     }
 }
\ No newline at end of file
diff --git a/TP4/ex1/resources/views/adherents/les-adherents.php b/TP4/ex1/resources/views/adherents/les-adherents.php
index 01e941b..202482d 100644
--- a/TP4/ex1/resources/views/adherents/les-adherents.php
+++ b/TP4/ex1/resources/views/adherents/les-adherents.php
@@ -5,25 +5,25 @@
     <?=view('components.head')?>
 </head>
 <body class="">
-<div class="container">
-    <h1>Liste de tous les adhérents</h1>
-    <p>Voici la liste de tous les adhérents</p>
-    <div class="d-flex flex-wrap justify-content-between">
-        <?php foreach ($adherents as $adherent) { ?>
-            <div class="card mb-2" style="width: 18rem;">
-                <div class="card-body">
-                    <h5 class="card-title"><?=$adherent->getPrenomAdherent()?> <?=$adherent->getNomAdherent()?></h5>
-                    <p class="card-text">ID: <?=$adherent->getLogin()?><br>
-                        Date de naissance: <?=$adherent->getDateAdhesion()->format("Y-m-d")?></p>
-                    <a class="btn btn-primary"
-                       href="index.php?action=lireAdherent&login=<?=$adherent->getLogin()?>">
-                        Lire les détails
-                    </a>
+    <div class="container">
+        <h1>Liste de tous les adhérents</h1>
+        <p>Voici la liste de tous les adhérents</p>
+        <div class="d-flex flex-wrap justify-content-between">
+            @foreach($adherents as $adherent)
+                <div class="card mb-2" style="width: 18rem;">
+                    <div class="card-body">
+                        <h5 class="card-title">{{ $adherent->getPrenomAdherent() }} {{ $adherent->getNomAdherent() }}</h5>
+                        <p class="card-text">ID: {{ $adherent->getLogin() }}<br>
+                            Date de naissance: {{ $adherent->getDateAdhesion()->format("Y-m-d") }}</p>
+                        <a class="btn btn-primary"
+                           href="index.php?action=lireAdherent&login={{ $adherent->getLogin() }}">
+                            Lire les détails
+                        </a>
+                    </div>
                 </div>
-            </div>
-        <?php } ?>
+            @endforeach
+        </div>
     </div>
-</div>
-<?=view('components.footer')?>
+    <?=view('components.footer')?>
 </body>
 </html>
\ No newline at end of file
diff --git a/TP4/ex1/resources/views/auteurs/les-auteurs.php b/TP4/ex1/resources/views/auteurs/les-auteurs.php
index d9ad923..e152377 100644
--- a/TP4/ex1/resources/views/auteurs/les-auteurs.php
+++ b/TP4/ex1/resources/views/auteurs/les-auteurs.php
@@ -9,19 +9,19 @@
         <h1>Liste de tous les auteurs</h1>
         <p>Voici la liste de tous les auteurs</p>
         <div class="d-flex flex-wrap justify-content-between">
-            <?php foreach ($auteurs as $auteur) { ?>
+            @foreach ($auteurs as $auteur)
                 <div class="card mb-2" style="width: 18rem;">
                     <div class="card-body">
-                        <h5 class="card-title"><?=$auteur->getPrenom()?> <?=$auteur->getNom()?></h5>
-                        <p class="card-text">ID: <?=$auteur->getNumAuteur()?><br>
-                        Date de naissance: <?=$auteur->getAnneeNaissance()?></p>
+                        <h5 class="card-title">{{ $auteur->getPrenom() }} {{ $auteur->getNom() }}</h5>
+                        <p class="card-text">ID: {{ $auteur->getNumAuteur() }}<br>
+                        Date de naissance: {{ $auteur->getAnneeNaissance() }}</p>
                         <a class="btn btn-primary"
-                           href="index.php?action=lireAuteur&numAuteur=<?=$auteur->getNumAuteur()?>">
+                           href="index.php?action=lireAuteur&numAuteur={{ $auteur->getNumAuteur() }}">
                             Lire les détails
                         </a>
                     </div>
                 </div>
-            <?php } ?>
+            @endforeach
         </div>
     </div>
     <?=view('components.footer')?>
diff --git a/TP4/ex1/resources/views/auteurs/un-auteur.php b/TP4/ex1/resources/views/auteurs/un-auteur.php
index c6fc74d..99ddff1 100644
--- a/TP4/ex1/resources/views/auteurs/un-auteur.php
+++ b/TP4/ex1/resources/views/auteurs/un-auteur.php
@@ -11,4 +11,4 @@
 </div>
 <?=view('components.footer')?>
 </body>
-</html><?php
+</html>
diff --git a/TP4/ex1/test.php b/TP4/ex1/test.php
new file mode 100644
index 0000000..b4b0768
--- /dev/null
+++ b/TP4/ex1/test.php
@@ -0,0 +1,4 @@
+<?php
+$test = "TESTEUU";
+echo('Ceci est un <?=$test?>!');
+<?php foreach($2 as $3) { ?>
\ No newline at end of file
-- 
GitLab