]> git.mxchange.org Git - friendica.git/blob - src/Core/Renderer.php
Fix PHPDoc comments project-wide
[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\Core\System;
11 use Friendica\Render\FriendicaSmarty;
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          *
58          * @return string substituted string
59          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
60          */
61     public static function replaceMacros($s, $vars)
62     {
63         $stamp1 = microtime(true);
64         $a = self::getApp();
65
66         // pass $baseurl to all templates
67         $vars['$baseurl'] = System::baseUrl();
68         $t = self::getTemplateEngine();
69
70         try {
71             $output = $t->replaceMacros($s, $vars);
72         } catch (Exception $e) {
73             echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
74             exit();
75         }
76
77         $a->saveTimestamp($stamp1, "rendering");
78
79         return $output;
80     }
81
82         /**
83          * @brief Load a given template $s
84          *
85          * @param string $s    Template to load.
86          * @param string $root Optional.
87          *
88          * @return string template.
89          * @throws Exception
90          */
91     public static function getMarkupTemplate($s, $root = '')
92     {
93         $stamp1 = microtime(true);
94         $a = self::getApp();
95         $t = self::getTemplateEngine();
96
97         try {
98             $template = $t->getTemplateFile($s, $root);
99         } catch (Exception $e) {
100             echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
101             exit();
102         }
103
104         $a->saveTimestamp($stamp1, "file");
105
106         return $template;
107     }
108
109     /**
110          * @brief Register template engine class
111          *
112          * @param string $class
113          */
114         public static function registerTemplateEngine($class)
115         {
116         $v = get_class_vars($class);
117         
118         if (!empty($v['name']))
119         {
120                         $name = $v['name'];
121                         self::$template_engines[$name] = $class;
122                 } else {
123                         echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
124                         die();
125                 }
126         }
127
128         /**
129          * @brief Return template engine instance.
130          *
131          * If $name is not defined, return engine defined by theme,
132          * or default
133          *
134          * @return object Template Engine instance
135          */
136         public static function getTemplateEngine()
137         {
138                 $template_engine = defaults(self::$theme, 'template_engine', 'smarty3');
139
140                 if (isset(self::$template_engines[$template_engine])) {
141                         if (isset(self::$template_engine_instance[$template_engine])) {
142                                 return self::$template_engine_instance[$template_engine];
143                         } else {
144                                 $class = self::$template_engines[$template_engine];
145                                 $obj = new $class;
146                                 self::$template_engine_instance[$template_engine] = $obj;
147                                 return $obj;
148                         }
149                 }
150
151                 echo "template engine <tt>$template_engine</tt> is not registered!\n";
152                 exit();
153     }
154     
155     /**
156          * @brief Returns the active template engine.
157          *
158          * @return string the active template engine
159          */
160         public static function getActiveTemplateEngine()
161         {
162                 return self::$theme['template_engine'];
163         }
164
165         /**
166          * sets the active template engine
167          *
168          * @param string $engine the template engine (default is Smarty3)
169          */
170         public static function setActiveTemplateEngine($engine = 'smarty3')
171         {
172                 self::$theme['template_engine'] = $engine;
173         }
174
175     /**
176          * Gets the right delimiter for a template engine
177          *
178          * Currently:
179          * Internal = ''
180          * Smarty3 = '{{'
181          *
182          * @param string $engine The template engine (default is Smarty3)
183          *
184          * @return string the right delimiter
185          */
186         public static function getTemplateLeftDelimiter($engine = 'smarty3')
187         {
188                 return self::$ldelim[$engine];
189         }
190
191         /**
192          * Gets the left delimiter for a template engine
193          *
194          * Currently:
195          * Internal = ''
196          * Smarty3 = '}}'
197          *
198          * @param string $engine The template engine (default is Smarty3)
199          *
200          * @return string the left delimiter
201          */
202         public static function getTemplateRightDelimiter($engine = 'smarty3')
203         {
204                 return self::$rdelim[$engine];
205         }
206 }