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

View File

@ -136,30 +136,6 @@ class App
$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 $queue;
private $scheme;
@ -301,7 +277,7 @@ class App
$this->isAjax = strtolower(defaults($_SERVER, 'HTTP_X_REQUESTED_WITH', '')) == 'xmlhttprequest';
// 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'];
}
if (!empty($this->theme['stylesheet'])) {
$stylesheet = $this->theme['stylesheet'];
if (!empty(Core\Renderer::$theme['stylesheet'])) {
$stylesheet = Core\Renderer::$theme['stylesheet'];
} else {
$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
* Necessary for profiling Friendica

View File

@ -15,6 +15,31 @@ use Friendica\Render\FriendicaSmarty;
*/
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 = [
'internal' => '',
'smarty3' => '{{'
@ -39,7 +64,7 @@ class Renderer extends BaseObject
// pass $baseurl to all templates
$r['$baseurl'] = System::baseUrl();
$t = $a->getTemplateEngine();
$t = self::getTemplateEngine();
try {
$output = $t->replaceMacros($s, $r);
@ -65,7 +90,7 @@ class Renderer extends BaseObject
{
$stamp1 = microtime(true);
$a = self::getApp();
$t = $a->getTemplateEngine();
$t = self::getTemplateEngine();
try {
$template = $t->getTemplateFile($s, $root);
@ -79,6 +104,72 @@ class Renderer extends BaseObject
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
*

View File

@ -164,7 +164,7 @@ class Profile
* 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';
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
// 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::$currentWizardStep = defaults($_POST, 'pass', self::SYSTEM_CHECK);

View File

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

View File

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

View File

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

View File

@ -26,7 +26,7 @@ function vier_init(App $a)
{
$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()) {
vier_community_info();