Skip to content

Commit

Permalink
完善Build类 开放module创建模块方法为公共方法
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Apr 1, 2016
1 parent 29a5fa9 commit e254999
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions library/think/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

class Build
{
public static function run($build)
// 根据传入的build资料创建目录和文件
public static function run($build = [])
{
// 锁定
$lockfile = APP_PATH . 'build.lock';
Expand All @@ -30,7 +31,7 @@ public static function run($build)
self::buildFile($list);
} else {
// 创建模块
self::buildModule($module, $list);
self::module($module, $list);
}
}
// 解除锁定
Expand Down Expand Up @@ -63,7 +64,7 @@ protected static function buildFile($list)
}

// 创建模块
protected static function buildModule($module, $list)
public static function module($module = '', $list = [])
{
$module = APP_MULTI_MODULE ? $module : '';
if (!is_dir(APP_PATH . $module)) {
Expand All @@ -76,6 +77,13 @@ protected static function buildModule($module, $list)
// 创建模块的默认页面
self::buildHello($module);
}
if (empty($list)) {
// 创建默认的模块目录和文件
$list = [
'__file__' => ['config.php', 'common.php'],
'__dir__' => ['controller', 'model', 'view'],
];
}
// 创建子目录和文件
foreach ($list as $path => $file) {
$modulePath = APP_PATH . $module . DS;
Expand All @@ -101,13 +109,13 @@ protected static function buildModule($module, $list)
$namespace = APP_NAMESPACE . '\\' . ($module ? $module . '\\' : '') . $path;
$class = $val . (CLASS_APPEND_SUFFIX ? ucfirst($path) : '');
switch ($path) {
case CONTROLLER_LAYER: // 控制器
case CONTROLLER_LAYER: // 控制器
$content = "<?php\nnamespace {$namespace};\n\nclass {$class}\n{\n\n}";
break;
case MODEL_LAYER: // 模型
case MODEL_LAYER: // 模型
$content = "<?php\nnamespace {$namespace};\n\nuse think\Model;\n\nclass {$class} extends Model\n{\n\n}";
break;
case VIEW_LAYER: // 视图
case VIEW_LAYER: // 视图
$filename = $modulePath . $path . DS . $val . '.html';
if (!is_dir(dirname($filename))) {
// 创建目录
Expand All @@ -128,7 +136,7 @@ protected static function buildModule($module, $list)
}
}

// 创建欢迎页面
// 创建模块的欢迎页面
protected static function buildHello($module)
{
$filename = APP_PATH . ($module ? $module . DS : '') . CONTROLLER_LAYER . DS . 'Index' . (CLASS_APPEND_SUFFIX ? ucfirst(CONTROLLER_LAYER) : '') . EXT;
Expand All @@ -142,12 +150,16 @@ protected static function buildHello($module)
}
}

// 创建模块公共文件
// 创建模块的公共文件
protected static function buildCommon($module)
{
$filename = APP_PATH . ($module ? $module . DS : '') . 'config.php';
if (!is_file($filename)) {
file_put_contents($filename, "<?php\nreturn [\n\n];");
}
$filename = APP_PATH . ($module ? $module . DS : '') . 'common.php';
if (!is_file($filename)) {
file_put_contents($filename, "<?php\n;");
}
}
}

0 comments on commit e254999

Please sign in to comment.