Template Engine and props to Renderer

move methods and props from App to Renderer
This commit is contained in:
Adam Magness 2018-10-31 13:25:38 -04:00
parent 3f74ba88c2
commit 70f01d6c00
9 changed files with 108 additions and 103 deletions

View File

@ -91,7 +91,7 @@ function admin_post(App $a)
$theme = $a->argv[2]; $theme = $a->argv[2];
if (is_file("view/theme/$theme/config.php")) { if (is_file("view/theme/$theme/config.php")) {
$orig_theme = $a->theme; $orig_theme = Renderer::$theme;
$orig_page = $a->page; $orig_page = $a->page;
$orig_session_theme = $_SESSION['theme']; $orig_session_theme = $_SESSION['theme'];
require_once "view/theme/$theme/theme.php"; require_once "view/theme/$theme/theme.php";
@ -107,7 +107,7 @@ function admin_post(App $a)
} }
$_SESSION['theme'] = $orig_session_theme; $_SESSION['theme'] = $orig_session_theme;
$a->theme = $orig_theme; Renderer::$theme = $orig_theme;
$a->page = $orig_page; $a->page = $orig_page;
} }
@ -2271,7 +2271,7 @@ function admin_page_themes(App $a)
$admin_form = ''; $admin_form = '';
if (is_file("view/theme/$theme/config.php")) { if (is_file("view/theme/$theme/config.php")) {
$orig_theme = $a->theme; $orig_theme = Renderer::$theme;
$orig_page = $a->page; $orig_page = $a->page;
$orig_session_theme = $_SESSION['theme']; $orig_session_theme = $_SESSION['theme'];
require_once "view/theme/$theme/theme.php"; require_once "view/theme/$theme/theme.php";
@ -2288,7 +2288,7 @@ function admin_page_themes(App $a)
} }
$_SESSION['theme'] = $orig_session_theme; $_SESSION['theme'] = $orig_session_theme;
$a->theme = $orig_theme; Renderer::$theme = $orig_theme;
$a->page = $orig_page; $a->page = $orig_page;
} }

View File

