Simple.php

浏览该文件的文档。
00001 <?php
00003 // FleaPHP Framework
00004 //
00005 // Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
00006 //
00007 // 许可协议,请查看源代码中附带的 LICENSE.txt 文件,
00008 // 或者访问 http://www.fleaphp.org/ 获得详细信息。
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         // 如果禁用缓存则返回 false
00160         if (!$this->enableCache) { return false; }
00161 
00162         // 如果缓存标志有效返回 true
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 }

Generated at Sat Feb 2 15:18:51 2008 for FleaPHP by  doxygen 1.5.3