]> src.bluestatic.org Git - isso.git/blob - Template.php
Update version number
[isso.git] / Template.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2008 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Database-Driven Template System (template.php)
24 *
25 * @package ISSO
26 */
27
28 require_once(ISSO . '/Functions.php');
29
30 /**
31 * File-Based Template System
32 *
33 * This framework merely replaces the template loading functions with
34 * file-system based ones. It has an optional caching system in which
35 * template data will remain stored in the database as long as the filesystem
36 * file is modified. To do this, pass a table name to setDatabaseCache() and make sure
37 * there's a DB module that has access to a table with this schema:
38 *
39 * CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL);
40 *
41 * @author Blue Static
42 * @copyright Copyright (c)2005 - 2008, Blue Static
43 * @package ISSO
44 *
45 */
46 class BSTemplate
47 {
48 /**
49 * The name of a function that is called before template parsing of phrases and conditionals occurs
50 * @var string
51 */
52 public static $preParseHook = ':undefined:';
53
54 /**
55 * The database table name for the template cache
56 * @var string
57 */
58 public static $dbCacheTable = null;
59
60 /**
61 * The name of the function phrases are fetched with
62 * @var string
63 */
64 public static $langcall = 'gettext';
65
66 /**
67 * The template path pattern; for instance, this could be: ./templates/%s.tpl
68 * @var string
69 */
70 public static $templatePath = '%s';
71
72 /**
73 * Array of pre-compiled templates that are stored for optimization
74 * @var array
75 */
76 protected static $cache = array();
77
78 /**
79 * The name of the function phrases are sprintf() parsed with
80 * @var string
81 */
82 public static $langconst = 'sprintf';
83
84 /**
85 * Template variables to populate
86 * @var array
87 */
88 public $vars = array();
89
90 /**
91 * Global variables
92 * @var array
93 */
94 public static $globalVars = array();
95
96 /**
97 * The file name of the template
98 * @var string
99 */
100 protected $filename;
101
102 /**
103 * Template contents
104 * @var string
105 */
106 protected $template;
107
108 /**
109 * Takes an array of template names, loads them, and then stores a
110 * parsed version for optimum speed.
111 *
112 * @param array List of template names to be cached
113 */
114 public static function cache($namearray)
115 {
116 if (!self::$dbCacheTable)
117 {
118 return; // there's no point in pre-caching file templates
119 }
120
121 $cache = BSApp::$db->query("SELECT * FROM " . self::$dbCacheTable . " WHERE filename IN ('" . implode("', '", $namearray) . "')");
122 while ($tpl = $cache->fetchArray())
123 {
124 self::$cache[$tpl['filename']] = $tpl;
125 }
126 }
127
128 /**
129 * Fluent interface-compatible constructor
130 */
131 public static function fetch()
132 {
133 $obj = new ReflectionClass(__CLASS__);
134 $args = func_get_args();
135 return $obj->newInstanceArgs($args);
136 }
137
138 /**
139 * Constructor
140 *
141 * @param string File name
142 */
143 public function __construct($path)
144 {
145 $this->file = $path;
146
147 // checks to see if the template has been cached
148 if (isset(self::$cache[$this->file]))
149 {
150 if (!self::$dbCacheTable || filemtime(sprintf(self::$templatePath, $this->file)) <= self::$cache[$this->file]['timestamp'])
151 {
152 $this->template = self::$cache[$this->file]['template'];
153 return;
154 }
155 }
156
157 // it hasn't been cached
158 $path = sprintf(self::$templatePath, $this->file);
159 if (!is_file($path) || !is_readable($path))
160 {
161 throw new Exception("Could not load the template $path");
162 }
163 $this->template = $this->_parseTemplate(file_get_contents($path));
164 self::$cache[$this->file]['template'] = $this->template;
165
166 // store the template in the database
167 if (self::$dbCacheTable)
168 {
169 BSApp::$db->query("REPLACE INTO " . self::$dbCacheTable . " SET template = '" . BSApp::$db->escapeString($this->template) . "', timestamp = " . TIMENOW . ", filename = '" . $this->file . "'");
170 self::$cache[$this->file]['time'] = TIMENOW;
171 }
172 }
173
174 /**
175 * Returns the template data
176 *
177 * @return string Final template data
178 */
179 public function getTemplate()
180 {
181 return $this->template;
182 }
183
184 /**
185 * This function globalizes/extracts the assigned variables and then
186 * returns the output buffer
187 *
188 * @param string Unevaluated template
189 *
190 * @return fluent interface
191 */
192 public function evaluate()
193 {
194 extract($this->vars);
195 extract(self::$globalVars);
196
197 ob_start();
198 $this->template = str_replace(array('$this->', 'self::'), 'null', $this->template); // don't want internal access coming from a template
199 $this->template = '?>' . $this->template;
200 $test = eval($this->template);
201 $output = ob_get_clean();
202 if ($output === false)
203 {
204 throw new Exception('A parse error was encountered while evaluating the template');
205 }
206
207 $this->template = $output;
208
209 return $this;
210 }
211
212 /**
213 * Output a template fully compiled to the browser
214 */
215 public function flush()
216 {
217 ob_start();
218
219 if (empty($this->template))
220 {
221 throw new Exception('There is no output to print');
222 }
223
224 $template = $this->template;
225
226 $debugBlock = '';
227 if (BSApp::get_debug() && strpos($template, '</body>') !== false)
228 {
229 $debugBlock .= "\n<div align=\"center\">Executed in " . round(BSFunctions::fetch_microtime_diff('0 ' . $_SERVER['REQUEST_TIME']), 10) . ' seconds</div>';
230 $debugBlock .= "\n<br /><div align=\"center\">" . BSApp::get_debug_list() . "</div>";
231
232 if (BSApp::$db)
233 {
234 $queries = BSApp::$db->getHistory();
235
236 $debugBlock .= "<br />\n" . '<table cellpadding="4" cellspacing="1" border="0" align="center" width="30%" style="background-color: rgb(60, 60, 60); color: white">' . "\n\t" . '<tr><td><strong>Query Debug</strong></td></tr>';
237
238 foreach ($queries as $query)
239 {
240 $debugBlock .= "\n\t<tr style=\"background-color: rgb(230, 230, 230); color: black\">";
241 $debugBlock .= "\n\t\t<td>";
242 $debugBlock .= "\n\t\t\t$query[query]\n\n\t\t\t<div style=\"font-size: 9px;\">($query[time])</div>\n<!--\n$query[trace]\n-->\n\t\t</td>\n\t</tr>";
243 }
244
245 $debugBlock .= "\n</table>\n\n\n";
246 }
247
248 $template = str_replace('</body>', $debugBlock . '</body>', $template);
249 }
250
251 print($template);
252 }
253
254 /**
255 * A wrapper for all the parsing functions and compiling functins
256 *
257 * @param string Unparsed template data
258 *
259 * @return string Parsed template data
260 */
261 protected function _parseTemplate($template)
262 {
263 if (function_exists(self::$preParseHook))
264 {
265 $template = call_user_func(self::$preParseHook, $template, $this);
266 }
267
268 $template = $this->_parseTokens($template);
269 return $template;
270 }
271
272 /**
273 * Parses tokens <% %>
274 *
275 * @param string Template data
276 *
277 * @return string Parsed template data
278 */
279 protected function _parseTokens($template)
280 {
281 $stack = array();
282 $tokens = array();
283
284 for ($i = 0; $i < strlen($template); $i++)
285 {
286 // opening tag
287 if ($template[$i] == '<' && $template[$i + 1] == '%')
288 {
289 array_push($stack, $i);
290 }
291 // closing tag
292 else if ($template[$i] == '%' && $template[$i + 1] == '>')
293 {
294 // there's no stack, so it's a bad template
295 if (sizeof($stack) == 0)
296 {
297 throw new Exception('Malformed template data: unexpected closing substitution tag');
298 }
299 // we're good and nested
300 else if (sizeof($stack) == 1)
301 {
302 $open = array_pop($stack);
303 $echo = ($template[$open + 2] == '-' ? 'echo ' : '');
304 $replace = '<?php ' . $echo . BSFunctions::substring($template, $open + ($echo ? 3 : 2), $i) . ' ?>';
305 $template = substr_replace($template, $replace, $open, ($i + 2) - $open);
306 }
307 // just pop it off
308 else
309 {
310 array_pop($stack);
311 } // end else
312 } // end if
313 } // end for
314
315 return $template;
316 }
317 }
318
319 ?>