-
Notifications
You must be signed in to change notification settings - Fork 12
/
caching_xtemplate.class.php
378 lines (308 loc) · 10.1 KB
/
caching_xtemplate.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'xtemplate.class.php');
/**
* CachingXTemplate
* Extension to XTemplate to provide block level and whole template caching facilities
* Needs Web server writable directory
*
* @package XTemplate
* @subpackage CachingXTemplate
* @uses XTemplate
* @author Jeremy Coates [cocomp@users.sourceforge.net]
* @copyright Jeremy Coates / Co-Comp Ltd 2006-2007
* @see license.txt BSD license
* @since PHP 5
* @link $HeadURL: https://xtpl.svn.sourceforge.net/svnroot/xtpl/trunk/caching_xtemplate.class.php $
* @version $Id: caching_xtemplate.class.php 21 2007-05-29 18:01:15Z cocomp $
*
* @example Whole template level caching (e.g. the total parsed output for the file)
* @example $xtpl = new CachingXTemplate('template.xtpl', '', null, 'main', true, 600, session_id(), './xcache', '.xcache');
*
* @example Alternatively (and perhaps more useful in real world):
* @example Block level caching
* @example $xtpl = new CachingXTemplate('template.xtpl', '', null, 'main', true, 0, session_id(), './xcache', '.xcache');
* @example $xtpl->parse('main', 600);
* @example Bear in mind that because XTemplate uses a reversed parsing tree the innermost blocks need to be parsed
* @example first, therefore if you cache an outer block, don't be surprised when it's inner content blocks don't update!
*/
class CachingXTemplate extends XTemplate {
/**
* Cache expiry time (seconds)
*
* @access public
* @var int
*/
public $cache_expiry = 0;
/**
* Cache file unique identifier
*
* @example session_id()
* @access public
* @var string
*/
public $cache_unique = 'unique';
/**
* Filename extension
*
* @example .xcache
* @access public
* @var string
*/
public $cache_ext = '.xcache';
/**
* Path to cache dir
* Needs to be writable by webserver
*
* @example ./xcache
* @access public
* @var string
*/
public $cache_dir = './xcache';
/**
* Flag showing whether template is cached
*
* @access private
* @var boolean
*/
private $_template_is_cached = false;
/**
* Cache expiry time
*
* @access private
* @var int
*/
private $_cache_expiry = 0;
/**
* File modified time
*
* @access private
* @var int
*/
private $_cache_filemtime = 0;
/**
* Override of parent constructor
*
* @access public
* @param string $file Template file to work on
* @param string $tpldir Location of template files (useful for keeping files outside web server root)
* @param array $files Filenames lookup
* @param string $mainblock Name of main block in the template
* @param boolean $autosetup If true, run setup() as part of constuctor
* @param int $cache_expiry Seconds to cache for
* @param string $cache_unique Unique file id (e.g. session_id())
* @param string $cache_dir Cache folder
* @param string $cache_ext Cache file extension
*/
public function __construct($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true, $cache_expiry = 0, $cache_unique = '', $cache_dir = './xcache', $cache_ext = '.xcache') {
$this->restart($file, $tpldir, $files, $mainblock, $autosetup, $this->tag_start_delim, $this->tag_end_delim, $cache_expiry, $cache_unique, $cache_dir, $cache_ext);
}
/**
* Override of parent restart method
*
* @access public
* @param string $file Template file to work on
* @param string $tpldir Location of template files
* @param array $files Filenames lookup
* @param string $mainblock Name of main block in the template
* @param boolean $autosetup If true, run setup() as part of restarting
* @param string $tag_start {
* @param string $tag_end }
* @param int $cache_expiry Seconds to cache for
* @param string $cache_unique Unique file id (e.g. session_id())
* @param string $cache_dir Cache folder
* @param string $cache_ext Cache file extension
*/
public function restart ($file, $tpldir = '', $files = null, $mainblock = 'main', $autosetup = true, $tag_start = '{', $tag_end = '}', $cache_expiry = 0, $cache_unique = '', $cache_dir = './xcache', $cache_ext = '.xcache') {
if ($cache_expiry > 0) {
$this->cache_expiry = $cache_expiry;
}
if (!empty($cache_unique)) {
if (!preg_match('/^\./', $cache_unique)) {
$cache_unique = '.' . $cache_unique;
}
$this->cache_unique = $cache_unique;
}
if (!empty($cache_dir)) {
$this->cache_dir = $cache_dir;
}
if (!empty($cache_ext)) {
if (!preg_match('/^\./', $cache_ext)) {
$cache_ext = '.' . $cache_ext;
}
$this->cache_ext = $cache_ext;
}
// Call parent restart method but don't run setup yet!
parent::restart($file, $tpldir, $files, $mainblock, false, $tag_start, $tag_end);
if ($this->cache_expiry > 0) {
$this->read_template_cache();
}
if (!$this->_template_is_cached && $autosetup) {
$this->setup();
}
}
/**
* Override of parent assign method
*
* @access public
* @param string $name Variable to assign $val to
* @param string / array $val Value to assign to $name
* @param boolean $magic_quotes
*/
public function assign ($name, $val = '', $magic_quotes = false) {
if (!$this->_template_is_cached) {
parent::assign($name, $val, $magic_quotes);
}
}
/**
* Override of parent assign_file method
*
* @access public
* @param string $name Variable to assign $val to
* @param string / array $val Values to assign to $name
*/
public function assign_file ($name, $val = '') {
if (!$this->_template_is_cached) {
parent::assign_file($name, $val);
}
}
/**
* Override of parent parse method
*
* @access public
* @param string $bname Block name to parse
* @param int $cache_expiry Seconds to cache block for
*/
public function parse ($bname, $cache_expiry = 0) {
if (!$this->_template_is_cached) {
if (!$this->read_block_cache($bname, $cache_expiry)) {
parent::parse($bname);
$this->write_block_cache($bname, $cache_expiry);
}
}
}
/**
* Override of parent text method
*
* @access public
* @param string $bname Block name to return
* @return string
*/
public function text ($bname = '') {
$text = parent::text($bname);
if (!$this->_template_is_cached && $this->cache_expiry > 0) {
$this->write_template_cache();
} elseif ($this->debug && $this->output_type == 'HTML') {
$text_header = "<!-- CachingXTemplate debug:\n";
if ($this->cache_expiry > 0) {
$filename = $this->_get_filename();
$file = $this->cache_dir . DIRECTORY_SEPARATOR . $filename . $this->cache_unique . $this->cache_ext;
$text_header .= 'File: ' . $file . "\nExpires in: " . ($this->_cache_filemtime - $this->_cache_expiry) . " seconds -->\n";
} else {
$text_header .= "Template Cache (whole template) disabled -->\n";
}
$text = $text_header . $text;
}
return $text;
}
/**
* Read whole template cache file
*
* @access protected
*/
protected function read_template_cache () {
$filename = $this->_get_filename();
$file = $this->cache_dir . DIRECTORY_SEPARATOR . $filename . DIRECTORY_SEPARATOR . $this->cache_unique . $this->cache_ext;
if ($this->cache_expiry > 0 && file_exists($file)) {
$this->_cache_filemtime = filemtime($file);
$this->_cache_expiry = time() - $this->cache_expiry;
if ($this->_cache_filemtime >= $this->_cache_expiry) {
if ($parsed_blocks = file_get_contents($file)) {
$this->parsed_blocks = unserialize($parsed_blocks);
$this->_template_is_cached = true;
}
} else {
// Stale file
if (is_writable($this->cache_dir) && is_writable($file)) {
unlink($file);
}
}
}
}
/**
* Write out whole template cache file
*
* @access protected
*/
protected function write_template_cache () {
if ($this->cache_expiry > 0 && is_writable($this->cache_dir)) {
$filename = $this->_get_filename();
if (!file_exists($this->cache_dir . DIRECTORY_SEPARATOR . $filename)) {
mkdir($this->cache_dir . DIRECTORY_SEPARATOR . $filename);
}
file_put_contents($this->cache_dir . DIRECTORY_SEPARATOR . $filename . DIRECTORY_SEPARATOR . $this->cache_unique . $this->cache_ext, serialize($this->parsed_blocks));
}
}
/**
* Read block level cache file
*
* @access protected
* @param string $bname Block name to read from cache
* @param ing $cache_expiry Seconds to cache block for
* @return boolean
*/
protected function read_block_cache ($bname, $cache_expiry = 0) {
$retval = false;
$filename = $this->_get_filename();
$file = $this->cache_dir . DIRECTORY_SEPARATOR . $filename . DIRECTORY_SEPARATOR . $bname . $this->cache_unique . $this->cache_ext;
if ($cache_expiry > 0 && file_exists($file)) {
$filemtime = filemtime($file);
$cache_expiry = time() - $cache_expiry;
if ($filemtime >= $cache_expiry) {
if ($block = file_get_contents($file)) {
$block = unserialize($block);
if ($this->debug) {
$block = "<!-- CachingXTemplate debug:\nFile: " . $file . "\nBlock: " . $bname . "\nExpires in: " . ($filemtime - $cache_expiry) . ' seconds -->' . "\n" . $block;
}
$this->parsed_blocks[$bname] = $block;
$retval = true;
}
} else {
// Stale file
if (is_writable($this->cache_dir) && is_writable($file)) {
unlink($file);
}
}
}
return $retval;
}
/**
* Write out block level cache file
*
* @access protected
* @param string $bname Block name to cache
* @param int $cache_expiry Seconds to cache block for
*/
protected function write_block_cache ($bname, $cache_expiry = 0) {
if ($cache_expiry > 0 && is_writable($this->cache_dir)) {
$filename = $this->_get_filename();
if (!file_exists($this->cache_dir . DIRECTORY_SEPARATOR . $filename)) {
mkdir($this->cache_dir . DIRECTORY_SEPARATOR . $filename);
}
file_put_contents($this->cache_dir . DIRECTORY_SEPARATOR . $filename . DIRECTORY_SEPARATOR . $bname . $this->cache_unique . $this->cache_ext, serialize($this->parsed_blocks[$bname]));
}
}
/**
* Create the main part of the cache filename
*
* @access private
* @return string
*/
private function _get_filename () {
$filename = $this->filename;
if (!empty($this->tpldir)) {
$filename = str_replace(DIRECTORY_SEPARATOR, '_', $this->tpldir . DIRECTORY_SEPARATOR) . $this->filename;
}
return $filename;
}
}
?>