genera.php
3.19 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<meta charset='utf-8'>
<?php
class Genera {
private $_search = array('%FRAMEWORK_DIR%');
private $_replace = array();
private $create_info = '' ;
/**
* 初始化
*
* @param string $_project 项目名称
* @param string $_object_dir 应用创建路径
* @param string $show_info 是否 显示创建信息
*/
function __construct($_project,$_object_dir,$show_info=true){
$_project = strtolower(preg_replace('/[^a-z0-9_]+/i', '', $_project));
if (empty($_project)){
throw new Exception('请填写有效的项目名称(字母、数字或下划线)');
}
if (!is_dir($_object_dir)){
throw new Exception('你想创建的目标路径不存在');
}
$from_source = dirname(__FILE__).'/app' ;
$target_source = $this->formatDir($_object_dir).'/'.$_project ;
if (!is_dir($from_source)){
throw new Exception($from_source.':框架模板不存在,缺少文件');
}
if (is_dir($target_source)) {
throw new Exception($target_source.':已存在,不可以重新生成');
}
if (!mkdir($target_source,0777)){
throw new Exception($target_source.':创建目录失败,请检查是否拥有权限');
}
$framework_dir = str_replace('\\','/',realpath(dirname(dirname(__FILE__))));
//设置替换值
$this->_replace = array($framework_dir);
if ($show_info == false){
ob_start();
}
$this->_copy($from_source,$target_source);
if ($show_info == false){
$create_info = ob_get_contents();
ob_clean();
$this->create_info = $create_info ;
}
}
/**
* 获取创建信息
*
* @return unknown
*/
public function getInfo(){
return $this->create_info ;
}
function formatDir($dir){
$dir = str_replace('\\','/',$dir);
if (substr($dir,-1,1) == '/'){
return substr($dir,0,-1);
}
return $dir ;
}
/**
* 复制内容
*
* @param unknown_type $_project
* @param unknown_type $_object_dir
*/
private function _copy($source,$target){
$source = rtrim($source, '/\\') . '/';
$target = rtrim($target, '/\\') . '/';
$h = opendir($source);
$skip = array('.', '..', '.svn', '.cvs');
while (($file = readdir($h)) !== false)
{
if (in_array($file, $skip)) continue;
$path = $source . $file;
echo ' create ', $target . $file;
echo "<br>";
if (is_dir($path))
{
mkdir($target . $file, 0777);
$this->_copy($path, $target . $file);
}
else
{
$filesize = filesize($path);
if ($filesize)
{
$fp = fopen($path, 'rb');
$content = fread($fp, $filesize);
fclose($fp);
}
else
{
$content = '';
}
$extname = strtolower(pathinfo($path, PATHINFO_EXTENSION));
if ($extname == 'php')
{
$content = str_replace($this->_search, $this->_replace, $content);
//$content = str_replace(array("\n\r", "\r\n", "\r"), "\n", $content);
}
$fp = fopen($target . $file, 'wb');
fwrite($fp, $content);
fclose($fp);
chmod($target . $file, 0666);
unset($content);
}
}
closedir($h);
}
}
?>