Coding Style: tryng to follow PSR-12
authorEduardo <[email protected]>
Sun, 26 Nov 2023 15:02:16 +0000 (16:02 +0100)
committerEduardo <[email protected]>
Sun, 26 Nov 2023 15:02:16 +0000 (16:02 +0100)
page-generator/generator.php
page-generator/index.php

index 1b919a7a2bcf3ff5ac8e31e223b840604629a09c..713e7ede6e92b20af306070f5818798e74be5bdb 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-const OPEN = true;
+const OPEN  = true;
 const CLOSE = false;
 
 class CodeGenerator
@@ -9,7 +9,7 @@ class CodeGenerator
      * Iterate through all folders and subfolders and make copy of files from
      * $fromFolder to $toFolder.
      */
-    function copyRecursive(string $fromFolder, string $toFolder)
+    public function copyRecursive(string $fromFolder, string $toFolder)
     {
         foreach ($i = new \RecursiveIteratorIterator(
             new \RecursiveDirectoryIterator($fromFolder, \RecursiveDirectoryIterator::SKIP_DOTS),
@@ -26,20 +26,22 @@ class CodeGenerator
     /**
      * Will delete a given folder and all its contents
      */
-    function deleteRecursive(string $dir)
+    public function deleteRecursive(string $dir)
     {
-        if (empty($dir))
+        if (empty($dir)) {
             return;
+        }
 
         if (file_exists($dir)) {
             foreach ($i = new \RecursiveIteratorIterator(
                 new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS),
                 \RecursiveIteratorIterator::CHILD_FIRST
             ) as $file) {
-                if ($file->isDir())
+                if ($file->isDir()) {
                     rmdir($file);
-                else
+                } else {
                     unlink($file);
+                }
             }
 
             rmdir($dir);
@@ -49,23 +51,25 @@ class CodeGenerator
     /**
      * Create a folder structure for a given list of pages
      */
-    function generateFoldersForPages(array $folderList, $basePath)
+    public function generateFoldersForPages(array $folderList, $basePath)
     {
         foreach ($folderList as $folder) {
-            if ($folder == '/')
+            if ($folder == '/') {
                 mkdir($basePath . "/index");
-            else
+            } else {
                 mkdir($basePath . '/' . $folder);
+            }
         }
     }
 
     /**
      * Create or append to a file ($filePath) the text in $content
      */
-    function writeToFile(string $filePath, string | null $content)
+    public function writeToFile(string $filePath, string | null $content)
     {
-        if (is_null($content))
+        if (is_null($content)) {
             return;
+        }
 
         file_put_contents($filePath, $content, FILE_APPEND | LOCK_EX);
     }
@@ -73,7 +77,7 @@ class CodeGenerator
     /**
      * returns a string with the var name encoded to be set later with setTemplateVar()
      */
-    function templateVar(string $name): string
+    public function templateVar(string $name): string
     {
         return "$$" . $name . "$$";
     }
@@ -81,25 +85,27 @@ class CodeGenerator
     /**
      * Replace a template variable with a given value and return the new content string
      */
-    function setTemplateVar(string $filePath, string $var, string $value)
+    public function setTemplateVar(string $filePath, string $var, string $value)
     {
         $replaceVar = "$$" . $var . "$$";
 
-        if (file_exists($filePath))
+        if (file_exists($filePath)) {
             return str_replace($replaceVar, $value, file_get_contents($filePath));
-        else
+        } else {
             return str_replace($replaceVar, $value, $filePath);
+        }
     }
 
     /**
      * Returns a string with a span tag with a title set
      */
-    function spanTitle(string $title, string $text, array | null $class): string
+    public function spanTitle(string $title, string $text, array | null $class): string
     {
         $html = "<span";
 
-        if (!is_null($class))
+        if (!is_null($class)) {
             $html .= ' class="' . implode(' ', $class) . '"';
+        }
 
         $html .= " title='$title'>$text</span> ";
 
@@ -109,17 +115,19 @@ class CodeGenerator
     /**
      * Returns a string with an anchor tag
      */
-    function a(string $url, string $text, string | null $title, array | null $class): string
+    public function a(string $url, string $text, string | null $title, array | null $class): string
     {
         $html = "<a";
 
-        if (!is_null($class))
+        if (!is_null($class)) {
             $html .= ' class="' . implode(' ', $class) . '"';
+        }
 
         $html .= " href='$url'";
 
-        if (!is_null($title))
+        if (!is_null($title)) {
             $html .= " title='$title'";
+        }
 
         $html .= ">$text</a> ";
 
@@ -128,7 +136,7 @@ class CodeGenerator
 
     /**
      * Returns a string with an image tag
-     * 
+     *
      * @param string $src - path to the image
      * @param string $alt - alt text for the image
      * @param string | null $title - title for the image
@@ -136,12 +144,13 @@ class CodeGenerator
      * @param string | null $id - id for the image, if $url is not null used for the anchor tag else for the image tag
      * @param array | null $class - array of classes for the image
      */
-    function img(string $src, string $alt, string | null $title, string | null $url, string | null $id, array | null $class): string
+    public function img(string $src, string $alt, string | null $title, string | null $url, string | null $id, array | null $class): string
     {
         $html = "";
 
-        if (!is_null($url))
-        $html .= "<a href='$url' id='$id'>";
+        if (!is_null($url)) {
+            $html .= "<a href='$url' id='$id'>";
+        }
 
         $html .= "<img";
 
@@ -151,12 +160,14 @@ class CodeGenerator
         }
 
         $html .= " src='$src' alt='$alt'";
-        
-        if (!is_null($title))
+
+        if (!is_null($title)) {
             $html .= " title='$title'";
+        }
 
-        if (is_null($url) && !is_null($id))
+        if (is_null($url) && !is_null($id)) {
             $html .= " id='$id'";
+        }
 
         $html .= "/>";
 
@@ -169,30 +180,29 @@ class CodeGenerator
 
     /**
      * Forms the html for a given array of data
-     * 
-     * @param array $data 
-     * - array of arrays with page data. 
+     *
+     * @param array $data
+     * - array of arrays with page data.
      * - Keys expected: section, text, url, title, class
      * @param string $lang - TODO: LANGUAGE NOT IMPLEMENTED
      * - language of the page
      * - will try to get localized strings (section-lang, text-lang, title-lang....)
-     * 
+     *
      * @return string the html formed from the given data with the given language or the default language if given blank or missing
      */
-    function form_html(array $data, string $lang = null): string
+    public function form_html(array $data, string $lang = null): string
     {
-        $html = "";
-        $is_p_closed = true;
+        $html           = "";
+        $is_p_closed    = true;
         $is_list_closed = true;
-        $is_div_closed = true;
-
+        $is_div_closed  = true;
 
         foreach ($data as $i => $line) {
             $section = $this->str_val_or_empty($line, 'section');
-            $text = $this->str_val_or_empty($line, 'text');
-            $url = $this->str_val_or_empty($line, 'url');
-            $title = $this->str_val_or_empty($line, 'title');
-            $class = array_map('trim', explode(",", $this->str_val_or_empty($line, 'class')));
+            $text    = $this->str_val_or_empty($line, 'text');
+            $url     = $this->str_val_or_empty($line, 'url');
+            $title   = $this->str_val_or_empty($line, 'title');
+            $class   = array_map('trim', explode(",", $this->str_val_or_empty($line, 'class')));
 
             // if new-col close p and ul and open div with col class
             if (in_array("new-col", $class)) {
@@ -209,7 +219,6 @@ class CodeGenerator
                 $is_div_closed = false;
             }
 
-
             switch ($class) {
                 case in_array("list", $class):
                     // if previous iteration was diferent section close li
@@ -263,8 +272,9 @@ class CodeGenerator
 
             // if class not empty add class
             $filtered_classes = $this->filter_meta_classes($class);
-            if (!empty($filtered_classes))
+            if (!empty($filtered_classes)) {
                 $html .= ' class="' . implode(' ', $filtered_classes) . '"';
+            }
 
             $html .= '>';
         } elseif (!$is_p_closed && !$open) {
@@ -285,8 +295,9 @@ class CodeGenerator
 
             // if class not empty and text empty add class
             $filtered_classes = $this->filter_meta_classes($class);
-            if (empty($text) && !empty($filtered_classes))
+            if (empty($text) && !empty($filtered_classes)) {
                 $html .= ' class="' . implode(' ', $filtered_classes) . '"';
+            }
 
             $html .= '>';
         } elseif (!$is_list_closed && !$open) {
@@ -309,16 +320,19 @@ class CodeGenerator
             $html .= '<h2';
             // if text is empty add class, title and url
             $filtered_classes = $this->filter_meta_classes($class);
-            if (empty($text) && !empty($filtered_classes))
+            if (empty($text) && !empty($filtered_classes)) {
                 $html .= ' class="' . implode(' ', $filtered_classes) . '"';
+            }
+
             $html .= '>';
 
-            if (empty($text) && !empty($url))
+            if (empty($text) && !empty($url)) {
                 $html .= $this->a($url, $section, $title, $filtered_classes);
-            elseif (empty($text) && !empty($title))
+            } elseif (empty($text) && !empty($title)) {
                 $html .= $this->spanTitle($title, $section, $filtered_classes);
-            else
+            } else {
                 $html .= $section;
+            }
 
             $html .= '</h2>';
         }
@@ -335,34 +349,40 @@ class CodeGenerator
         return array_diff($class, $meta_classes);
     }
 
-
     private function form_content(string $text, array $class, string $url, string $title): string
     {
-        $html = "";
+        $html             = "";
         $filtered_classes = $this->filter_meta_classes($class);
 
         // if has title and url = a with title
-        if (!empty($title) && !empty($url))
+        if (!empty($title) && !empty($url)) {
             $html .= $this->a($url, $text, $title, $filtered_classes);
+        }
+
         // if has only title = span with title
         elseif (!empty($title)) {
             $html .= $this->spanTitle($title, $text, $filtered_classes);
         }
         // if has only url = a
-        elseif (!empty($url))
+        elseif (!empty($url)) {
             $html .= $this->a($url, $text, null, $filtered_classes);
+        }
+
         // else just text = p
-        else
+        else {
             $html .= $text;
+        }
 
         // if in class array is not "same-line" add br
-        if (!in_array("same-line", $class))
+        if (!in_array("same-line", $class)) {
             $html .= '<br>';
-        else
+        } else {
             $html .= ' ';
+        }
 
-        if (in_array("no-space", $class))
+        if (in_array("no-space", $class)) {
             $html = trim($html);
+        }
 
         return $html;
     }
@@ -370,41 +390,50 @@ class CodeGenerator
     private function form_list(string $text, array $class, string $url, string $title): string
     {
         // if in class array is not "same-line" set flag
-        if (!in_array("same-line", $class))
+        if (!in_array("same-line", $class)) {
             $this->is_li_same_line = false;
-        else
+        } else {
             $this->is_li_same_line = true;
+        }
 
-        if (!isset($this->was_li_same_line) || !$this->was_li_same_line)
+        if (!isset($this->was_li_same_line) || !$this->was_li_same_line) {
             $html = "<li";
-        else
+        } else {
             $html = "";
+        }
 
         // if class not empty add class
         $filtered_classes = $this->filter_meta_classes($class);
-        if (!empty($filtered_classes))
+        if (!empty($filtered_classes)) {
             $html .= ' class="' . implode(' ', $filtered_classes) . '"';
+        }
 
         // if has title and url = a with title
         if (!empty($url)) {
-            if (!isset($this->was_li_same_line) || !$this->was_li_same_line)
+            if (!isset($this->was_li_same_line) || !$this->was_li_same_line) {
                 $html .= '>';
+            }
+
             $html .= $this->a($url, $text, $title, $filtered_classes);
 
-            if (in_array("no-space", $class))
+            if (in_array("no-space", $class)) {
                 $html = trim($html);
+            }
         }
         // if has only title = span with title
         elseif (!empty($title)) {
             $html .= " title='$title'>$text";
         } else {
-            if (!isset($this->was_li_same_line) || !$this->was_li_same_line)
+            if (!isset($this->was_li_same_line) || !$this->was_li_same_line) {
                 $html .= '>';
+            }
+
             $html .= "$text";
         }
 
-        if (!$this->is_li_same_line)
+        if (!$this->is_li_same_line) {
             $html .= '</li>';
+        }
 
         $this->was_li_same_line = $this->is_li_same_line;
         return $html;
index d1ea3e16b6a2b1a42071e1af84a40320cbf9b7f5..8c884c2de0dca1e416593be8f88e7ceafa81358f 100644 (file)
@@ -1,12 +1,12 @@
 <?php
 
-require_once("generator.php");
+require_once "generator.php";
 
 // constans
-const URL = "http://localhost:8000";
+const URL            = "http://localhost:8000";
 const BASE_PATH_TEMP = "../generated_temp";
-const SRC_PATH = "../src";
-const PAGE_TITLE = "TheRiceGoat";
+const SRC_PATH       = "../src";
+const PAGE_TITLE     = "TheRiceGoat";
 
 $generator = new CodeGenerator();
 
@@ -22,13 +22,13 @@ function load_data(string $url): mixed
     return $data;
 }
 
-$data = [];
-$data['langs'] = load_data(URL . '?sheet=_langs');
+$data           = [];
+$data['langs']  = load_data(URL . '?sheet=_langs');
 $data['images'] = load_data(URL . '?sheet=_images');
 $data['header'] = load_data(URL . '?sheet=_header');
 $data['footer'] = load_data(URL . '?sheet=_footer');
-$data['menu'] = load_data(URL . '?sheet=_menu');
-$data['pages'] = load_data(URL . '?sheet=_pages');
+$data['menu']   = load_data(URL . '?sheet=_menu');
+$data['pages']  = load_data(URL . '?sheet=_pages');
 
 // remove langs that are not active
 $data['langs'] = array_filter($data['langs'], function ($item) {
@@ -38,8 +38,9 @@ $data['langs'] = array_filter($data['langs'], function ($item) {
 #endregion
 #region lang folders
 
-foreach ($data['langs'] as $item)
+foreach ($data['langs'] as $item) {
     mkdir(BASE_PATH_TEMP . "/" . $item['lang']);
+}
 
 #endregion
 #region assets and images
@@ -64,10 +65,11 @@ foreach ($data['images'] as $key => $image) {
 }
 // format image classes
 foreach ($data['images'] as $key => $image) {
-    if (empty($image['class']))
+    if (empty($image['class'])) {
         $data['images'][$key]['class'] = null;
-    else
+    } else {
         $data['images'][$key]['class'] = array_map('trim', explode(",", $image['class']));
+    }
 }
 
 // create partials:
@@ -101,8 +103,9 @@ foreach ($data['header'] as $item) {
     }
 }
 
-foreach ($data['langs'] as $lang)
+foreach ($data['langs'] as $lang) {
     $generator->writeToFile($head_path, '<link rel="alternate" hreflang="' . $lang['lang'] . '" href="' . URL . "/" . $lang['lang'] . '/">');
+}
 
 $generator->writeToFile($head_path, '
     <title>' . $generator->templateVar("page-title") . '</title>
@@ -144,7 +147,7 @@ foreach ($data['menu'] as $item) {
     if (boolval($item['active'])) {
         $generator->writeToFile($header_path, '<li>');
         $generator->writeToFile($header_path, $generator->a('../' . $item['path'], $item['item'], null, null));
-        $generator->writeToFile($header_path, '</li>');    
+        $generator->writeToFile($header_path, '</li>');
     }
 }
 $generator->writeToFile($header_path, '</ul>');
@@ -157,10 +160,13 @@ $generator->writeToFile($header_path, '</section>');
 
 // populate page array with paths
 $page_array = [];
-foreach ($data['menu'] as $item)
-    foreach ($item as $key => $value)
-        if ($key == 'path')
+foreach ($data['menu'] as $item) {
+    foreach ($item as $key => $value) {
+        if ($key == 'path') {
             $page_array[] = $value;
+        }
+    }
+}
 
 // generate page folders
 mkdir(BASE_PATH_TEMP . "/pages");
@@ -171,18 +177,20 @@ $tmp_head = $generator->setTemplateVar($head_path, "lang-code", "en");
 
 // populate an array with path => title
 $path_title = [];
-foreach ($menu_list as $item)
+foreach ($menu_list as $item) {
     $path_title[$item['path']] = $item['title'];
+}
 
 foreach ($data['pages'] as $page) {
-    $page_path = BASE_PATH_TEMP . "/pages/" . $page['path'] . "/index.html";
+    $page_path     = BASE_PATH_TEMP . "/pages/" . $page['path'] . "/index.html";
     $page['class'] = array_map('trim', explode(",", $page['class']));
 
     // put page title in header
-    if (!empty($path_title[$page['path']]))
+    if (!empty($path_title[$page['path']])) {
         $formed_head = $generator->setTemplateVar($tmp_head, "page-title", $path_title[$page['path']] . " | " . PAGE_TITLE);
-    else
+    } else {
         $formed_head = $generator->setTemplateVar($tmp_head, "page-title", PAGE_TITLE);
+    }
 
     // put headers
     $generator->writeToFile($page_path, $formed_head);
@@ -195,11 +203,12 @@ foreach ($data['pages'] as $page) {
     }
 
     // get page data
-    if (empty($data['pages'][$page['path']]))
+    if (empty($data['pages'][$page['path']])) {
         $data['pages'][$page['path']] = load_data(URL . '?sheet=' . $page['path']);
+    }
 
     $page_data = $data['pages'][$page['path']];
+
     // generate page
     $generator->writeToFile($page_path, $generator->form_html($page_data, "en"));
     $generator->writeToFile($page_path, '</section>');