-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefresh.php
More file actions
executable file
·93 lines (91 loc) · 2.72 KB
/
refresh.php
File metadata and controls
executable file
·93 lines (91 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* 本文见的功能就是在添加文件之后,更新文章对应的列表
* 本文件的意义在于一定程度上学习模仿模板的原理,而且一定程度上可以规避静态网站的缺点
* @name: refresh.php
* @author: unasm<[email protected]>
* @since : 2014-04-19 13:59:41
*/
Define("HREF" , "http://unasm.github.io/");
Define("TPL" , 'index.tpl.php');
class Refresh{
private $tplHandle;
/**
* 筹备数据,为接下来的解析准备
*/
function __construct($path){
$data['fileList'] = $this->readFile($path);
$data['title'] = "unasm的博客";
$data['domain'] = HREF;
//将数据传入,减少于外界的耦合
$this->parseIndex($data, TPL);
}
/**
* 读取当前文件夹下面所有的html文件
* @todo 实现按照时间顺序进行排序,具体巩固filemtime的方式获取修改时间,
*/
function readFile($path){
$fileList = [];
if(is_dir($path)){
$dir = opendir($path);
if(!$dir){
exit("打开文件夹失败" . __LINE__ . "行\n");
}
while(($fileName = readdir($dir)) !== false){
if((substr($fileName,-5) === '.html') && is_file($path . $fileName)){
if($fileName !== 'index.html'){
$fileList [] = $fileName;
}
}
}
} else{
echo "路径并非是文件夹";
}
closedir($dir);
return $fileList;
}
/**
* 开始解析生成模板数据index.php
* 因为是生成的目录,所以默认生成的文件名子是index.html
*/
function parseIndex($data, $tplPath){
foreach($data as $idx => $value){
$$idx = $value;
}
ob_start();
include $tplPath;
$tpl = ob_get_contents();
ob_end_clean();
$fp = @fopen('index.html', 'w') or die("打开文件index.html失败");
if(flock($fp, LOCK_EX)){
fwrite($fp , $tpl);
fflush($fp);
flock($fp, LOCK_UN);
}
fclose($fp);
}
/**
* 生成数据文件
*/
public function parseFile($filepath){
$file = fopen($filePath , true);
$fileName = basename($filePath);
if(!$this->tplHandle){
exit("模板文件尚未初始化");
}
fclose($file);
}
/**
* 初始化数据的模板文件
*/
protected function initDataTpl(){
$this->tplHandle = fopen("./tplData.tpl.html");
}
function __destruct(){
if($this->tplHandle){
fclose($this->tplHandle);
}
}
}
$oper = new Refresh("./");
?>