From 1d6b1076b9fc039dc3eb8ca080bde3c04b42d1bb Mon Sep 17 00:00:00 2001 From: Roman Parpalak Date: Sat, 30 Mar 2024 19:06:28 +0200 Subject: [PATCH] Removed templates from Zeta style (seems like there is no more need to keep duplicates). --- .../s2_blog/Controller/BlogController.php | 2 +- _extensions/s2_blog/templates/blog.php | 4 +- _extensions/s2_blog/templates/blog_main.php | 4 +- .../src/Template/HtmlTemplateProvider.php | 41 ++++++---- _include/src/Template/Viewer.php | 82 +++++++++---------- _include/templates/back_forward.php | 4 +- _include/templates/error404.php | 4 +- _include/templates/mainpage.php | 5 +- _include/templates/service.php | 4 +- _include/templates/site.php | 4 +- _styles/zeta/templates/back_forward.php | 42 ---------- _styles/zeta/templates/blog.php | 42 ---------- _styles/zeta/templates/blog_main.php | 42 ---------- _styles/zeta/templates/error404.php | 32 -------- _styles/zeta/templates/index.html | 8 -- _styles/zeta/templates/mainpage.php | 42 ---------- _styles/zeta/templates/service.php | 34 -------- _styles/zeta/templates/site.php | 48 ----------- 18 files changed, 83 insertions(+), 361 deletions(-) delete mode 100644 _styles/zeta/templates/back_forward.php delete mode 100644 _styles/zeta/templates/blog.php delete mode 100644 _styles/zeta/templates/blog_main.php delete mode 100644 _styles/zeta/templates/error404.php delete mode 100644 _styles/zeta/templates/index.html delete mode 100644 _styles/zeta/templates/mainpage.php delete mode 100644 _styles/zeta/templates/service.php delete mode 100644 _styles/zeta/templates/site.php diff --git a/_extensions/s2_blog/Controller/BlogController.php b/_extensions/s2_blog/Controller/BlogController.php index a634941d..41e96f35 100644 --- a/_extensions/s2_blog/Controller/BlogController.php +++ b/_extensions/s2_blog/Controller/BlogController.php @@ -49,7 +49,7 @@ public function __construct( public function handle(Request $request): Response { - $template = $this->templateProvider->getTemplate($this->template_id); + $template = $this->templateProvider->getTemplate($this->template_id, 's2_blog'); $template ->putInPlaceholder('commented', 0) diff --git a/_extensions/s2_blog/templates/blog.php b/_extensions/s2_blog/templates/blog.php index 0643afc1..dcb0126e 100644 --- a/_extensions/s2_blog/templates/blog.php +++ b/_extensions/s2_blog/templates/blog.php @@ -1,4 +1,4 @@ - + @@ -39,4 +39,4 @@ - \ No newline at end of file + diff --git a/_extensions/s2_blog/templates/blog_main.php b/_extensions/s2_blog/templates/blog_main.php index 6ef19822..f2c8ceae 100644 --- a/_extensions/s2_blog/templates/blog_main.php +++ b/_extensions/s2_blog/templates/blog_main.php @@ -1,4 +1,4 @@ - + @@ -39,4 +39,4 @@ - \ No newline at end of file + diff --git a/_include/src/Template/HtmlTemplateProvider.php b/_include/src/Template/HtmlTemplateProvider.php index b34cb771..657aee6e 100644 --- a/_include/src/Template/HtmlTemplateProvider.php +++ b/_include/src/Template/HtmlTemplateProvider.php @@ -36,9 +36,9 @@ public function __construct( $this->styleName = $this->dynamicConfigProvider->get('S2_STYLE'); } - public function getTemplate(string $templateId): HtmlTemplate + public function getTemplate(string $templateId, ?string $extraDir = null): HtmlTemplate { - $templateContent = $this->getRawTemplateContent($templateId); + $templateContent = $this->getRawTemplateContent($templateId, $extraDir); $templateContent = $this->replaceCurrentLinks($templateContent); $htmlTemplate = new HtmlTemplate( @@ -59,25 +59,16 @@ public function getTemplate(string $templateId): HtmlTemplate /** * Searches for a template file (in the style or 'template' directory) */ - public function getRawTemplateContent(string $templateId): string + public function getRawTemplateContent(string $templateId, ?string $extraDir): string { - $defaultPath = $this->rootDir . '_include/templates/'; - $path = null; $cleanTemplateId = preg_replace('#[^0-9a-zA-Z._\-]#', '', $templateId); $buildEvent = new TemplateBuildEvent($this->styleName, $cleanTemplateId, $path); $this->dispatcher->dispatch($buildEvent, TemplateBuildEvent::EVENT_START); - if ($path === null) { // Can be modified via event - $templatePathInStyles = $this->rootDir . '_styles/' . $this->styleName . '/templates/' . $cleanTemplateId; - if (file_exists($templatePathInStyles)) { - $path = $templatePathInStyles; - } elseif (file_exists($defaultPath . $cleanTemplateId)) { - $path = $defaultPath . $cleanTemplateId; - } else { - throw new \RuntimeException(sprintf(\Lang::get('Template not found'), $defaultPath . $cleanTemplateId)); - } + if ($path === null) { // Can be not null via event + $path = $this->getTemplateFullFilename($extraDir, $cleanTemplateId); } ob_start(); @@ -143,4 +134,26 @@ static function ($matches) use ($requestUri) { return $templateContent; } + + private function getTemplateFullFilename(?string $extraDir, string $cleanTemplateId): string + { + if ($extraDir !== null) { + $templatePathInExtension = $this->rootDir . '_extensions/' . $extraDir . '/templates/' . $cleanTemplateId; + if (file_exists($templatePathInExtension)) { + return $templatePathInExtension; + } + } + + $templatePathInStyles = $this->rootDir . '_styles/' . $this->styleName . '/templates/' . $cleanTemplateId; + if (file_exists($templatePathInStyles)) { + return $templatePathInStyles; + } + + $templateDefaultPath = $this->rootDir . '_include/templates/' . $cleanTemplateId; + if (file_exists($templateDefaultPath)) { + return $templateDefaultPath; + } + + throw new \RuntimeException(sprintf(\Lang::get('Template not found'), $templateDefaultPath)); + } } diff --git a/_include/src/Template/Viewer.php b/_include/src/Template/Viewer.php index 08e7d9e3..6e1f4457 100644 --- a/_include/src/Template/Viewer.php +++ b/_include/src/Template/Viewer.php @@ -23,6 +23,47 @@ public function __construct(string $rootDir, string $style, private readonly boo $this->systemViewDir = $rootDir . '_include/views/'; } + public function render(string $name, array $vars, string ...$extraDirs): string + { + $name = preg_replace('#[^0-9a-zA-Z._\-]#', '', $name); + $filename = $name . '.php'; + + $foundFile = null; + $dirs = [ + $this->styleViewDir, + ...array_map(fn(string $dir) => sprintf($this->extensionDirPattern, $dir), $extraDirs), + $this->systemViewDir + ]; + foreach ($dirs as $dir) { + if (file_exists($dir . $filename)) { + $foundFile = $dir . $filename; + break; + } + } + + ob_start(); + + if ($this->debug) { + echo '
', + '
', $name, '
', + '
';
+            echo self::jsonFormat($vars);
+            echo '
'; + } + + if ($foundFile !== null) { + $this->includeFile($foundFile, $vars); + } elseif ($this->debug) { + echo 'View file not found in ', s2_htmlencode(var_export($dirs, true)); + } + + if ($this->debug) { + echo '
'; + } + + return ob_get_clean(); + } + /** * @throws \JsonException */ @@ -66,47 +107,6 @@ private static function jsonFormat($vars, int $level = 0): string return $str; } - public function render(string $name, array $vars, string ...$extraDirs): string - { - $name = preg_replace('#[^0-9a-zA-Z._\-]#', '', $name); - $filename = $name . '.php'; - - $foundFile = null; - $dirs = [ - $this->styleViewDir, - ...array_map(fn(string $dir) => sprintf($this->extensionDirPattern, $dir), $extraDirs), - $this->systemViewDir - ]; - foreach ($dirs as $dir) { - if (file_exists($dir . $filename)) { - $foundFile = $dir . $filename; - break; - } - } - - ob_start(); - - if ($this->debug) { - echo '
', - '
', $name, '
', - '
';
-            echo self::jsonFormat($vars);
-            echo '
'; - } - - if ($foundFile !== null) { - $this->includeFile($foundFile, $vars); - } elseif ($this->debug) { - echo 'View file not found in ', s2_htmlencode(var_export($dirs, true)); - } - - if ($this->debug) { - echo '
'; - } - - return ob_get_clean(); - } - private function includeFile(string $_found_file, array $_vars): void { extract($_vars, EXTR_OVERWRITE); diff --git a/_include/templates/back_forward.php b/_include/templates/back_forward.php index 8ac743cf..02945770 100644 --- a/_include/templates/back_forward.php +++ b/_include/templates/back_forward.php @@ -1,4 +1,4 @@ - + @@ -39,4 +39,4 @@ - \ No newline at end of file + diff --git a/_include/templates/error404.php b/_include/templates/error404.php index 47a0df8f..3a0cab58 100644 --- a/_include/templates/error404.php +++ b/_include/templates/error404.php @@ -1,4 +1,4 @@ - + @@ -29,4 +29,4 @@ - \ No newline at end of file + diff --git a/_include/templates/mainpage.php b/_include/templates/mainpage.php index 54be8300..936adbd0 100644 --- a/_include/templates/mainpage.php +++ b/_include/templates/mainpage.php @@ -1,4 +1,4 @@ - + @@ -23,7 +23,6 @@ - \ No newline at end of file + diff --git a/_include/templates/service.php b/_include/templates/service.php index ef371695..7b41db26 100644 --- a/_include/templates/service.php +++ b/_include/templates/service.php @@ -1,4 +1,4 @@ - + @@ -31,4 +31,4 @@ - \ No newline at end of file + diff --git a/_include/templates/site.php b/_include/templates/site.php index af80c47f..107e6d13 100644 --- a/_include/templates/site.php +++ b/_include/templates/site.php @@ -1,4 +1,4 @@ - + @@ -45,4 +45,4 @@ - \ No newline at end of file + diff --git a/_styles/zeta/templates/back_forward.php b/_styles/zeta/templates/back_forward.php deleted file mode 100644 index 02945770..00000000 --- a/_styles/zeta/templates/back_forward.php +++ /dev/null @@ -1,42 +0,0 @@ - - - - - -<!-- s2_head_title --> - - - - - - - -
- - - -
-
-
- - - - - - - - - -
-
-
- -
- - - - - diff --git a/_styles/zeta/templates/blog.php b/_styles/zeta/templates/blog.php deleted file mode 100644 index dcb0126e..00000000 --- a/_styles/zeta/templates/blog.php +++ /dev/null @@ -1,42 +0,0 @@ - - - - - -<!-- s2_head_title --> - - - - - - - -
- - - -
-
-
- - - - - -
- -
-
- -
- - - - - diff --git a/_styles/zeta/templates/blog_main.php b/_styles/zeta/templates/blog_main.php deleted file mode 100644 index f2c8ceae..00000000 --- a/_styles/zeta/templates/blog_main.php +++ /dev/null @@ -1,42 +0,0 @@ - - - - - -<!-- s2_head_title --> - - - - - - - -
- - - -
-
-
- - - - -
- -
-
- -
- - - - - diff --git a/_styles/zeta/templates/error404.php b/_styles/zeta/templates/error404.php deleted file mode 100644 index 3a0cab58..00000000 --- a/_styles/zeta/templates/error404.php +++ /dev/null @@ -1,32 +0,0 @@ - - - - - -<!-- s2_head_title --> - - - - - -
- - -
-
-
- - -
-
-
- -
- - - - - diff --git a/_styles/zeta/templates/index.html b/_styles/zeta/templates/index.html deleted file mode 100644 index 2db9a3cc..00000000 --- a/_styles/zeta/templates/index.html +++ /dev/null @@ -1,8 +0,0 @@ - - -. - - -. - - \ No newline at end of file diff --git a/_styles/zeta/templates/mainpage.php b/_styles/zeta/templates/mainpage.php deleted file mode 100644 index 98ef9f43..00000000 --- a/_styles/zeta/templates/mainpage.php +++ /dev/null @@ -1,42 +0,0 @@ - - - - - -<!-- s2_head_title --> - - - - - - - -
- - - -
-
-
- - - -
- -
-
- -
- - - - - diff --git a/_styles/zeta/templates/service.php b/_styles/zeta/templates/service.php deleted file mode 100644 index 7b41db26..00000000 --- a/_styles/zeta/templates/service.php +++ /dev/null @@ -1,34 +0,0 @@ - - - - - -<!-- s2_head_title --> - - - - - - -
- - -
-
-
- - - -
-
-
- -
- - - - - diff --git a/_styles/zeta/templates/site.php b/_styles/zeta/templates/site.php deleted file mode 100644 index 107e6d13..00000000 --- a/_styles/zeta/templates/site.php +++ /dev/null @@ -1,48 +0,0 @@ - - - - - -<!-- s2_head_title --> - - - - - - - -
- - - -
-
-
- - - - - - - - -
- -
-
- -
- - - - -