X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FRender%2FFriendicaSmartyEngine.php;h=0f5ee21f27ea3fc6bdc8593b91266ad11448e65b;hb=f4e1acedfcd86c81403c14747ecfd3b11583506e;hp=1e419bcf765d720e7a6775817e1c200c30c20786;hpb=2ec3a97393959e42d450e074271d2d622d25701f;p=friendica.git
diff --git a/src/Render/FriendicaSmartyEngine.php b/src/Render/FriendicaSmartyEngine.php
index 1e419bcf76..0f5ee21f27 100644
--- a/src/Render/FriendicaSmartyEngine.php
+++ b/src/Render/FriendicaSmartyEngine.php
@@ -23,76 +23,105 @@ namespace Friendica\Render;
use Friendica\Core\Hook;
use Friendica\DI;
+use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Util\Strings;
/**
- * Smarty implementation of the Friendica template engine interface
+ * Smarty implementation of the Friendica template abstraction
*/
-class FriendicaSmartyEngine implements ITemplateEngine
+final class FriendicaSmartyEngine extends TemplateEngine
{
static $name = "smarty3";
- public function __construct()
+ const FILE_PREFIX = 'file:';
+ const STRING_PREFIX = 'string:';
+
+ /** @var FriendicaSmarty */
+ private $smarty;
+
+ /**
+ * @inheritDoc
+ */
+ public function __construct(string $theme, array $theme_info)
{
- if (!is_writable(__DIR__ . '/../../view/smarty3/')) {
- echo "ERROR: folder view/smarty3/ must be writable by webserver.";
- exit();
+ $this->theme = $theme;
+ $this->theme_info = $theme_info;
+ $this->smarty = new FriendicaSmarty($this->theme, $this->theme_info);
+
+ if (!is_writable(DI::basePath() . '/view/smarty3')) {
+ $admin_message = DI::l10n()->t('The folder view/smarty3/ must be writable by webserver.');
+ DI::logger()->critical($admin_message);
+ $message = is_site_admin() ?
+ $admin_message :
+ DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
+ throw new InternalServerErrorException($message);
}
}
- // ITemplateEngine interface
- public function replaceMacros($s, $r)
+ /**
+ * @inheritDoc
+ */
+ public function testInstall(array &$errors = null)
{
- $template = '';
- if (gettype($s) === 'string') {
- $template = $s;
- $s = new FriendicaSmarty();
- }
+ $this->smarty->testInstall($errors);
+ }
- $r['$APP'] = DI::app();
+ /**
+ * @inheritDoc
+ */
+ public function replaceMacros(string $template, array $vars)
+ {
+ if (!Strings::startsWith($template, self::FILE_PREFIX)) {
+ $template = self::STRING_PREFIX . $template;
+ }
// "middleware": inject variables into templates
$arr = [
- "template" => basename($s->filename),
- "vars" => $r
+ 'template' => basename($this->smarty->filename),
+ 'vars' => $vars
];
- Hook::callAll("template_vars", $arr);
- $r = $arr['vars'];
+ Hook::callAll('template_vars', $arr);
+ $vars = $arr['vars'];
+
+ $this->smarty->clearAllAssign();
- foreach ($r as $key => $value) {
+ foreach ($vars as $key => $value) {
if ($key[0] === '$') {
$key = substr($key, 1);
}
- $s->assign($key, $value);
+ $this->smarty->assign($key, $value);
}
- return $s->parsed($template);
+
+ return $this->smarty->fetch($template);
}
- public function getTemplateFile($file, $root = '')
+ /**
+ * @inheritDoc
+ */
+ public function getTemplateFile(string $file, string $subDir = '')
{
- $a = DI::app();
- $template = new FriendicaSmarty();
-
// Make sure $root ends with a slash /
- if ($root !== '' && substr($root, -1, 1) !== '/') {
- $root = $root . '/';
+ if ($subDir !== '' && substr($subDir, -1, 1) !== '/') {
+ $subDir = $subDir . '/';
}
- $theme = $a->getCurrentTheme();
- $filename = $template::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
+ $root = DI::basePath() . '/' . $subDir;
+
+ $filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
- if (file_exists("{$root}view/theme/$theme/$filename")) {
- $template_file = "{$root}view/theme/$theme/$filename";
- } elseif (!empty($a->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename))) {
- $template_file = sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename);
+ if (file_exists("{$root}view/theme/$this->theme/$filename")) {
+ $template_file = "{$root}view/theme/$this->theme/$filename";
+ } elseif (!empty($this->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename))) {
+ $template_file = sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename);
} elseif (file_exists("{$root}/$filename")) {
$template_file = "{$root}/$filename";
} else {
$template_file = "{$root}view/$filename";
}
- $template->filename = $template_file;
+ $this->smarty->filename = $template_file;
- return $template;
+ return self::FILE_PREFIX . $template_file;
}
}