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