X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FRender%2FFriendicaSmartyEngine.php;h=5d7ed6c0c94dbb353e01f07c8d16925c5543421c;hb=46cd39fb34613f4b331793f19c0e562f93125066;hp=6fd51cf5d1a9e307920e980dc3b9cc11c1b11d19;hpb=6500c32362a3342fca59c2b54cba13961987c5ff;p=friendica.git
diff --git a/src/Render/FriendicaSmartyEngine.php b/src/Render/FriendicaSmartyEngine.php
index 6fd51cf5d1..5d7ed6c0c9 100644
--- a/src/Render/FriendicaSmartyEngine.php
+++ b/src/Render/FriendicaSmartyEngine.php
@@ -1,61 +1,131 @@
.
+ *
*/
-namespace Friendica\Render;
-use Friendica\Core\Addon;
+namespace Friendica\Render;
-define('SMARTY3_TEMPLATE_FOLDER', 'templates');
+use Friendica\Core\Hook;
+use Friendica\DI;
+use Friendica\Network\HTTPException\ServiceUnavailableException;
+use Friendica\Util\Strings;
-class FriendicaSmartyEngine implements ITemplateEngine
+/**
+ * Smarty implementation of the Friendica template abstraction
+ */
+final class FriendicaSmartyEngine extends TemplateEngine
{
- static $name = "smarty3";
+ static $name = 'smarty3';
+
+ const FILE_PREFIX = 'file:';
+ const STRING_PREFIX = 'string:';
+
+ /** @var FriendicaSmarty */
+ private $smarty;
- public function __construct()
+ /**
+ * @inheritDoc
+ */
+ public function __construct(string $theme, array $theme_info)
{
- if (!is_writable('view/smarty3/')) {
- echo "ERROR: folder view/smarty3/ must be writable by webserver.";
- killme();
+ $this->theme = $theme;
+ $this->theme_info = $theme_info;
+
+ $work_dir = DI::config()->get('smarty3', 'config_dir');
+ $use_sub_dirs = DI::config()->get('smarty3', 'use_sub_dirs');
+
+ $this->smarty = new FriendicaSmarty($this->theme, $this->theme_info, $work_dir, $use_sub_dirs);
+
+ if (!is_writable($work_dir)) {
+ $admin_message = DI::l10n()->t('The folder %s must be writable by webserver.', $work_dir);
+ DI::logger()->critical($admin_message);
+ $message = DI::app()->isSiteAdmin() ?
+ $admin_message :
+ DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
+ throw new ServiceUnavailableException($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'] = get_app();
+ /**
+ * @inheritDoc
+ */
+ public function replaceMacros(string $template, array $vars): string
+ {
+ 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
];
- Addon::callHooks("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 = get_app();
- $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER . '/' . $file, $root);
- $template = new FriendicaSmarty();
- $template->filename = $template_file;
+ // Make sure $root ends with a slash /
+ if ($subDir !== '' && substr($subDir, -1, 1) !== '/') {
+ $subDir = $subDir . '/';
+ }
+
+ $root = DI::basePath() . '/' . $subDir;
+
+ $filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
+
+ 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";
+ }
+
+ $this->smarty->filename = $template_file;
- return $template;
+ return self::FILE_PREFIX . $template_file;
}
}