move all data loaded from server to the same region
authorEduardo <[email protected]>
Sun, 12 Nov 2023 19:34:02 +0000 (20:34 +0100)
committerEduardo <[email protected]>
Sun, 12 Nov 2023 19:34:02 +0000 (20:34 +0100)
page-generator/index.php

index 2b68714b68ff418a2d932a74bdb27daa05477f0c..a4b35b67cd4ab34c7db3550945a48160d231a050 100644 (file)
@@ -13,33 +13,47 @@ $generator = new CodeGenerator();
 // create new folder (dont remove the old one yet)
 mkdir(BASE_PATH_TEMP);
 
+#region load data
+
+function load_data(string $url): mixed
+{
+    $data = file_get_contents($url);
+    $data = json_decode($data, true);
+    return $data;
+}
+
+$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');
+
+#endregion
+
+#region assets and images
+
 // copy assets
 mkdir(BASE_PATH_TEMP . "/assets");
 $generator->copyRecursive(SRC_PATH . "/assets", BASE_PATH_TEMP . "/assets");
 
-// get images sheet
-$image_list = file_get_contents(URL . '?sheet=_images');
-$image_list = json_decode($image_list, true);
-
 // copy images from whatever they are (if this is in a docker container they may be mounted somewhere)
-foreach ($image_list as $key => $image) {
+foreach ($data['images'] as $key => $image) {
     $old_path = SRC_PATH . $image['path'];
     $new_path = BASE_PATH_TEMP . "/assets/img/" . $image['id'] . '.' . pathinfo($image['path'], PATHINFO_EXTENSION);
     copy($old_path, $new_path);
 
-    $image_list[$key]['path'] = "../../assets/img/" . $image['id'] . '.' . pathinfo($image['path'], PATHINFO_EXTENSION);
+    $data['images'][$key]['path'] = "../../assets/img/" . $image['id'] . '.' . pathinfo($image['path'], PATHINFO_EXTENSION);
 }
 
 // create partials:
 $partialsPath = BASE_PATH_TEMP . "/partials";
 mkdir($partialsPath);
 
+#endregion
 #region head
 
-// get head data
-$head_data = file_get_contents(URL . '?sheet=_header');
-$head_data = json_decode($head_data, true);
-
 $head_path = $partialsPath . "/head.html";
 
 // generate head
@@ -48,7 +62,7 @@ $generator->writeToFile($head_path, '
 <html lang="' . $generator->templateVar("lang-code") . '">
 <head>');
 
-foreach ($head_data as $item) {
+foreach ($data['header'] as $item) {
     switch ($item['type']) {
         case 'meta':
             $generator->writeToFile($head_path, '<meta ' . $item['value'] . '>');
@@ -73,16 +87,12 @@ $generator->writeToFile($head_path, '
 #endregion
 #region footer
 
-// get footer data
-$footer_data = file_get_contents(URL . '?sheet=_footer');
-$footer_data = json_decode($footer_data, true);
-
 // generate footer
 $footer_path = $partialsPath . "/footer.html";
 $generator->writeToFile($footer_path, '<section id="footer"><p>');
 
 $footer_line = 1;
-foreach ($footer_data as $item) {
+foreach ($data['footer'] as $item) {
     if ($item['line'] != $footer_line) {
         $footer_line = $item['line'];
         $generator->writeToFile($footer_path, '<br>');
@@ -111,13 +121,13 @@ $menu_list = json_decode($menu_list, true);
 $generator->writeToFile($header_path, '<section id="header">');
 
 // logo (if any, else probably just a broken image with nothing inside)
-$logo = $image_list[array_search("logo", array_column($image_list, "id"))];
+$logo = $data['images'][array_search("logo", array_column($data['images'], "id"))];
 $generator->writeToFile($header_path, '<img src="' . $logo['path'] . '" alt="' . $logo['description'] . '" />');
 
 $generator->writeToFile($header_path, '<nav id="menu">');
 
 $generator->writeToFile($header_path, '<ul>');
-foreach ($menu_list as $item) {
+foreach ($data['menu'] as $item) {
     $generator->writeToFile($header_path, '<li>');
     $generator->writeToFile($header_path, $generator->a('../' . $item['path'], $item['item']));
     $generator->writeToFile($header_path, '</li>');
@@ -132,7 +142,7 @@ $generator->writeToFile($header_path, '</section>');
 
 // populate page array with paths
 $page_array = [];
-foreach ($menu_list as $item)
+foreach ($data['menu'] as $item)
     foreach ($item as $key => $value)
         if ($key == 'path')
             $page_array[] = $value;
@@ -149,11 +159,7 @@ $path_title = [];
 foreach ($menu_list as $item)
     $path_title[$item['path']] = $item['title'];
 
-// get page list
-$page_list = file_get_contents(URL . '?sheet=_pages');
-$page_list = json_decode($page_list, true);
-
-foreach ($page_list as $page) {
+foreach ($data['pages'] as $page) {
     $page_path = BASE_PATH_TEMP . "/pages/" . $page['path'] . "/index.html";
 
     // put page title in header
@@ -173,8 +179,11 @@ foreach ($page_list as $page) {
     }
 
     // get page data
-    $page_data = file_get_contents(URL . '?sheet=' . $page['path']);
-    $page_data = json_decode($page_data, true);
+
+    if (empty($data['pages'][$page['path']]))
+        $data['pages'][$page['path']] = load_data(URL . '?sheet=' . $page['path']);
+
+    $page_data = $data['pages'][$page['path']];
 
     switch ($page["type"]) {
         case '1-col':