不知道是否是官方的忽略,这里有个很明显的bug.
function FLEA_View_Simple($path = null) {
log_message('Construction FLEA_View_Simple', 'debug');
$this->path = $path;
$this->cacheLifetime = 900;
$this->enableCache = TRUE;
$this->cacheDir = './cache';
$viewConfig = (array)FLEA::getAppInf('viewConfig');
$keys = array(
'templateDir', 'cacheDir', 'cacheLifeTime', 'enableCache',
);
foreach ($keys as $key) {
if (!empty($viewConfig[$key])) {
$this->{$key} = $viewConfig[$key];
dump($this->{$key});
}
}
}
$viewConfig = (array)FLEA::getAppInf('viewConfig');
$keys = array(
'templateDir', 'cacheDir', 'cacheLifeTime', 'enableCache',
);
foreach ($keys as $key) {
if (!empty($viewConfig[$key])) {
$this->{$key} = $viewConfig[$key];
dump($this->{$key});
}
}
我在外面的配置文件如下:
$viewTplInf = array(
'view' => 'FLEA_View_Simple',
'viewConfig' => array(
'templateDir' => 'exap-tpl/TplEngine/tpl',
'cacheDir' => 'exap-tpl/TplEngine/cache',
'cacheLifetime' => 600 ,
'enableCache' => FALSE
)
);
写道
if (!empty($viewConfig[$key]))
因为 empty方法,对布尔型的 false 也认为 空
所以 'enableCache' => FALSE,这个根本就传不进去
因为 empty方法,对布尔型的 false 也认为 空
所以 'enableCache' => FALSE,这个根本就传不进去
这个是我改正的,由于不好在源代码上做更改,我建了一个新的类..
<?php
/**
* TplEngine 实现了一个简单的、使用 PHP 自身作为模版语言,
* 带有缓存功能的模版引擎
*/
class TplEngine
{
var $templateDir; //模板文件所在路径
var $cacheLifetime; //缓存过期时间
var $enableCache; //指示是否使用 cache
var $cacheDir; //缓存文件保存位置
var $vars = array(); //模板变量
var $cacheState = array(); //保存各个缓存内容的缓存状态
function config($viewTplInf = null){
$keys = array(
'templateDir', 'cacheDir', 'cacheLifeTime', 'enableCache',
);
foreach ($keys as $key) {
if (array_key_exists($key,$viewTplInf))
$this->{$key} = $viewTplInf[$key];
}
}
/**
* @param string $path 模板文件所在路径
*
* @return Simple_View_Engine
*/
function TplEngine() {
$this->templateDir = './tpl' ;
$this->cacheLifetime = 900;
$this->enableCache = 1;
$this->cacheDir = './cache';
}
/**
* 设置模板变量
*
* @param mixed $name 模板变量名称
* @param mixed $value 变量内容
*/
function assign($name, $value = null) {
if (is_array($name) && is_null($value)) {
$this->vars = array_merge($this->vars, $name);
} else {
$this->vars[$name] = $value;
}
}
/**
* 构造模板输出内容
*
* @param string $file 模板文件名
* @param string $cacheId 缓存 ID,如果指定该值则会使用该内容的缓存输出
*
* @return string
*/
function fetch($file, $cacheId = null) {
if ($this->enableCache) {
$cacheFile = $this->_getCacheFile($file, $cacheId);
if ($this->isCached($file, $cacheId)) {
return file_get_contents($cacheFile);
}
}
// 生成输出内容并缓存
extract($this->vars);
ob_start();
include($this->templateDir . '/' . $file);
$contents = ob_get_contents();
ob_end_clean();
if ($this->enableCache) {
// 缓存输出内容,并保存缓存状态
$this->cacheState[$cacheFile] = file_put_contents($cacheFile, $contents) > 0;
}
return $contents;
}
/**
* 显示指定模版的内容
*
* @param string $file 模板文件名
* @param string $cacheId 缓存 ID,如果指定该值则会使用该内容的缓存输出
*/
function display($file, $cacheId = null) {
echo $this->fetch($file, $cacheId);
}
/**
* 检查内容是否已经被缓存
*
* @param string $file 模板文件名
* @param string $cacheId 缓存 ID
*
* @return boolean
*/
function isCached($file, $cacheId = null) {
// 如果禁用缓存则返回 false
if (!$this->enableCache) { return false; }
// 如果缓存标志有效返回 true
$cacheFile = $this->_getCacheFile($file, $cacheId);
if (isset($this->cacheState[$cacheFile]) && $this->cacheState[$cacheFile]) {
return true;
}
// 检查缓存文件是否存在
if (!is_readable($cacheFile)) { return false; }
// 检查缓存文件是否已经过期
$mtime = filemtime($cacheFile);
if ($mtime == false) { return false; }
if (($mtime + $this->cacheLifetime) < time()) {
$this->cacheState[$cacheFile] = false;
@unlink($cacheFile);
return false;
}
$this->cacheState[$cacheFile] = true;
return true;
}
/**
* 清除指定的缓存
*
* @param string $file 模板资源名
* @param string $cacheId 缓存 ID
*/
function cleanCache($file, $cacheId = null) {
@unlink($this->_getCacheFile($file, $cacheId));
}
/**
* 清除所有缓存
*/
function cleanAllCache() {
foreach (glob($this->cacheDir . '/' . "*.html") as $filename) {
@unlink($filename);
}
}
/**
* 返回缓存文件名
*
* @param string $file
* @param string $cacheId
*
* @return string
*/
function _getCacheFile($file, $cacheId) {
return $this->cacheDir . '/' . rawurlencode($file . '-' . $cacheId) . '.html';
}
}
例子:
$GLOBALS['viewTplInf'] = array(
'templateDir' => 'exap-tpl/TplEngine/tpl',
'cacheDir' => 'exap-tpl/TplEngine/cache',
'cacheLifeTime' => 600 ,
'enableCache' => true
);
$viewTpl = FLEA::getSingleton('TplEngine');
$viewTpl->config($GLOBALS['viewTplInf']);
dump($viewTpl);