00001 <?php
00003
00004
00005
00006
00007
00008
00010
00028 class FLEA_View_Simple
00029 {
00035 var $path;
00036
00043 var $cacheLifetime;
00044
00051 var $enableCache;
00052
00059 var $cacheDir;
00060
00067 var $vars;
00068
00075 var $cacheState;
00076
00084 function FLEA_View_Simple($path = null) {
00085 log_message('Construction FLEA_View_Simple', 'debug');
00086 $this->path = $path;
00087 $this->cacheLifetime = 900;
00088 $this->enableCache = true;
00089 $this->cacheDir = './cache';
00090 $this->vars = array();
00091 $this->cacheState = array();
00092 }
00093
00100 function assign($name, $value = null) {
00101 if (is_array($name) && is_null($value)) {
00102 $this->vars = array_merge($this->vars, $name);
00103 } else {
00104 $this->vars[$name] = $value;
00105 }
00106 }
00107
00116 function & fetch($file, $cacheId = null) {
00117 if ($this->enableCache) {
00118 $cacheFile = $this->_getCacheFile($file, $cacheId);
00119 if ($this->isCached($file, $cacheId)) {
00120 return file_get_contents($cacheFile);
00121 }
00122 }
00123
00124
00125 extract($this->vars);
00126 ob_start();
00127
00128 include($this->path . DIRECTORY_SEPARATOR . $file);
00129 $contents = ob_get_contents();
00130 ob_end_clean();
00131
00132 if ($this->enableCache) {
00133
00134 $this->cacheState[$cacheFile] = file_put_contents($cacheFile, $contents) > 0;
00135 }
00136
00137 return $contents;
00138 }
00139
00146 function display($file, $cacheId = null) {
00147 echo $this->fetch($file, $cacheId);
00148 }
00149
00158 function isCached($file, $cacheId = null) {
00159
00160 if (!$this->enableCache) { return false; }
00161
00162
00163 $cacheFile = $this->_getCacheFile($file, $cacheId);
00164 if (isset($this->cacheState[$cacheFile]) && $this->cacheState[$cacheFile]) {
00165 return true;
00166 }
00167
00168
00169 if (!is_readable($cacheFile)) { return false; }
00170
00171
00172 $mtime = filemtime($cacheFile);
00173 if ($mtime == false) { return false; }
00174 if (($mtime + $this->cacheLifetime) < time()) {
00175 $this->cacheState[$cacheFile] = false;
00176 @unlink($cacheFile);
00177 return false;
00178 }
00179
00180 $this->cacheState[$cacheFile] = true;
00181 return true;
00182 }
00183
00190 function cleanCache($file, $cacheId = null) {
00191 @unlink($this->_getCacheFile($file, $cacheId));
00192 }
00193
00197 function cleanAllCache() {
00198 foreach (glob($this->cacheDir . '/' . "*.php") as $filename) {
00199 @unlink($filename);
00200 }
00201 }
00202
00211 function _getCacheFile($file, $cacheId) {
00212 return $this->cacheDir . DIRECTORY_SEPARATOR .
00213 rawurlencode($file . '-' . $cacheId) . '.php';
00214 }
00215 }