]> git.mxchange.org Git - friendica.git/blob - src/Core/Renderer.php
4dab3184c77eb956c8e20830b25b85aad56746d6
[friendica.git] / src / Core / Renderer.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core;
23
24 use Exception;
25 use Friendica\DI;
26 use Friendica\Render\TemplateEngine;
27
28 /**
29  * This class handles Renderer related functions.
30  */
31 class Renderer
32 {
33         /**
34          * An array of registered template engines ('name'=>'class name')
35          */
36         public static $template_engines = [];
37
38         /**
39          * An array of instanced template engines ('name'=>'instance')
40          */
41         public static $template_engine_instance = [];
42
43         /**
44          * An array for all theme-controllable parameters
45          *
46          * Mostly unimplemented yet. Only options 'template_engine' and
47          * beyond are used.
48          */
49         public static $theme = [
50                 'sourcename' => '',
51                 'videowidth' => 425,
52                 'videoheight' => 350,
53                 'force_max_items' => 0,
54                 'stylesheet' => '',
55                 'template_engine' => 'smarty3',
56         ];
57
58         private static $ldelim = [
59                 'internal' => '',
60                 'smarty3' => '{{'
61         ];
62         private static $rdelim = [
63                 'internal' => '',
64                 'smarty3' => '}}'
65         ];
66
67         /**
68          * Returns the rendered template output from the template string and variables
69          *
70          * @param string $template
71          * @param array  $vars
72          * @return string
73          */
74         public static function replaceMacros(string $template, array $vars)
75         {
76                 $stamp1 = microtime(true);
77
78                 // pass $baseurl to all templates if it isn't set
79                 $vars = array_merge(['$baseurl' => DI::baseUrl()->get(), '$APP' => DI::app()], $vars);
80
81                 $t = self::getTemplateEngine();
82
83                 try {
84                         $output = $t->replaceMacros($template, $vars);
85                 } catch (Exception $e) {
86                         echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
87                         exit();
88                 }
89
90                 DI::profiler()->saveTimestamp($stamp1, "rendering", System::callstack());
91
92                 return $output;
93         }
94
95         /**
96          * Load a given template $s
97          *
98          * @param string $file   Template to load.
99          * @param string $subDir Subdirectory (Optional)
100          *
101          * @return string template.
102          * @throws Exception
103          */
104         public static function getMarkupTemplate($file, $subDir = '')
105         {
106                 $stamp1 = microtime(true);
107                 $t = self::getTemplateEngine();
108
109                 try {
110                         $template = $t->getTemplateFile($file, $subDir);
111                 } catch (Exception $e) {
112                         echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
113                         exit();
114                 }
115
116                 DI::profiler()->saveTimestamp($stamp1, "file", System::callstack());
117
118                 return $template;
119         }
120
121         /**
122          * Register template engine class
123          *
124          * @param string $class
125          */
126         public static function registerTemplateEngine($class)
127         {
128                 $v = get_class_vars($class);
129
130                 if (!empty($v['name'])) {
131                         $name = $v['name'];
132                         self::$template_engines[$name] = $class;
133                 } else {
134                         echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
135                         die();
136                 }
137         }
138
139         /**
140          * Return template engine instance.
141          *
142          * If $name is not defined, return engine defined by theme,
143          * or default
144          *
145          * @return TemplateEngine Template Engine instance
146          */
147         public static function getTemplateEngine()
148         {
149                 $template_engine = (self::$theme['template_engine'] ?? '') ?: 'smarty3';
150
151                 if (isset(self::$template_engines[$template_engine])) {
152                         if (isset(self::$template_engine_instance[$template_engine])) {
153                                 return self::$template_engine_instance[$template_engine];
154                         } else {
155                                 $a = DI::app();
156                                 $class = self::$template_engines[$template_engine];
157                                 $obj = new $class($a->getCurrentTheme(), $a->theme_info);
158                                 self::$template_engine_instance[$template_engine] = $obj;
159                                 return $obj;
160                         }
161                 }
162
163                 echo "template engine <tt>$template_engine</tt> is not registered!\n";
164                 exit();
165         }
166
167         /**
168          * Returns the active template engine.
169          *
170          * @return string the active template engine
171          */
172         public static function getActiveTemplateEngine()
173         {
174                 return self::$theme['template_engine'];
175         }
176
177         /**
178          * sets the active template engine
179          *
180          * @param string $engine the template engine (default is Smarty3)
181          */
182         public static function setActiveTemplateEngine($engine = 'smarty3')
183         {
184                 self::$theme['template_engine'] = $engine;
185         }
186
187         /**
188          * Gets the right delimiter for a template engine
189          *
190          * Currently:
191          * Internal = ''
192          * Smarty3 = '{{'
193          *
194          * @param string $engine The template engine (default is Smarty3)
195          *
196          * @return string the right delimiter
197          */
198         public static function getTemplateLeftDelimiter($engine = 'smarty3')
199         {
200                 return self::$ldelim[$engine];
201         }
202
203         /**
204          * Gets the left delimiter for a template engine
205          *
206          * Currently:
207          * Internal = ''
208          * Smarty3 = '}}'
209          *
210          * @param string $engine The template engine (default is Smarty3)
211          *
212          * @return string the left delimiter
213          */
214         public static function getTemplateRightDelimiter($engine = 'smarty3')
215         {
216                 return self::$rdelim[$engine];
217         }
218 }