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