]> src.bluestatic.org Git - isso.git/blob - TemplateFs.php
Added an additional parsing step in which curly braces are parsed into PHP code no...
[isso.git] / TemplateFs.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] 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 [#]gpl[#] 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 * File System Template System (TemplateFs.php)
24 *
25 * @package ISSO
26 */
27
28 require_once('ISSO/Template.php');
29 require_once('ISSO/Functions.php');
30
31 /**
32 * File System Template System
33 *
34 * This framework merely replaces the template loading functions with
35 * file-system based ones. It has an optional caching system in which
36 * template data will remain stored in the database as long as the filesystem
37 * file is modified. To do this, pass a table name to setDatabaseCache() and make sure
38 * there's a DB module that has access to a table with this schema:
39 *
40 * CREATE TABLE template (filename VARCHAR (250) NOT NULL, template TEXT NOT NULL, timestamp INT NOT NULL);
41 *
42 * @author Blue Static
43 * @copyright Copyright ©2002 - [#]year[#], Blue Static
44 * @version $Revision$
45 * @package ISSO
46 *
47 */
48 class BSTemplateFs extends BSTemplate
49 {
50 /**
51 * The path, from the path of the application, where templates are stored
52 * @var string
53 */
54 private $templateDir = '';
55
56 /**
57 * The extension all the template files have
58 * @var string
59 */
60 private $extension = 'tpl';
61
62 /**
63 * The database table name for the template cache
64 * @var string
65 */
66 private $dbCacheTable = null;
67
68 // ###################################################################
69 /**
70 * Constructor (overriding so we don't require the Db module)
71 */
72 public function __construct() {}
73
74 // ###################################################################
75 /**
76 * Sets the template directory name
77 *
78 * @param string Template directory name
79 */
80 public function setTemplateDirectory($dir)
81 {
82 $this->templateDir = BSFunctions::FetchSourcePath($dir);
83 }
84
85 // ###################################################################
86 /**
87 * Sets the file extension for the templates
88 *
89 * @param string File extension
90 */
91 public function setExtension($ext)
92 {
93 $this->extension = $ext;
94 }
95
96 // ###################################################################
97 /**
98 * Sets the name of the table to access for the datbase cache
99 *
100 * @param string DB table name
101 */
102 public function setDatabaseCache($table)
103 {
104 $this->dbCacheTable = $table;
105 }
106
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 function cache($namearray)
115 {
116 if (sizeof($this->cache) > 0)
117 {
118 trigger_error('You cannot cache templates more than once per initialization');
119 }
120 else
121 {
122 $dbCache = array();
123 if ($this->dbCacheTable)
124 {
125 $db =& BSRegister::GetType('Db');
126 $cache = $db->query("SELECT * FROM {$this->dbCacheTable} WHERE filename IN ('" . implode("', '", $namearray) . "')");
127 while ($tpl = $db->fetchArray($cache))
128 {
129 $time = filemtime(BSRegister::GetAppPath() . $this->templateDir . $tpl['filename'] . '.' . $this->extension);
130 $template = $tpl['template'];
131 if ($time > $tpl['timestamp'])
132 {
133 $template = $this->_parseTemplate($this->_loadTemplate($tpl['filename']));
134 $db->query("UPDATE {$this->dbCacheTable} SET template = '" . $db->escapeString($template) . "', timestamp = " . TIMENOW . " WHERE filename = '" . $tpl['filename'] . "'");
135 $tpl['template'] = $template;
136 }
137 $dbCache["$tpl[filename]"] = $template;
138 }
139 }
140 foreach ($namearray AS $name)
141 {
142 if ($this->dbCacheTable)
143 {
144 if (isset($dbCache["$name"]))
145 {
146 $template = $dbCache["$name"];
147 }
148 else
149 {
150 $template = $this->_parseTemplate($this->_loadTemplate($name));
151 $db->query("INSERT INTO {$this->dbCacheTable} (filename, template, timestamp) VALUES ('$name', '" . $db->escapeString($template) . "', " . TIMENOW . ")");
152 }
153 }
154 else
155 {
156 $template = $this->_parseTemplate($this->_loadTemplate($name));
157 }
158
159 $this->cache["$name"] = $template;
160 $this->usage["$name"] = 0;
161 }
162 }
163 }
164
165 // ###################################################################
166 /**
167 * Loads a template from the file system from the specified
168 * $templatedir with the file extension $extension
169 *
170 * @param string The name of the template call
171 */
172 protected function _loadTemplate($name)
173 {
174 $path = BSRegister::GetAppPath() . $this->templateDir . $name . '.' . $this->extension;
175 if (is_file($path))
176 {
177 if (($template = @file_get_contents($path)) !== false)
178 {
179 return $template;
180 }
181 else
182 {
183 trigger_error("Could not load the template '$path'");
184 exit;
185 }
186 }
187 else
188 {
189 trigger_error("Could not load the template '$path'");
190 exit;
191 }
192 }
193 }
194
195 /*=====================================================================*\
196 || ###################################################################
197 || # $HeadURL$
198 || # $Id$
199 || ###################################################################
200 \*=====================================================================*/
201 ?>