改进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;
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)) {
return ;
} else {
touch($lockfile);
}
foreach ($install as $module=>$list){
foreach ($build as $module=>$list){
if(!is_dir(APP_PATH.$module)) {// 创建模块目录
mkdir(APP_PATH.$module);
}
// 创建配置文件和公共文件
self::buildCommonFile($module);
// 创建欢迎页面
self::buildHelloController($module);
// 创建子目录和文件
foreach($list as $path=>$file){
if(is_int($path)) {
// 生成文件
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{
// 创建模块的子目录
if(!is_dir(APP_PATH.$module.'/'.$path)){
mkdir(APP_PATH.$module.'/'.$path);
}
foreach($file as $val){
switch($path) {
case 'Controller':
case 'Controller':// 控制器
$filename = ucwords($val).$path;
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}");
}
break;
case 'Model':
case 'Model': // 模型
$filename = ucwords($val).$path;
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}");
}
break;
case 'View':
case 'View': // 视图
break;
default:
$filename = ucwords($val).$path;
@@ -66,8 +73,27 @@ class Create {
}
// 创建欢迎页面
static public function buildHelloControll() {
$content = file_get_contents(THINK_PATH.'Tpl/default_index.tpl');
file_put_contents(LIB_PATH.'Action/IndexAction.class.php',$content);
static public function buildHelloController($module) {
if(!is_file(APP_PATH.$module.'/Controller/IndexController.php')) {
$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];");
}
}
}