[置顶] FleaPHP中FLEA_View_Simple的bug 和 本人的修正版本
来源:互联网 发布:人工智能能否代替老师 编辑:程序博客网 时间:2024/05/17 09:43
不知道是否是官方的忽略,这里有个很明显的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);
0 0
- [置顶] FleaPHP中FLEA_View_Simple的bug 和 本人的修正版本
- Fleaphp 数组辅助文件中 array_to_tree 的bug修正
- [置顶] 我修正的 modalbox 的一个bug
- [置顶] FLEAPHP 数据库查询 in 的用法
- 修正dwr中addRows方法的bug
- 修正dwr中addRows方法的bug
- 修正ECMALL在PHP5.3以上版本中无法开启支付方式的BUG
- XUBUNTU-8.04-a6版本中安装BCM43XX无线网卡的BUG修正方法
- 修正bug的方法
- CCEditBox的bug修正
- 修正asp.net版本下无法新增部门的BUG
- [置顶] 让FLEAPHP支持简易的命名空间
- ReportBuilder 中ppMemo不支持中文换行的Bug 的修正
- 修正XPMenu的两个Bug
- BugFree 的一个 bug 修正
- Subversion分支的Bug修正
- 修正IEWebControls中TreeView的一处bug
- 修正TEmbeddedWEB不能使用中键滚动的bug
- Oracle设置账户口令生存期
- 点击新增按钮,弹出对话框,并且对话框加载另外一个页面的内容的具体实现
- [置顶] PHP有无必要使用Smarty呢??
- ubuntu下helloworld内核模块编译、加载/卸载
- leetcode水题
- [置顶] FleaPHP中FLEA_View_Simple的bug 和 本人的修正版本
- 高效Web开发的10个jQuery代码片段
- java 百文买百鸡 鸡兔同笼 回文数 关键代码
- [置顶] Office Web化的一种解决方案
- 点击UITableViewcell 弹出AlertController 出现延迟问题
- [置顶] 网站unicode字符乱码问题一扫空解决方案 -- 带demo 简单的小说应用
- loadrunner场景对性能测试策略的映射
- [置顶] QUI继续之 -- 添加JS模板引擎功能, 征集LOGO
- 使用NSArray时,到底用strong修饰还是copy修饰