]> git.mxchange.org Git - friendica.git/blob - src/Core/Renderer.php
Merge pull request #11506 from annando/featured-worker
[friendica.git] / src / Core / Renderer.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\Network\HTTPException\ServiceUnavailableException;
27 use Friendica\Render\TemplateEngine;
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                 'videowidth' => 425,
52                 'videoheight' => 350,
53                 'stylesheet' => '',
54                 'template_engine' => 'smarty3',
55         ];
56
57         private static $ldelim = [
58                 'internal' => '',
59                 'smarty3' => '{{'
60         ];
61         private static $rdelim = [
62                 'internal' => '',
63                 'smarty3' => '}}'
64         ];
65
66         /**
67          * Returns the rendered template output from the template string and variables
68          *
69          * @param string $template
70          * @param array  $vars
71          * @return string
72          * @throws ServiceUnavailableException
73          */
74         public static function replaceMacros(string $template, array $vars = [])
75         {
76                 DI::profiler()->startRecording('rendering');
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                         DI::logger()->critical($e->getMessage(), ['template' => $template, 'vars' => $vars]);
87                         $message = DI::app()->isSiteAdmin() ?
88                                 $e->getMessage() :
89                                 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
90                         throw new ServiceUnavailableException($message);
91                 }
92
93                 DI::profiler()->stopRecording();
94
95                 return $output;
96         }
97
98         /**
99          * Load a given template $s
100          *
101          * @param string $file   Template to load.
102          * @param string $subDir Subdirectory (Optional)
103          *
104          * @return string template.
105          * @throws ServiceUnavailableException
106          */
107         public static function getMarkupTemplate($file, $subDir = '')
108         {
109                 DI::profiler()->startRecording('file');
110                 $t = self::getTemplateEngine();
111
112                 try {
113                         $template = $t->getTemplateFile($file, $subDir);
114                 } catch (Exception $e) {
115                         DI::logger()->critical($e->getMessage(), ['file' => $file, 'subDir' => $subDir]);
116                         $message = DI::app()->isSiteAdmin() ?
117                                 $e->getMessage() :
118                                 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
119                         throw new ServiceUnavailableException($message);
120                 }
121
122                 DI::profiler()->stopRecording();
123
124                 return $template;
125         }
126
127         /**
128          * Register template engine class
129          *
130          * @param string $class
131          * @throws ServiceUnavailableException
132          */
133         public static function registerTemplateEngine($class)
134         {
135                 $v = get_class_vars($class);
136
137                 if (!empty($v['name'])) {
138                         $name = $v['name'];
139                         self::$template_engines[$name] = $class;
140                 } else {
141                         $admin_message = DI::l10n()->t('template engine cannot be registered without a name.');
142                         DI::logger()->critical($admin_message, ['class' => $class]);
143                         $message = DI::app()->isSiteAdmin() ?
144                                 $admin_message :
145                                 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
146                         throw new ServiceUnavailableException($message);
147                 }
148         }
149
150         /**
151          * Return template engine instance.
152          *
153          * If $name is not defined, return engine defined by theme,
154          * or default
155          *
156          * @return TemplateEngine Template Engine instance
157          * @throws ServiceUnavailableException
158          */
159         public static function getTemplateEngine()
160         {
161                 $template_engine = (self::$theme['template_engine'] ?? '') ?: 'smarty3';
162
163                 if (isset(self::$template_engines[$template_engine])) {
164                         if (isset(self::$template_engine_instance[$template_engine])) {
165                                 return self::$template_engine_instance[$template_engine];
166                         } else {
167                                 $a = DI::app();
168                                 $class = self::$template_engines[$template_engine];
169                                 $obj = new $class($a->getCurrentTheme(), $a->getThemeInfo());
170                                 self::$template_engine_instance[$template_engine] = $obj;
171                                 return $obj;
172                         }
173                 }
174
175                 $admin_message = DI::l10n()->t('template engine is not registered!');
176                 DI::logger()->critical($admin_message, ['template_engine' => $template_engine]);
177                 $message = DI::app()->isSiteAdmin() ?
178                         $admin_message :
179                         DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
180                 throw new ServiceUnavailableException($message);
181         }
182
183         /**
184          * Returns the active template engine.
185          *
186          * @return string the active template engine
187          */
188         public static function getActiveTemplateEngine()
189         {
190                 return self::$theme['template_engine'];
191         }
192
193         /**
194          * sets the active template engine
195          *
196          * @param string $engine the template engine (default is Smarty3)
197          */
198         public static function setActiveTemplateEngine($engine = 'smarty3')
199         {
200                 self::$theme['template_engine'] = $engine;
201         }
202
203         /**
204          * Gets the right 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 right delimiter
213          */
214         public static function getTemplateLeftDelimiter($engine = 'smarty3')
215         {
216                 return self::$ldelim[$engine];
217         }
218
219         /**
220          * Gets the left delimiter for a template engine
221          *
222          * Currently:
223          * Internal = ''
224          * Smarty3 = '}}'
225          *
226          * @param string $engine The template engine (default is Smarty3)
227          *
228          * @return string the left delimiter
229          */
230         public static function getTemplateRightDelimiter($engine = 'smarty3')
231         {
232                 return self::$rdelim[$engine];
233         }
234 }