// +---------------------------------------------------------------------- namespace think\app\command; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\Output; class Build extends Command { /** * 应用基础目录 * @var string */ protected $basePath; /** * {@inheritdoc} */ protected function configure() { $this->setName('build') ->addArgument('app', Argument::OPTIONAL, 'app name .') ->setDescription('Build App Dirs'); } protected function execute(Input $input, Output $output) { $this->basePath = $this->app->getBasePath(); $app = $input->getArgument('app') ?: ''; if (is_file($this->basePath . 'build.php')) { $list = include $this->basePath . 'build.php'; } else { $list = [ '__dir__' => ['controller', 'model', 'view'], ]; } $this->buildApp($app, $list); $output->writeln("Successed"); } /** * 创建应用 * @access protected * @param string $app 应用名 * @param array $list 目录结构 * @return void */ protected function buildApp(string $app, array $list = []): void { if (!is_dir($this->basePath . $app)) { // 创建应用目录 mkdir($this->basePath . $app); } $appPath = $this->basePath . ($app ? $app . DIRECTORY_SEPARATOR : ''); $namespace = 'app' . ($app ? '\\' . $app : ''); // 创建配置文件和公共文件 $this->buildCommon($app); // 创建模块的默认页面 $this->buildHello($app, $namespace); foreach ($list as $path => $file) { if ('__dir__' == $path) { // 生成子目录 foreach ($file as $dir) { $this->checkDirBuild($appPath . $dir); } } elseif ('__file__' == $path) { // 生成(空白)文件 foreach ($file as $name) { if (!is_file($appPath . $name)) { file_put_contents($appPath . $name, 'php' == pathinfo($name, PATHINFO_EXTENSION) ? 'app->config->get('route.controller_suffix')) { $filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . 'Controller.php'; $class = $val . 'Controller'; } $content = "checkDirBuild(dirname($filename)); $content = ''; break; default: // 其他文件 $content = "app->config->get('route.controller_suffix') ? 'Controller' : ''; $filename = $this->basePath . ($app ? $app . DIRECTORY_SEPARATOR : '') . 'controller' . DIRECTORY_SEPARATOR . 'Index' . $suffix . '.php'; if (!is_file($filename)) { $content = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'controller.stub'); $content = str_replace(['{%name%}', '{%app%}', '{%layer%}', '{%suffix%}'], [$app, $namespace, 'controller', $suffix], $content); $this->checkDirBuild(dirname($filename)); file_put_contents($filename, $content); } } /** * 创建应用的公共文件 * @access protected * @param string $app 目录 * @return void */ protected function buildCommon(string $app): void { $appPath = $this->basePath . ($app ? $app . DIRECTORY_SEPARATOR : ''); if (!is_file($appPath . 'common.php')) { file_put_contents($appPath . 'common.php', "