From: Adam Magness Date: Wed, 31 Oct 2018 17:25:38 +0000 (-0400) Subject: Template Engine and props to Renderer X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=70f01d6c00a44979e024f0a8bdd41c70b48a2d20;p=friendica.git Template Engine and props to Renderer move methods and props from App to Renderer --- diff --git a/mod/admin.php b/mod/admin.php index ff37c8b611..5604cd5aa3 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -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; } diff --git a/src/App.php b/src/App.php index deb15f702c..3cfcc67186 100644 --- a/src/App.php +++ b/src/App.php @@ -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 $class 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 $template_engine 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 diff --git a/src/Core/Renderer.php b/src/Core/Renderer.php index 378652fe26..44b56fcba3 100644 --- a/src/Core/Renderer.php +++ b/src/Core/Renderer.php @@ -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 $class 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 $template_engine 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 * diff --git a/src/Model/Profile.php b/src/Model/Profile.php index a98cb30d87..2cd3f7a86c 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -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)) { diff --git a/src/Module/Install.php b/src/Module/Install.php index b6b7027cb1..09de23c080 100644 --- a/src/Module/Install.php +++ b/src/Module/Install.php @@ -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); diff --git a/view/theme/duepuntozero/theme.php b/view/theme/duepuntozero/theme.php index 588a6fa463..015e8090fc 100644 --- a/view/theme/duepuntozero/theme.php +++ b/view/theme/duepuntozero/theme.php @@ -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) diff --git a/view/theme/frio/theme.php b/view/theme/frio/theme.php index 8cb4c34295..c7d38baeb9 100644 --- a/view/theme/frio/theme.php +++ b/view/theme/frio/theme.php @@ -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(); diff --git a/view/theme/smoothly/theme.php b/view/theme/smoothly/theme.php index 727650ef23..6168932e95 100644 --- a/view/theme/smoothly/theme.php +++ b/view/theme/smoothly/theme.php @@ -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; diff --git a/view/theme/vier/theme.php b/view/theme/vier/theme.php index 457816b24e..fb4f66431b 100644 --- a/view/theme/vier/theme.php +++ b/view/theme/vier/theme.php @@ -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();