您的位置:首页 > 编程语言 > PHP开发

zhphp framework(九) html静态处理相关类,自动页面静态化处理

2015-12-11 13:39 561 查看
<?php
/**
* Created by JetBrains PhpStorm.
* User: 张华
* Date: 14-3-8
* Time: 下午12:21
* QQ: 746502560@qq.com
* To change this template use File | Settings | File Templates.
*/
final class html {
/**
* arr2option
* 数组转成<option></option>列表
* @param array $arr 待转换的数组
* @param string $value option选项中的value所对应的数组中的key
* @param string $name option选项中<option>与</option>之间用于描述的文字对应数组的key
* @param string $selected 与value对比,相同的值则设置为选中状态
* @return string 返回html代码字符串
*/
public  static  function arr2option($arr,$value,$name,$selected=""){
$option="";
foreach($arr as $v){
if(!is_array($v)) continue;
if(!isset($v[$value]) || !isset($v[$name])) continue;
if($v[$value]==$selected){
$option.="<option value=\"{$v[$value]}\" selected>{$v[$name]}</option>".PHP_EOL;
}else{
$option.="<option value=\"{$v[$value]}\">{$v[$name]}</option>".PHP_EOL;
}
}
return $option;
}
/**
* 提前载入静态,减少程序开销
* @return
*/
public static function  tryStaticHtml(){
$staticHtmls=config::readConfig('staticHtml');
if($staticHtmls == false ||  empty($staticHtmls)) return;#表示没有缓存静;
#读取静态操作,debug 最大, 如果 debug 为true, 那么程序始终是动态状态
if(APP_DEBUG == true)return;
$router=engine::load('router');
$routerInfo=createModule_Control_Action(serialize($router));
$template_path=$routerInfo['moduleName'].'/'.$routerInfo['controlName'].'/';
$template_file=WEB_STATIC.$template_path.$routerInfo['actionName'].createStaticHtmlFileName($routerInfo['params']).'.html';
if(file_exists($template_file)){
if( self::checkHtmlTime($template_file) ){
echo file_get_contents($template_file);
unset($router,$staticHtmls,$routerInfo,$template_path);
ob_end_flush();#刷新磁盘
exit;
}

}
return;
##################  后面的程序为非法文件 #################################
}

/***
* @param $fileName
* @param $tplObject
*/
public static  function actionStaticFile($fileName,$tplObject){
$smarty=unserialize($tplObject);
$content=$smarty->fetch($fileName);
$routerInfo=createModule_Control_Action(serialize(engine::load('router')));
$template_path=$routerInfo['moduleName'].'/'.$routerInfo['controlName'].'/';
$filedir=is_dir(WEB_STATIC)?WEB_STATIC.$template_path:die('error:静态逻辑不存在');
file::mk_dir($filedir);chmod($filedir,0775);
$template_file=$filedir.$routerInfo['actionName'].createStaticHtmlFileName($routerInfo['params']).'.html';
if(file_exists($template_file) === false){
file::touchFile($template_file); chmod($template_file,0777);
$fp=fopen($template_file,'a+') or die('error:模板文件资源打开失败');
$startTime = microtime();
do{
$canWrite=flock($fp,LOCK_EX);
if( $canWrite === false) usleep(round(rand(0,100)*1000));
}while((!$canWrite)&&((microtime()-$startTime)< 1000));
if($canWrite){
fwrite($fp,$content);
}
fclose($fp);
}
echo $content;
unset($fileName,$template_path,$filedir,$template_file,$startTime,$canWrite,$content,$fp,$smarty,$routerInfo);
exit();
}

/**
* @param $fileName
* @param $action
* @param $cacheId
* @param $parentLayout
* @param $tplObject
*/
public static function  runTpl($fileName,$action,$cacheId,$parentLayout,$tplObject){
$smarty=unserialize($tplObject);
if(is_null($parentLayout)){
empty($cacheId)?$smarty->display($fileName,$action):$smarty->display($fileName,$cacheId);
}else{
empty($cacheId)?$smarty->display('extends:'.$parentLayout.'|'.$fileName,$action):$smarty->display('extends:'.$parentLayout.'|'.$fileName,$cacheId);
}
exit;
}
/**
* @param $htmlFile
* @param int $losttime
* @return bool
*  检查html 文件是否过期
*/
public  static function checkHtmlTime($htmlFile,$losttime=0){
//定义过期时间,5分钟
$lostTime  =$losttime==0?config::readConfig('staticHtml_lifetime'):$losttime;
if(file_exists($htmlFile)){
$fileSize = filesize($htmlFile);
if($fileSize<=0){
return true;
}
$checkTime = filemtime($htmlFile);
if( $lostTime > 0 ){
return true;
}else{
$ccTime    = time() - ($checkTime + $lostTime);
if($ccTime>0) {
return true;
}else {
return false;
}
}
}else{
return true;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: