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