<?php
namespace Common;
/* *
* 功效:页面静态化的创立和删除
* 创立:当且仅当,一个页面须要被静态化并且还未静态化时。
* 删除:当且仅当,一个页面存在静态化页面并且须要被重新静态化时。
*
* 著作:郭军周
*
* 注 :本类基于ThinkPHP3.2,或其他具有“单一入口且MVC模式”的其他php框架。
*
* 应用方法:在Controller的结构办法中获得其对象;在Controller的烧毁办法里,用其对象的_static办法。
* 例:XXXController extends BaseController.
* BaseController:
* function __construct(){
* $this->__sh = StaticHtml::getInstance();
* }
* function __destruct(){
* $this->__sh->_static();
* }
*
* */
class StaticHtml{
private static $_instance = null; /* 单例模式,本身的援用 */
private $_needStatic = false; /* 是不是须要将其静态化 */
private $_needDeleteStatic = false; /* 是不是须要删除其静态化的页面 */
private $_hasStaticed = true; /* 是不是存在其的静态化页面 */
private $_controller = null; /* 当前被拜访的controller */
private $_action = null; /* 当前被拜访的action */
// private $_staticAgain = false; /* 删除静态文件后,是不是马上重新更新【【注意】再次要求】 */
private $_save_path = null; /* 将要创立或删除的静态文件的路径 */
private $_conf = array( /* 此文件定义一些静态文件的寄存方法 */
"files_per_directory" => 100 /* 此值不准可被修正,除非是要删除所有已存在的静态文件,重新缓存 */
);
private $_staticList = array( /* 此数组,定义了须要创立静态化页面的Controller的action */
// "Base" => array( /* Base为controller name */
// "aaa" => array( /* aaa为action name */
// "save_path" => "/StaticHtml/Base/aaa/", /* save_path为生成的静态文件寄存的根路径 */
// "static_base" => "id", /* static_base为生成静态文件的“根据”。建议为对应数据库的primary_key */
// "alias" => "aaa" /* 静态文件的名字,否则为1.html */
// )
// )
"Mynotes" => array(
"look_notes" => array(
"save_path" => "/StaticHtml/Mynotes/look_notes/",
"static_base" => "id",
"alias" => "note"
),
"add_personal_notes" => array(
"save_path" => "/StaticHtml/Mynotes/",
"alias" => "note-add"
)
),
"Resource" => array(
"allResource" => array(
"save_path" => "/StaticHtml/Resource/",
"alias" => "allResource"
),
"resource_add" => array(
"save_path" => "/StaticHtml/Resource/",
"alias" => "resourceAdd"
)
),
"Thing" => array(
"suggestion_of_lecture" => array(
"save_path" => "/StaticHtml/Lecture/",
"alias" => "voteLecture"
)
),
"Passwordfix" => array(
"index" => array(
"save_path" => "/StaticHtml/Information/",
"alias" => "updatePassword"
)
),
"Information" => array(
"index" => array(
"save_path" => "/StaticHtml/Information/",
"static_base" => "user_id",
"alias" => "information"
)
),
"Courseinfo" => array(
"course_show" => array(
"save_path" => "/StaticHtml/Information/",
"static_base" => "user_id",
"alias" => "course"
)
)
);
private $_deleteList = array( /* 此数组,定义了须要删除某些静态化页面的Controller的action */
// "Base" => array( /* Base为controller name */
// "bbb" => array( /* bbb为action name */
// "save_path" => "/StaticHtml/Base/aaa/", /* save_path为要删除的静态文件寄存的根路径 */
// "static_base" => "id", /* static_base为肯定静态文件路径的“根据”。建议为对应数据库的primary_key */
// "alias" => "aaa" /* 静态文件的名字,否则为1.html */
// )
// )
"Mynotes" => array(
"edits_notes" => array(
"save_path" => "/StaticHtml/Mynotes/look_notes/",
"static_base" => "id",
"alias" => "note"
)
),
"Information" => array(
"save_user_info" => array(
"save_path" => "/StaticHtml/Information/",
"static_base" => "user_id",
"alias" => "information"
)
),
"Courseinfo" => array(
"course_update" => array(
"save_path" => "/StaticHtml/Information/",
"static_base" => "user_id",
"alias" => "course"
)
)
);
private function __construct(){
$this->needStatic(); /* 肯定本次要求是不是须要静态化 */
$this->hasStaticed(); /* 肯定本次要求是不是已存在静态化页面 */
$this->needDeleteStatic(); /* 肯定本次要求是不是须要删除某些静态页面 */
}
/* 肯定须要删除的静态文件的寄存路径 */
private function needDeleteStatic(){
if($this->_needDeleteStatic){
$save_path = $this->getSavePath($this->_deleteList[$this->_controller][$this->_action]);
$this->_hasStaticed = false;
if(file_exists(ROOT_PATH.$save_path)){
$this->_hasStaticed = true;
}
// $this->_staticAgain = $this->_deleteList[$this->_controller][$this->_action]["visitAgain"];
$this->_save_path = ROOT_PATH.$save_path;
}
}
/* 获得本类的,唯一的,实例化 */
public static function getInstance(){
if(!(self::$_instance instanceof self)){
self::$_instance = new self();
}
return self::$_instance;
}
/* 断定是不是存在其静态化的文件 */
private function hasStaticed(){
if($this->_needStatic){
$save_path = $this->getSavePath($this->_staticList[$this->_controller][$this->_action]);
if(!file_exists(ROOT_PATH.$save_path)){
$this->_hasStaticed = false;
ob_start();
}else{
header("location: https://".$_SERVER["HTTP_HOST"].dirname($_SERVER["SCRIPT_NAME"]).$save_path);
}
$this->_save_path = ROOT_PATH.$save_path;
}
}
/* 获得本次要求要生成或删除的,静态化文件的路径 */
private function getSavePath($conf){
if(!isset($conf["static_base"])){
$save_path = $conf["save_path"];
$save_path .= $conf["alias"].".html";
}else{
if($conf["static_base"] == "user_id"){
$id = (int)$_SESSION["logined_user"]["id"];
}else{
if(IS_GET){
$id = $_GET[$conf["static_base"]];
}else{
$id = $_POST[$conf["static_base"]];
}
}
$save_path = $conf["save_path"];
$directory_id = ceil($id/$this->_conf["files_per_directory"]);
$save_path .= $directory_id."/";
if($conf["alias"]){
$fileName = $conf["alias"]."-";
}
$fileName .= $id.".html";
$save_path .= $fileName;
}
return $save_path;
}
/* 肯定本次要求,是不是须要生成静态化文件 */
private function needStatic(){
$url = explode("/",/Index/article);
$this->_controller = $url[4];
$this->_action = $url[5];
if(isset($this->_staticList[$this->_controller]) && isset($this->_staticList[$this->_controller][$this->_action])){
$this->_needStatic = true;
}
if(isset($this->_deleteList[$this->_controller]) && isset($this->_deleteList[$this->_controller][$this->_action])){
$this->_needDeleteStatic = true;
}
}
/* 生成,或删除,静态化文件 */
public function _static(){
if($this->_needStatic && !$this->_hasStaticed){
$html = ob_get_contents();
$this->_mkdir(dirname($this->_save_path));
file_put_contents($this->_save_path,$html);
}
if($this->_needDeleteStatic && $this->_hasStaticed){
unlink($this->_save_path);
/*if($this->_staticAgain){
header("location: https://www.baidu.com");
// header("location: https://".$_SERVER["HTTP_HOST"]."/".$_SERVER["REQUEST_URI"]);
}*/
}
}
/* 创立目录 */
private function _mkdir($path){
if (!file_exists($path)){
$this->_mkdir(dirname($path));
mkdir($path, 0777);
}
}
}
?><?php
namespace Common;
/* *
* 功效:页面静态化的创立和删除
*