X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FRender%2FFriendicaSmartyEngine.php;h=6fc8f4480676eb892583c7f113968ff2bd4fc243;hb=80a533ddab9ee4ec7b715ff2dc92e1e464ccd85d;hp=2cfce760adc2cba53c6e62950638f61097e5208c;hpb=e805350ce465b9ff311698767f4bfab4a0080916;p=friendica.git diff --git a/src/Render/FriendicaSmartyEngine.php b/src/Render/FriendicaSmartyEngine.php index 2cfce760ad..6fc8f44806 100644 --- a/src/Render/FriendicaSmartyEngine.php +++ b/src/Render/FriendicaSmartyEngine.php @@ -1,59 +1,127 @@ -ERROR: folder view/smarty3/ must be writable by webserver."; - killme(); - } - } - - // ITemplateEngine interface - public function replaceMacros($s, $r) - { - $template = ''; - if (gettype($s) === 'string') { - $template = $s; - $s = new FriendicaSmarty(); - } - - $r['$APP'] = get_app(); - - // "middleware": inject variables into templates - $arr = [ - "template" => basename($s->filename), - "vars" => $r - ]; - Addon::callHooks("template_vars", $arr); - $r = $arr['vars']; - - foreach ($r as $key => $value) { - if ($key[0] === '$') { - $key = substr($key, 1); - } - - $s->assign($key, $value); - } - return $s->parsed($template); - } - - public function getTemplateFile($file, $root = '') - { - $a = get_app(); - $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER . '/' . $file, $root); - $template = new FriendicaSmarty(); - $template->filename = $template_file; - - return $template; - } -} +. + * + */ + +namespace Friendica\Render; + +use Friendica\Core\Hook; +use Friendica\DI; +use Friendica\Network\HTTPException\ServiceUnavailableException; +use Friendica\Util\Strings; + +/** + * Smarty implementation of the Friendica template abstraction + */ +final class FriendicaSmartyEngine extends TemplateEngine +{ + static $name = "smarty3"; + + const FILE_PREFIX = 'file:'; + const STRING_PREFIX = 'string:'; + + /** @var FriendicaSmarty */ + private $smarty; + + /** + * @inheritDoc + */ + public function __construct(string $theme, array $theme_info) + { + $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 = 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); + } + } + + /** + * @inheritDoc + */ + public function testInstall(array &$errors = null) + { + $this->smarty->testInstall($errors); + } + + /** + * @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($this->smarty->filename), + 'vars' => $vars + ]; + Hook::callAll('template_vars', $arr); + $vars = $arr['vars']; + + $this->smarty->clearAllAssign(); + + foreach ($vars as $key => $value) { + if ($key[0] === '$') { + $key = substr($key, 1); + } + + $this->smarty->assign($key, $value); + } + + return $this->smarty->fetch($template); + } + + /** + * @inheritDoc + */ + public function getTemplateFile(string $file, string $subDir = '') + { + // 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 self::FILE_PREFIX . $template_file; + } +}