]> git.mxchange.org Git - friendica.git/blob - src/Core/Renderer.php
Merge pull request #8629 from annando/item-insert
[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\FriendicaSmarty;
27 use Friendica\Render\ITemplateEngine;
28
29 /**
30  * This class handles Renderer related functions.
31  */
32 class Renderer
33 {
34         /**
35          * An array of registered template engines ('name'=>'class name')
36          */
37         public static $template_engines = [];
38
39         /**
40          * An array of instanced template engines ('name'=>'instance')
41          */
42         public static $template_engine_instance = [];
43
44         /**
45          * An array for all theme-controllable parameters
46          *
47          * Mostly unimplemented yet. Only options 'template_engine' and
48          * beyond are used.
49          */
50         public static $theme = [
51                 'sourcename' => '',
52                 'videowidth' => 425,
53                 'videoheight' => 350,
54                 'force_max_items' => 0,
55                 'stylesheet' => '',
56                 'template_engine' => 'smarty3',
57         ];
58
59         private static $ldelim = [
60                 'internal' => '',
61                 'smarty3' => '{{'
62         ];
63         private static $rdelim = [
64                 'internal' => '',
65                 'smarty3' => '}}'
66         ];
67
68         /**
69          * This is our template processor
70          *
71          * @param string|FriendicaSmarty $s    The string requiring macro substitution or an instance of FriendicaSmarty
72          * @param array                  $vars Key value pairs (search => replace)
73          *
74          * @return string substituted string
75          * @throws Exception
76          */
77         public static function replaceMacros($s, array $vars = [])
78         {
79                 $stamp1 = microtime(true);
80
81                 // pass $baseurl to all templates if it isn't set
82                 $vars = array_merge(['$baseurl' => DI::baseUrl()->get()], $vars);
83
84                 $t = self::getTemplateEngine();
85
86                 try {
87                         $output = $t->replaceMacros($s, $vars);
88                 } catch (Exception $e) {
89                         echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
90                         exit();
91                 }
92
93                 DI::profiler()->saveTimestamp($stamp1, "rendering", System::callstack());
94
95                 return $output;
96         }
97
98         /**
99          * Load a given template $s
100          *
101          * @param string $s    Template to load.
102          * @param string $subDir Subdirectory (Optional)
103          *
104          * @return string template.
105          * @throws Exception
106          */
107         public static function getMarkupTemplate($s, $subDir = '')
108         {
109                 $stamp1 = microtime(true);
110                 $t = self::getTemplateEngine();
111
112                 try {
113                         $template = $t->getTemplateFile($s, $subDir);
114                 } catch (Exception $e) {
115                         echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
116                         exit();
117                 }
118
119                 DI::profiler()->saveTimestamp($stamp1, "file", System::callstack());
120
121                 return $template;
122         }
123
124         /**
125          * Register template engine class
126          *
127          * @param string $class
128          */
129         public static function registerTemplateEngine($class)
130         {
131                 $v = get_class_vars($class);
132
133                 if (!empty($v['name']))
134                 {
135                         $name = $v['name'];
136                         self::$template_engines[$name] = $class;
137                 } else {
138                         echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
139                         die();
140                 }
141         }
142
143         /**
144          * Return template engine instance.
145          *
146          * If $name is not defined, return engine defined by theme,
147          * or default
148          *
149          * @return ITemplateEngine Template Engine instance
150          */
151         public static function getTemplateEngine()
152         {
153                 $template_engine = (self::$theme['template_engine'] ?? '') ?: 'smarty3';
154
155                 if (isset(self::$template_engines[$template_engine])) {
156                         if (isset(self::$template_engine_instance[$template_engine])) {
157                                 return self::$template_engine_instance[$template_engine];
158                         } else {
159                                 $class = self::$template_engines[$template_engine];
160                                 $obj = new $class;
161                                 self::$template_engine_instance[$template_engine] = $obj;
162                                 return $obj;
163                         }
164                 }
165
166                 echo "template engine <tt>$template_engine</tt> is not registered!\n";
167                 exit();
168         }
169
170         /**
171          * Returns the active template engine.
172          *
173          * @return string the active template engine
174          */
175         public static function getActiveTemplateEngine()
176         {
177                 return self::$theme['template_engine'];
178         }
179
180         /**
181          * sets the active template engine
182          *
183          * @param string $engine the template engine (default is Smarty3)
184          */
185         public static function setActiveTemplateEngine($engine = 'smarty3')
186         {
187                 self::$theme['template_engine'] = $engine;
188         }
189
190         /**
191          * Gets the right delimiter for a template engine
192          *
193          * Currently:
194          * Internal = ''
195          * Smarty3 = '{{'
196          *
197          * @param string $engine The template engine (default is Smarty3)
198          *
199          * @return string the right delimiter
200          */
201         public static function getTemplateLeftDelimiter($engine = 'smarty3')
202         {
203                 return self::$ldelim[$engine];
204         }
205
206         /**
207          * Gets the left delimiter for a template engine
208          *
209          * Currently:
210          * Internal = ''
211          * Smarty3 = '}}'
212          *
213          * @param string $engine The template engine (default is Smarty3)
214          *
215          * @return string the left delimiter
216          */
217         public static function getTemplateRightDelimiter($engine = 'smarty3')
218         {
219                 return self::$rdelim[$engine];
220         }
221 }