| 2809 |
rexy |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Smarty Internal Plugin
|
|
|
4 |
*
|
|
|
5 |
* @package Smarty
|
|
|
6 |
* @subpackage Cacher
|
|
|
7 |
*/
|
|
|
8 |
|
|
|
9 |
/**
|
|
|
10 |
* Cache Handler API
|
|
|
11 |
*
|
|
|
12 |
* @package Smarty
|
|
|
13 |
* @subpackage Cacher
|
|
|
14 |
* @author Rodney Rehm
|
|
|
15 |
*/
|
|
|
16 |
abstract class Smarty_CacheResource
|
|
|
17 |
{
|
|
|
18 |
/**
|
|
|
19 |
* resource types provided by the core
|
|
|
20 |
*
|
|
|
21 |
* @var array
|
|
|
22 |
*/
|
|
|
23 |
protected static $sysplugins = array('file' => 'smarty_internal_cacheresource_file.php',);
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* populate Cached Object with meta data from Resource
|
|
|
27 |
*
|
|
|
28 |
* @param \Smarty_Template_Cached $cached cached object
|
|
|
29 |
* @param Smarty_Internal_Template $_template template object
|
|
|
30 |
*
|
|
|
31 |
* @return void
|
|
|
32 |
*/
|
|
|
33 |
abstract public function populate(\Smarty_Template_Cached $cached, Smarty_Internal_Template $_template);
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* populate Cached Object with timestamp and exists from Resource
|
|
|
37 |
*
|
|
|
38 |
* @param Smarty_Template_Cached $cached
|
|
|
39 |
*
|
|
|
40 |
* @return void
|
|
|
41 |
*/
|
|
|
42 |
abstract public function populateTimestamp(Smarty_Template_Cached $cached);
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Read the cached template and process header
|
|
|
46 |
*
|
|
|
47 |
* @param Smarty_Internal_Template $_template template object
|
|
|
48 |
* @param Smarty_Template_Cached $cached cached object
|
|
|
49 |
* @param boolean $update flag if called because cache update
|
|
|
50 |
*
|
|
|
51 |
* @return boolean true or false if the cached content does not exist
|
|
|
52 |
*/
|
|
|
53 |
abstract public function process(
|
|
|
54 |
Smarty_Internal_Template $_template,
|
|
|
55 |
Smarty_Template_Cached $cached = null,
|
|
|
56 |
$update = false
|
|
|
57 |
);
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Write the rendered template output to cache
|
|
|
61 |
*
|
|
|
62 |
* @param Smarty_Internal_Template $_template template object
|
|
|
63 |
* @param string $content content to cache
|
|
|
64 |
*
|
|
|
65 |
* @return boolean success
|
|
|
66 |
*/
|
|
|
67 |
abstract public function writeCachedContent(Smarty_Internal_Template $_template, $content);
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Read cached template from cache
|
|
|
71 |
*
|
|
|
72 |
* @param Smarty_Internal_Template $_template template object
|
|
|
73 |
*
|
|
|
74 |
* @return string content
|
|
|
75 |
*/
|
|
|
76 |
abstract public function readCachedContent(Smarty_Internal_Template $_template);
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Return cached content
|
|
|
80 |
*
|
|
|
81 |
* @param Smarty_Internal_Template $_template template object
|
|
|
82 |
*
|
|
|
83 |
* @return null|string
|
|
|
84 |
*/
|
|
|
85 |
public function getCachedContent(Smarty_Internal_Template $_template)
|
|
|
86 |
{
|
|
|
87 |
if ($_template->cached->handler->process($_template)) {
|
|
|
88 |
ob_start();
|
|
|
89 |
$unifunc = $_template->cached->unifunc;
|
|
|
90 |
$unifunc($_template);
|
|
|
91 |
return ob_get_clean();
|
|
|
92 |
}
|
|
|
93 |
return null;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Empty cache
|
|
|
98 |
*
|
|
|
99 |
* @param Smarty $smarty Smarty object
|
|
|
100 |
* @param integer $exp_time expiration time (number of seconds, not timestamp)
|
|
|
101 |
*
|
|
|
102 |
* @return integer number of cache files deleted
|
|
|
103 |
*/
|
|
|
104 |
abstract public function clearAll(Smarty $smarty, $exp_time = null);
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Empty cache for a specific template
|
|
|
108 |
*
|
|
|
109 |
* @param Smarty $smarty Smarty object
|
|
|
110 |
* @param string $resource_name template name
|
|
|
111 |
* @param string $cache_id cache id
|
|
|
112 |
* @param string $compile_id compile id
|
|
|
113 |
* @param integer $exp_time expiration time (number of seconds, not timestamp)
|
|
|
114 |
*
|
|
|
115 |
* @return integer number of cache files deleted
|
|
|
116 |
*/
|
|
|
117 |
abstract public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time);
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* @param Smarty $smarty
|
|
|
121 |
* @param Smarty_Template_Cached $cached
|
|
|
122 |
*
|
|
|
123 |
* @return bool|null
|
|
|
124 |
*/
|
|
|
125 |
public function locked(Smarty $smarty, Smarty_Template_Cached $cached)
|
|
|
126 |
{
|
|
|
127 |
// theoretically locking_timeout should be checked against time_limit (max_execution_time)
|
|
|
128 |
$start = microtime(true);
|
|
|
129 |
$hadLock = null;
|
|
|
130 |
while ($this->hasLock($smarty, $cached)) {
|
|
|
131 |
$hadLock = true;
|
|
|
132 |
if (microtime(true) - $start > $smarty->locking_timeout) {
|
|
|
133 |
// abort waiting for lock release
|
|
|
134 |
return false;
|
|
|
135 |
}
|
|
|
136 |
sleep(1);
|
|
|
137 |
}
|
|
|
138 |
return $hadLock;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Check is cache is locked for this template
|
|
|
143 |
*
|
|
|
144 |
* @param Smarty $smarty
|
|
|
145 |
* @param Smarty_Template_Cached $cached
|
|
|
146 |
*
|
|
|
147 |
* @return bool
|
|
|
148 |
*/
|
|
|
149 |
public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
|
|
|
150 |
{
|
|
|
151 |
// check if lock exists
|
|
|
152 |
return false;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
/**
|
|
|
156 |
* Lock cache for this template
|
|
|
157 |
*
|
|
|
158 |
* @param Smarty $smarty
|
|
|
159 |
* @param Smarty_Template_Cached $cached
|
|
|
160 |
*
|
|
|
161 |
* @return bool
|
|
|
162 |
*/
|
|
|
163 |
public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
|
|
|
164 |
{
|
|
|
165 |
// create lock
|
|
|
166 |
return true;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Unlock cache for this template
|
|
|
171 |
*
|
|
|
172 |
* @param Smarty $smarty
|
|
|
173 |
* @param Smarty_Template_Cached $cached
|
|
|
174 |
*
|
|
|
175 |
* @return bool
|
|
|
176 |
*/
|
|
|
177 |
public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
|
|
|
178 |
{
|
|
|
179 |
// release lock
|
|
|
180 |
return true;
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
/**
|
|
|
184 |
* Load Cache Resource Handler
|
|
|
185 |
*
|
|
|
186 |
* @param Smarty $smarty Smarty object
|
|
|
187 |
* @param string $type name of the cache resource
|
|
|
188 |
*
|
|
|
189 |
* @throws SmartyException
|
|
|
190 |
* @return Smarty_CacheResource Cache Resource Handler
|
|
|
191 |
*/
|
|
|
192 |
public static function load(Smarty $smarty, $type = null)
|
|
|
193 |
{
|
|
|
194 |
if (!isset($type)) {
|
|
|
195 |
$type = $smarty->caching_type;
|
|
|
196 |
}
|
|
|
197 |
// try smarty's cache
|
|
|
198 |
if (isset($smarty->_cache[ 'cacheresource_handlers' ][ $type ])) {
|
|
|
199 |
return $smarty->_cache[ 'cacheresource_handlers' ][ $type ];
|
|
|
200 |
}
|
|
|
201 |
// try registered resource
|
|
|
202 |
if (isset($smarty->registered_cache_resources[ $type ])) {
|
|
|
203 |
// do not cache these instances as they may vary from instance to instance
|
|
|
204 |
return $smarty->_cache[ 'cacheresource_handlers' ][ $type ] = $smarty->registered_cache_resources[ $type ];
|
|
|
205 |
}
|
|
|
206 |
// try sysplugins dir
|
|
|
207 |
if (isset(self::$sysplugins[ $type ])) {
|
|
|
208 |
$cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type);
|
|
|
209 |
return $smarty->_cache[ 'cacheresource_handlers' ][ $type ] = new $cache_resource_class();
|
|
|
210 |
}
|
|
|
211 |
// try plugins dir
|
|
|
212 |
$cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type);
|
|
|
213 |
if ($smarty->loadPlugin($cache_resource_class)) {
|
|
|
214 |
return $smarty->_cache[ 'cacheresource_handlers' ][ $type ] = new $cache_resource_class();
|
|
|
215 |
}
|
|
|
216 |
// give up
|
|
|
217 |
throw new SmartyException("Unable to load cache resource '{$type}'");
|
|
|
218 |
}
|
|
|
219 |
}
|