Skip to content
Snippets Groups Projects
Select Git revision
  • f12db57edc09bfb3f4710da29d46cf0f9092f7d6
  • main default protected
2 results

View.php

Blame
  • View.php 3.75 KiB
    <?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);
            $viewPath = "";
            foreach ($viewPathParts as $pathPart){
                $viewPath .= "/" . $pathPart;
            }
            $viewPath = "resources/views" . $viewPath . ".php";
            if(file_exists($viewPath)){
                return self::render(self::parse($viewPath), $args);
            }else{
                return "La vue " . $viewName . " est introuvable.";
            }
        }
    
        /**
         * S'occuper de remplir la template
         * @param string $viewPath
         * @param array $args
         * @return string
         */
        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