优化2处写法

This commit is contained in:
huangdijia
2016-03-16 13:35:56 +08:00
parent 8a47be77b5
commit 45ca16e8b7

View File

@@ -221,15 +221,18 @@ class Template
*/ */
public function layout($name) public function layout($name)
{ {
if (false !== $name) { if (false === $name) {
// 关闭布局
$this->config['layout_on'] = false;
return $this;
}
// 开启布局
$this->config['layout_on'] = true; $this->config['layout_on'] = true;
// 名称必须为字符串
if (is_string($name)) { if (is_string($name)) {
$this->config['layout_name'] = $name; $this->config['layout_name'] = $name;
} }
} else { return $this;
// 关闭布局
$this->config['layout_on'] = false;
}
} }
/** /**
@@ -241,12 +244,27 @@ class Template
*/ */
private function checkCache($cacheFile) private function checkCache($cacheFile)
{ {
if ($this->config['tpl_cache'] && is_file($cacheFile) && $handle = @fopen($cacheFile, "r")) { // 未开启缓存功能
if (!$this->config['tpl_cache']) {
return false;
}
// 缓存文件不存在
if (!is_file($cacheFile)) {
return false;
}
// 读取缓存文件失败
if (!$handle = @fopen($cacheFile, "r")) {
return false;
}
// 读取第一行 // 读取第一行
preg_match('/\/\*(.+?)\*\//', fgets($handle), $matches); preg_match('/\/\*(.+?)\*\//', fgets($handle), $matches);
if (isset($matches[1])) { if (!isset($matches[1])) {
return false;
}
$includeFile = unserialize($matches[1]); $includeFile = unserialize($matches[1]);
if (is_array($includeFile)) { if (!is_array($includeFile)) {
return false;
}
// 检查模板文件是否有更新 // 检查模板文件是否有更新
foreach ($includeFile as $path => $time) { foreach ($includeFile as $path => $time) {
if (is_file($path) && filemtime($path) > $time) { if (is_file($path) && filemtime($path) > $time) {
@@ -254,13 +272,9 @@ class Template
return false; return false;
} }
} }
}
// 检查编译存储是否有效 // 检查编译存储是否有效
return $this->storage->check($cacheFile, $this->config['cache_time']); return $this->storage->check($cacheFile, $this->config['cache_time']);
} }
}
return false;
}
/** /**
* 检查编译缓存是否存在 * 检查编译缓存是否存在