改进Think\Create类,支持自动生成模块的公共文件

This commit is contained in:
thinkphp
2013-04-07 15:08:03 +08:00
parent d4944bd9ca
commit 24755c4cf6

View File

@@ -12,43 +12,50 @@
namespace Think; namespace Think;
class Create { class Create {
static public function build($install) { static public function build($build) {
// 锁定 // 锁定
$lockfile = APP_PATH.'install.lock'; $lockfile = APP_PATH.'create.lock';
if(is_writable($lockfile)) { if(is_writable($lockfile)) {
return ; return ;
} else { } else {
touch($lockfile); touch($lockfile);
} }
foreach ($install as $module=>$list){ foreach ($build as $module=>$list){
if(!is_dir(APP_PATH.$module)) {// 创建模块目录 if(!is_dir(APP_PATH.$module)) {// 创建模块目录
mkdir(APP_PATH.$module); mkdir(APP_PATH.$module);
} }
// 创建配置文件和公共文件
self::buildCommonFile($module);
// 创建欢迎页面
self::buildHelloController($module);
// 创建子目录和文件
foreach($list as $path=>$file){ foreach($list as $path=>$file){
if(is_int($path)) { if(is_int($path)) {
// 生成文件 // 生成文件
if(!is_file(APP_PATH.$module.'/'.$file)) { if(!is_file(APP_PATH.$module.'/'.$file)) {
file_put_contents(APP_PATH.$module.'/'.$file,"<?php\nreturn [\n];\n?>"); file_put_contents(APP_PATH.$module.'/'.$file,"<?php\n");
} }
}else{ }else{
// 创建模块的子目录
if(!is_dir(APP_PATH.$module.'/'.$path)){ if(!is_dir(APP_PATH.$module.'/'.$path)){
mkdir(APP_PATH.$module.'/'.$path); mkdir(APP_PATH.$module.'/'.$path);
} }
foreach($file as $val){ foreach($file as $val){
switch($path) { switch($path) {
case 'Controller': case 'Controller':// 控制器
$filename = ucwords($val).$path; $filename = ucwords($val).$path;
if(!is_file(APP_PATH.$module.'/'.$path.'/'.$filename.'.php')) { if(!is_file(APP_PATH.$module.'/'.$path.'/'.$filename.'.php')) {
file_put_contents(APP_PATH.$module.'/'.$path.'/'.$filename.'.php',"<?php\nnamespace {$module}\\{$path}\nclass {$filename} {\n}"); file_put_contents(APP_PATH.$module.'/'.$path.'/'.$filename.'.php',"<?php\nnamespace {$module}\\{$path}\nclass {$filename} {\n}");
} }
break; break;
case 'Model': case 'Model': // 模型
$filename = ucwords($val).$path; $filename = ucwords($val).$path;
if(!is_file(APP_PATH.$module.'/'.$path.'/'.$filename.'.php')) { if(!is_file(APP_PATH.$module.'/'.$path.'/'.$filename.'.php')) {
file_put_contents(APP_PATH.$module.'/'.$path.'/'.$filename.'.php',"<?php\nnamespace {$module}\\{$path}\nclass {$filename} extends Model{\n}"); file_put_contents(APP_PATH.$module.'/'.$path.'/'.$filename.'.php',"<?php\nnamespace {$module}\\{$path}\nclass {$filename} extends Model{\n}");
} }
break; break;
case 'View': case 'View': // 视图
break; break;
default: default:
$filename = ucwords($val).$path; $filename = ucwords($val).$path;
@@ -66,8 +73,27 @@ class Create {
} }
// 创建欢迎页面 // 创建欢迎页面
static public function buildHelloControll() { static public function buildHelloController($module) {
$content = file_get_contents(THINK_PATH.'Tpl/default_index.tpl'); if(!is_file(APP_PATH.$module.'/Controller/IndexController.php')) {
file_put_contents(LIB_PATH.'Action/IndexAction.class.php',$content); $content = file_get_contents(THINK_PATH.'Tpl/default_index.tpl');
$content = str_replace('{$module}',$module,$content);
if(!is_dir(APP_PATH.$module.'/Controller')) {
mkdir(APP_PATH.$module.'/Controller');
}
file_put_contents(APP_PATH.$module.'/Controller/IndexController.php',$content);
}
}
// 创建模块公共文件
static public function buildCommonFile($module){
if(!is_file(APP_PATH.$module.'/common.php')) {
file_put_contents(APP_PATH.$module.'/common.php',"<?php\n");
}
if(!is_file(APP_PATH.$module.'/config.php')) {
file_put_contents(APP_PATH.$module.'/config.php',"<?php\nreturn [\n];");
}
if(!is_file(APP_PATH.$module.'/alias.php')) {
file_put_contents(APP_PATH.$module.'/alias.php',"<?php\nreturn [\n];");
}
} }
} }