think\Build::module PHP Method

module() public static method

创建模块
public static module ( string $module = '', array $list = [], string $namespace = 'app', boolean $suffix = false ) : void
$module string 模块名
$list array build列表
$namespace string 应用类库命名空间
$suffix boolean 类库后缀
return void
    public static function module($module = '', $list = [], $namespace = 'app', $suffix = false)
    {
        $module = $module ? $module : '';
        if (!is_dir(APP_PATH . $module)) {
            // 创建模块目录
            mkdir(APP_PATH . $module);
        }
        if (basename(RUNTIME_PATH) != $module) {
            // 创建配置文件和公共文件
            self::buildCommon($module);
            // 创建模块的默认页面
            self::buildHello($module, $namespace, $suffix);
        }
        if (empty($list)) {
            // 创建默认的模块目录和文件
            $list = ['__file__' => ['config.php', 'common.php'], '__dir__' => ['controller', 'model', 'view']];
        }
        // 创建子目录和文件
        foreach ($list as $path => $file) {
            $modulePath = APP_PATH . $module . DS;
            if ('__dir__' == $path) {
                // 生成子目录
                foreach ($file as $dir) {
                    if (!is_dir($modulePath . $dir)) {
                        // 创建目录
                        mkdir($modulePath . $dir, 0755, true);
                    }
                }
            } elseif ('__file__' == $path) {
                // 生成(空白)文件
                foreach ($file as $name) {
                    if (!is_file($modulePath . $name)) {
                        file_put_contents($modulePath . $name, 'php' == pathinfo($name, PATHINFO_EXTENSION) ? "<?php\n" : '');
                    }
                }
            } else {
                // 生成相关MVC文件
                foreach ($file as $val) {
                    $val = trim($val);
                    $filename = $modulePath . $path . DS . $val . ($suffix ? ucfirst($path) : '') . EXT;
                    $space = $namespace . '\\' . ($module ? $module . '\\' : '') . $path;
                    $class = $val . ($suffix ? ucfirst($path) : '');
                    switch ($path) {
                        case 'controller':
                            // 控制器
                            $content = "<?php\nnamespace {$space};\n\nclass {$class}\n{\n\n}";
                            break;
                        case 'model':
                            // 模型
                            $content = "<?php\nnamespace {$space};\n\nuse think\\Model;\n\nclass {$class} extends Model\n{\n\n}";
                            break;
                        case 'view':
                            // 视图
                            $filename = $modulePath . $path . DS . $val . '.html';
                            if (!is_dir(dirname($filename))) {
                                // 创建目录
                                mkdir(dirname($filename), 0755, true);
                            }
                            $content = '';
                            break;
                        default:
                            // 其他文件
                            $content = "<?php\nnamespace {$space};\n\nclass {$class}\n{\n\n}";
                    }
                    if (!is_file($filename)) {
                        file_put_contents($filename, $content);
                    }
                }
            }
        }
    }

Usage Example

Ejemplo n.º 1
0
 protected function execute(Input $input, Output $output)
 {
     if ($input->hasOption('module')) {
         \think\Build::module($input->getOption('module'));
         $output->writeln("Successed");
         return;
     }
     if ($input->hasOption('config')) {
         $build = (include $input->getOption('config'));
     } else {
         $build = (include APP_PATH . 'build.php');
     }
     if (empty($build)) {
         $output->writeln("Build Config Is Empty");
         return;
     }
     \think\Build::run($build);
     $output->writeln("Successed");
 }