Skip to content

Commit

Permalink
Removed templates from Zeta style (seems like there is no more need t…
Browse files Browse the repository at this point in the history
…o keep duplicates).
  • Loading branch information
parpalak committed Mar 30, 2024
1 parent 036b09c commit 1d6b107
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 361 deletions.
2 changes: 1 addition & 1 deletion _extensions/s2_blog/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions _extensions/s2_blog/templates/blog.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php if (!defined('S2_ROOT')) die; ?>
<?php if (!isset($this)) die; ?>
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -39,4 +39,4 @@
</div>
<!-- s2_scripts -->
</body>
</html>
</html>
4 changes: 2 additions & 2 deletions _extensions/s2_blog/templates/blog_main.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php if (!defined('S2_ROOT')) die; ?>
<?php if (!isset($this)) die; ?>
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -39,4 +39,4 @@
</div>
<!-- s2_scripts -->
</body>
</html>
</html>
41 changes: 27 additions & 14 deletions _include/src/Template/HtmlTemplateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
Expand Down Expand Up @@ -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));
}
}
82 changes: 41 additions & 41 deletions _include/src/Template/Viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<div style="border: 1px solid rgba(0, 0, 0, 0.15); margin: 1px; position: relative;">',
'<pre style="opacity: 0.4; background: darkgray; color: white; position: absolute; z-index: 10000; right: 0; cursor: pointer; text-decoration: underline; padding: 0.1em 0.65em;" onclick="this.nextSibling.style.display = this.nextSibling.style.display === \'block\' ? \'none\' : \'block\'; ">', $name, '</pre>',
'<pre style="display: none; font-size: 12px; line-height: 1.3; color: #9e9; background: #003;">';
echo self::jsonFormat($vars);
echo '</pre>';
}

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 '</div>';
}

return ob_get_clean();
}

/**
* @throws \JsonException
*/
Expand Down Expand Up @@ -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 '<div style="border: 1px solid rgba(0, 0, 0, 0.15); margin: 1px; position: relative;">',
'<pre style="opacity: 0.4; background: darkgray; color: white; position: absolute; z-index: 10000; right: 0; cursor: pointer; text-decoration: underline; padding: 0.1em 0.65em;" onclick="this.nextSibling.style.display = this.nextSibling.style.display === \'block\' ? \'none\' : \'block\'; ">', $name, '</pre>',
'<pre style="display: none; font-size: 12px; line-height: 1.3; color: #9e9; background: #003;">';
echo self::jsonFormat($vars);
echo '</pre>';
}

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 '</div>';
}

return ob_get_clean();
}

private function includeFile(string $_found_file, array $_vars): void
{
extract($_vars, EXTR_OVERWRITE);
Expand Down
4 changes: 2 additions & 2 deletions _include/templates/back_forward.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php if (!defined('S2_ROOT')) die; ?>
<?php if (!isset($this)) die; ?>
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -39,4 +39,4 @@
</div>
<!-- s2_scripts -->
</body>
</html>
</html>
4 changes: 2 additions & 2 deletions _include/templates/error404.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php if (!defined('S2_ROOT')) die; ?>
<?php if (!isset($this)) die; ?>
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -29,4 +29,4 @@
</div>
<!-- s2_scripts -->
</body>
</html>
</html>
5 changes: 2 additions & 3 deletions _include/templates/mainpage.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php if (!defined('S2_ROOT')) die; ?>
<?php if (!isset($this)) die; ?>
<!DOCTYPE html>
<html>
<head>
Expand All @@ -23,7 +23,6 @@
<!-- s2_text -->
</div>
<div id="menu">
<!-- s2_menu_siblings -->
<!-- s2_menu_children -->
<!-- s2_menu_subsections -->
<!-- s2_last_comments -->
Expand All @@ -40,4 +39,4 @@
</div>
<!-- s2_scripts -->
</body>
</html>
</html>
4 changes: 2 additions & 2 deletions _include/templates/service.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php if (!defined('S2_ROOT')) die; ?>
<?php if (!isset($this)) die; ?>
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -31,4 +31,4 @@
</div>
<!-- s2_scripts -->
</body>
</html>
</html>
4 changes: 2 additions & 2 deletions _include/templates/site.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php if (!defined('S2_ROOT')) die; ?>
<?php if (!isset($this)) die; ?>
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -45,4 +45,4 @@
</div>
<!-- s2_scripts -->
</body>
</html>
</html>
42 changes: 0 additions & 42 deletions _styles/zeta/templates/back_forward.php

This file was deleted.

42 changes: 0 additions & 42 deletions _styles/zeta/templates/blog.php

This file was deleted.

Loading

0 comments on commit 1d6b107

Please sign in to comment.