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; } } }
相关文章推荐
- ThinkPHP 十一基于角色的控制访问
- zhphp framework (八) 路由引擎
- 高效的php 导出 xls,csv 的方法
- zhphp framework (七) 配置文件加载与读取以及动态写、读配置文件
- 基于ace后台管理系统模板--CMS(Thinkphp框架)的筹划
- zhphp frmaework (六) help帮助函数包,以及系统应用函数包
- zhphp framework (五) 框架加载类
- zhphp framework (四)动态加载应用程序产量,方便任何程序代码重构
- zhphp framework (三) 框架工作流程
- ThinkPHP
- 夺命雷公狗---PHP---玩转安卓2之扫码进行登录原理
- php laravel 帧 该文件上传
- php创建空对象
- Gearmand和PECL extension for Gearman版本不兼容的问题
- php扩展名获取函数
- php利用pdo进行mysql的事务处理机制
- phpstorm移除已创建的项目
- PHP漏洞全解(六)-Session劫持
- PHP漏洞全解(四)-SQL注入攻击
- php通过curl模拟功能总结