@ -136,30 +136,6 @@ class App
$this->footerScripts[] = trim($url, '/'); $this->footerScripts[] = trim($url, '/');
} }
/**
* @brief An array for all theme-controllable parameters
*
* Mostly unimplemented yet. Only options 'template_engine' and
* beyond are used.
*/
public $theme = [
'sourcename' => '',
'videowidth' => 425,
'videoheight' => 350,
'force_max_items' => 0,
'stylesheet' => '',
'template_engine' => 'smarty3',
];
/**
* @brief An array of registered template engines ('name'=>'class name')
*/
public $template_engines = [];
/**
* @brief An array of instanced template engines ('name'=>'instance')
*/
public $template_engine_instance = [];
public $process_id; public $process_id;
public $queue; public $queue;
private $scheme; private $scheme;
@ -301,7 +277,7 @@ class App
$this->isAjax = strtolower(defaults($_SERVER, 'HTTP_X_REQUESTED_WITH', '')) == 'xmlhttprequest'; $this->isAjax = strtolower(defaults($_SERVER, 'HTTP_X_REQUESTED_WITH', '')) == 'xmlhttprequest';
// Register template engines // Register template engines
$this->registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine'); Core\Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');
} }
/** /**
@ -744,8 +720,8 @@ class App
$this->page['title'] = $this->config['sitename']; $this->page['title'] = $this->config['sitename'];
} }
if (!empty($this->theme['stylesheet'])) { if (!empty(Core\Renderer::$theme['stylesheet'])) {
$stylesheet = $this->theme['stylesheet']; $stylesheet = Core\Renderer::$theme['stylesheet'];
} else { } else {
$stylesheet = $this->getCurrentThemeStylesheetPath(); $stylesheet = $this->getCurrentThemeStylesheetPath();
} }
@ -852,70 +828,6 @@ class App
} }
} }
/**
* @brief Register template engine class
*
* @param string $class
*/
private function registerTemplateEngine($class)
{
$v = get_class_vars($class);
if (!empty($v['name'])) {
$name = $v['name'];
$this->template_engines[$name] = $class;
} else {
echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
die();
}
}
/**
* @brief Return template engine instance.
*
* If $name is not defined, return engine defined by theme,
* or default
*
* @return object Template Engine instance
*/
public function getTemplateEngine()
{
$template_engine = defaults($this->theme, 'template_engine', 'smarty3');
if (isset($this->template_engines[$template_engine])) {
if (isset($this->template_engine_instance[$template_engine])) {
return $this->template_engine_instance[$template_engine];
} else {
$class = $this->template_engines[$template_engine];
$obj = new $class;
$this->template_engine_instance[$template_engine] = $obj;
return $obj;
}
}
echo "template engine <tt>$template_engine</tt> is not registered!\n";
exit();
}
/**
* @brief Returns the active template engine.
*
* @return string the active template engine
*/
public function getActiveTemplateEngine()
{
return $this->theme['template_engine'];
}
/**
* sets the active template engine
*
* @param string $engine the template engine (default is Smarty3)
*/
public function setActiveTemplateEngine($engine = 'smarty3')
{
$this->theme['template_engine'] = $engine;
}
/** /**
* Saves a timestamp for a value - f.e. a call * Saves a timestamp for a value - f.e. a call
* Necessary for profiling Friendica * Necessary for profiling Friendica

View File

@ -15,6 +15,31 @@ use Friendica\Render\FriendicaSmarty;
*/ */
class Renderer extends BaseObject class Renderer extends BaseObject
{ {
/**
* @brief An array of registered template engines ('name'=>'class name')
*/
public static $template_engines = [];
/**
* @brief An array of instanced template engines ('name'=>'instance')
*/
public static $template_engine_instance = [];
/**
* @brief An array for all theme-controllable parameters
*
* Mostly unimplemented yet. Only options 'template_engine' and
* beyond are used.
*/
public static $theme = [
'sourcename' => '',
'videowidth' => 425,
'videoheight' => 350,
'force_max_items' => 0,
'stylesheet' => '',
'template_engine' => 'smarty3',
];
private static $ldelim = [ private static $ldelim = [
'internal' => '', 'internal' => '',
'smarty3' => '{{' 'smarty3' => '{{'
@ -39,7 +64,7 @@ class Renderer extends BaseObject
// pass $baseurl to all templates // pass $baseurl to all templates
$r['$baseurl'] = System::baseUrl(); $r['$baseurl'] = System::baseUrl();
$t = $a->getTemplateEngine(); $t = self::getTemplateEngine();
try { try {
$output = $t->replaceMacros($s, $r); $output = $t->replaceMacros($s, $r);
@ -65,7 +90,7 @@ class Renderer extends BaseObject
{ {
$stamp1 = microtime(true); $stamp1 = microtime(true);
$a = self::getApp(); $a = self::getApp();
$t = $a->getTemplateEngine(); $t = self::getTemplateEngine();
try { try {
$template = $t->getTemplateFile($s, $root); $template = $t->getTemplateFile($s, $root);
@ -79,6 +104,72 @@ class Renderer extends BaseObject
return $template; return $template;
} }
/**
* @brief Register template engine class
*
* @param string $class
*/
public static function registerTemplateEngine($class)
{
$v = get_class_vars($class);
if (!empty($v['name']))
{
$name = $v['name'];
self::$template_engines[$name] = $class;
} else {
echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
die();
}
}
/**
* @brief Return template engine instance.
*
* If $name is not defined, return engine defined by theme,
* or default
*
* @return object Template Engine instance
*/
public static function getTemplateEngine()
{
$template_engine = defaults(self::$theme, 'template_engine', 'smarty3');
if (isset(self::$template_engines[$template_engine])) {
if (isset(self::$template_engine_instance[$template_engine])) {
return self::$template_engine_instance[$template_engine];
} else {
$class = self::$template_engines[$template_engine];
$obj = new $class;
self::$template_engine_instance[$template_engine] = $obj;
return $obj;
}
}
echo "template engine <tt>$template_engine</tt> is not registered!\n";
exit();
}
/**
* @brief Returns the active template engine.
*
* @return string the active template engine
*/
public static function getActiveTemplateEngine()
{
return self::$theme['template_engine'];
}
/**
* sets the active template engine
*
* @param string $engine the template engine (default is Smarty3)
*/
public static function setActiveTemplateEngine($engine = 'smarty3')
{
self::$theme['template_engine'] = $engine;
}
/** /**
* Gets the right delimiter for a template engine * Gets the right delimiter for a template engine
* *

View File

@ -164,7 +164,7 @@ class Profile
* load/reload current theme info * load/reload current theme info
*/ */
$a->setActiveTemplateEngine(); // reset the template engine to the default in case the user's theme doesn't specify one Renderer::setActiveTemplateEngine(); // reset the template engine to the default in case the user's theme doesn't specify one
$theme_info_file = 'view/theme/' . $a->getCurrentTheme() . '/theme.php'; $theme_info_file = 'view/theme/' . $a->getCurrentTheme() . '/theme.php';
if (file_exists($theme_info_file)) { if (file_exists($theme_info_file)) {

View File

@ -53,7 +53,7 @@ class Install extends BaseModule
// We overwrite current theme css, because during install we may not have a working mod_rewrite // We overwrite current theme css, because during install we may not have a working mod_rewrite
// so we may not have a css at all. Here we set a static css file for the install procedure pages // so we may not have a css at all. Here we set a static css file for the install procedure pages
$a->theme['stylesheet'] = $a->getBaseURL() . '/view/install/style.css'; Renderer::$theme['stylesheet'] = $a->getBaseURL() . '/view/install/style.css';
self::$installer = new Core\Installer(); self::$installer = new Core\Installer();
self::$currentWizardStep = defaults($_POST, 'pass', self::SYSTEM_CHECK); self::$currentWizardStep = defaults($_POST, 'pass', self::SYSTEM_CHECK);

View File

@ -3,10 +3,11 @@
use Friendica\App; use Friendica\App;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Friendica\Core\Renderer;
function duepuntozero_init(App $a) { function duepuntozero_init(App $a) {
$a->setActiveTemplateEngine('smarty3'); Renderer::setActiveTemplateEngine('smarty3');
$colorset = PConfig::get( local_user(), 'duepuntozero','colorset'); $colorset = PConfig::get( local_user(), 'duepuntozero','colorset');
if (!$colorset) if (!$colorset)

View File

@ -15,6 +15,7 @@ use Friendica\Core\Config;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\PConfig; use Friendica\Core\PConfig;
use Friendica\Core\Renderer;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model; use Friendica\Model;
@ -30,7 +31,7 @@ function frio_init(App $a)
$a->theme_events_in_profile = false; $a->theme_events_in_profile = false;
$a->videowidth = 622; $a->videowidth = 622;
$a->setActiveTemplateEngine('smarty3'); Renderer::setActiveTemplateEngine('smarty3');
$baseurl = System::baseUrl(); $baseurl = System::baseUrl();

View File

@ -15,7 +15,7 @@ use Friendica\Core\Renderer;
use Friendica\Core\System; use Friendica\Core\System;
function smoothly_init(App $a) { function smoothly_init(App $a) {
$a->setActiveTemplateEngine('smarty3'); Renderer::setActiveTemplateEngine('smarty3');
$cssFile = null; $cssFile = null;
$ssl_state = null; $ssl_state = null;

View File

@ -26,7 +26,7 @@ function vier_init(App $a)
{ {
$a->theme_events_in_profile = false; $a->theme_events_in_profile = false;
$a->setActiveTemplateEngine('smarty3'); Renderer::setActiveTemplateEngine('smarty3');
if (!empty($a->argv[0]) && $a->argv[0] . defaults($a->argv, 1, '') === "profile".$a->user['nickname'] || $a->argv[0] === "network" && local_user()) { if (!empty($a->argv[0]) && $a->argv[0] . defaults($a->argv, 1, '') === "profile".$a->user['nickname'] || $a->argv[0] === "network" && local_user()) {
vier_community_info(); vier_community_info();