diff --git a/TP4/ex1/app/models/View.php b/TP4/ex1/app/models/View.php index 4bcc3b9d9f2b4f313ee5cbdea9163b4bb8dd9a8a..2d8f7701256563709c789a07d80017777d2ba647 100644 --- a/TP4/ex1/app/models/View.php +++ b/TP4/ex1/app/models/View.php @@ -20,8 +20,7 @@ class View } $viewPath = "resources/views" . $viewPath . ".php"; if(file_exists($viewPath)){ - echo self::render(self::parse($viewPath), $args); - return ""; + return self::render(self::parse($viewPath), $args); }else{ return "La vue " . $viewName . " est introuvable."; } @@ -36,10 +35,46 @@ class View public static function parse(string $viewPath): string { $viewContent = file_get_contents($viewPath); + + // On va chercher le @extends('nom-de-la-vue') (seul le premier est pris en compte) + preg_match("/@extends[ ]{0,1}\([\"|'](.*?)[\"|']\)/", $viewContent, $extendMatch); + if(!empty($extendMatch)){ + $contentToPlaceInExtends = str_replace($extendMatch[0], "", $viewContent); + $viewContent = self::load($extendMatch[1]); + + // Maintenant on va regarder si on a pas des @yield('nom') à remplacer + // On commence avec les @section("nom", "valeur"). L'ordre est important car la second regex peut englober la première. + preg_match_all("/@section[ ]{0,1}\([ ]{0,1}[\"|'](.*?)[\"|'][ ]{0,1},[ ]{0,1}[\"|'](.*?)[\"|'][ ]{0,1}\)/", + $contentToPlaceInExtends, $inlineSectionMatches); + for($i = 0; $i<count($inlineSectionMatches[0]); $i++){ + $viewContent = preg_replace( + "/@yield[ ]{0,1}\([\"|']" . preg_quote($inlineSectionMatches[1][$i]) . "[\"|']\)/", + $inlineSectionMatches[2][$i], + $viewContent + ); + + // On supprime la directive dans le contenu mis de côté car la regex du dessous capte aussi celle-ci. + $contentToPlaceInExtends = str_replace($inlineSectionMatches[0][$i], + $inlineSectionMatches[2][$i], + $contentToPlaceInExtends); + } + + // Et @section("nom") --> @endsection + preg_match_all("/@section[ ]{0,1}\([ ]{0,1}[\"|'](.*?)[\"|'][ ]{0,1}\)((\n|.)*?)@endsection/", + $contentToPlaceInExtends, $sectionMatches); + for($i = 0; $i<count($sectionMatches[0]); $i++){ + //echo $sectionMatches[1][$i] . " - " . $sectionMatches[2][$i]; + $viewContent = preg_replace( + "/@yield[ ]{0,1}\([\"|']" . preg_quote($sectionMatches[1][$i]) . "[\"|']\)/", + $sectionMatches[2][$i], + $viewContent + ); + } + } + // On va rechercher toutes les concaténations {{ $var }} $viewContent = preg_replace("/\{\{(.*?)\}\}/", "<?=$1?>", $viewContent); - // On va rechercher tous les @foreach($var1 as $var2) @endforeach $viewContent = preg_replace("/@foreach[ ]{0,1}\((.*?) as (.*?)\)/", "<?php foreach($1 as $2) { ?>", $viewContent); diff --git a/TP4/ex1/index.php b/TP4/ex1/index.php index 4b9c23098c254283740def79387fbe131281c9eb..3bf1b9cc2e6bdc097c35efb87f3716b55713a1fe 100644 --- a/TP4/ex1/index.php +++ b/TP4/ex1/index.php @@ -13,14 +13,14 @@ require_once("controller/ControleurAdherent.php"); Database::connect(); if(empty($_GET["action"])){ - ControleurAuteur::lireAuteurs(); + echo ControleurAuteur::lireAuteurs(); }else{ if(in_array($_GET["action"], get_class_methods('ControleurAuteur'))){ $action = $_GET["action"]; - ControleurAuteur::$action(); + echo ControleurAuteur::$action(); }elseif(in_array($_GET["action"], get_class_methods('ControleurAdherent'))){ $action = $_GET["action"]; - ControleurAdherent::$action(); + echo ControleurAdherent::$action(); } } diff --git a/TP4/ex1/resources/views/adherents/les-adherents.php b/TP4/ex1/resources/views/adherents/les-adherents.php index 202482dd5ee793cb312729b139523a3b153dd8b2..49444c3b189eb291d5a0f1ef4569bc64b4f82c63 100644 --- a/TP4/ex1/resources/views/adherents/les-adherents.php +++ b/TP4/ex1/resources/views/adherents/les-adherents.php @@ -1,10 +1,8 @@ -<html lang="fr"> -<head> - <meta charset="utf-8"> - <title>Liste des adhérents</title> - <?=view('components.head')?> -</head> -<body class=""> +@extends('layouts.page-layout') + +@section('title', 'Liste des adhérents') + +@section('content') <div class="container"> <h1>Liste de tous les adhérents</h1> <p>Voici la liste de tous les adhérents</p> @@ -24,6 +22,4 @@ @endforeach </div> </div> - <?=view('components.footer')?> -</body> -</html> \ No newline at end of file +@endsection \ No newline at end of file diff --git a/TP4/ex1/resources/views/adherents/un-adherent.php b/TP4/ex1/resources/views/adherents/un-adherent.php index 32e6eb1e814cb4bd724fa810bb5a37132961b796..217691b61ce37ddf7426d457d19aa91da625bf01 100644 --- a/TP4/ex1/resources/views/adherents/un-adherent.php +++ b/TP4/ex1/resources/views/adherents/un-adherent.php @@ -1,14 +1,10 @@ -<html lang="fr"> -<head> - <meta charset="utf-8"> - <title>Info de l'adhérent</title> - <?=view('components.head')?> -</head> -<body class=""> -<div class="container"> - <h1>Information de l'adhérent</h1> - {{ $adherent->afficher() }} -</div> -<?=view('components.footer')?> -</body> -</html><?php \ No newline at end of file +@extends('layouts.page-layout') + +@section('title', 'Info de l'adhérent') + +@section('content') + <div class="container"> + <h1>Information de l'adhérent</h1> + {{ $adherent->afficher() }} + </div> +@endsection \ 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 e152377b394d8de14aa93f213f51e51917ba9fbe..a9e2c3b7b36eb5fee70d6a426475821f00d88adf 100644 --- a/TP4/ex1/resources/views/auteurs/les-auteurs.php +++ b/TP4/ex1/resources/views/auteurs/les-auteurs.php @@ -1,10 +1,8 @@ -<html lang="fr"> -<head> - <meta charset="utf-8"> - <title>Liste des auteurs</title> - <?=view('components.head')?> -</head> -<body class=""> +@extends('layouts.page-layout') + +@section('title', 'Liste des auteurs') + +@section('content') <div class="container"> <h1>Liste de tous les auteurs</h1> <p>Voici la liste de tous les auteurs</p> @@ -24,6 +22,4 @@ @endforeach </div> </div> - <?=view('components.footer')?> -</body> -</html> \ No newline at end of file +@endsection \ No newline at end of file diff --git a/TP4/ex1/resources/views/auteurs/un-auteur.php b/TP4/ex1/resources/views/auteurs/un-auteur.php index 99ddff1ccc3a0849ff06824fecbe93e98af65dda..37bb1ecf7f379c77898db22b3cd94a664e090233 100644 --- a/TP4/ex1/resources/views/auteurs/un-auteur.php +++ b/TP4/ex1/resources/views/auteurs/un-auteur.php @@ -1,14 +1,10 @@ -<html lang="fr"> -<head> - <meta charset="utf-8"> - <title>Info de l'auteur</title> - <?=view('components.head')?> -</head> -<body class=""> -<div class="container"> - <h1>Information de l'auteur</h1> - {{ $auteur->afficher() }} -</div> -<?=view('components.footer')?> -</body> -</html> +@extends('layouts.page-layout') + +@section('title', 'Info de l'auteur') + +@section('content') + <div class="container"> + <h1>Information de l'auteur</h1> + {{ $auteur->afficher() }} + </div> +@endsection \ No newline at end of file diff --git a/TP4/ex1/resources/views/layouts/page-layout.php b/TP4/ex1/resources/views/layouts/page-layout.php new file mode 100644 index 0000000000000000000000000000000000000000..f87a0a175bb8df2b9f681c185e0d3d62edd5f3ad --- /dev/null +++ b/TP4/ex1/resources/views/layouts/page-layout.php @@ -0,0 +1,11 @@ +<html lang="fr"> +<head> + <meta charset="utf-8"> + <title>@yield("title")</title> + <?=view('components.head')?> +</head> +<body class=""> + @yield("content") + <?=view('components.footer')?> +</body> +</html> diff --git a/TP4/ex1/test.php b/TP4/ex1/test.php index b4b0768d6d4860515b8a279d57033cfd1a49c7ec..67f33f8c4bf9dc7db3eaaa75b8a620f289b99a2d 100644 --- a/TP4/ex1/test.php +++ b/TP4/ex1/test.php @@ -1,4 +1,3 @@ <?php $test = "TESTEUU"; -echo('Ceci est un <?=$test?>!'); -<?php foreach($2 as $3) { ?> \ No newline at end of file +echo('Ceci est un <?=$test?>!'); \ No newline at end of file