From 1ce4c7122a56e66b681fa2c5ac3958449838f38d Mon Sep 17 00:00:00 2001 From: wuyanwen Date: Sat, 14 Dec 2019 14:58:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=89=E8=A3=85=E5=91=BD?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/command/BackupCommand.php | 4 ++- extend/catcher/command/InstallCommand.php | 36 +++++++++++++++----- extend/catcher/command/MigrateRunCommand.php | 2 ++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/extend/catcher/command/BackupCommand.php b/extend/catcher/command/BackupCommand.php index 18d904d..f35bc27 100644 --- a/extend/catcher/command/BackupCommand.php +++ b/extend/catcher/command/BackupCommand.php @@ -157,7 +157,9 @@ class BackupCommand extends Command \PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false ]; - \config($connections,'database'); + \config([ + 'connections' => $connections, + ],'database.connections'); } diff --git a/extend/catcher/command/InstallCommand.php b/extend/catcher/command/InstallCommand.php index 139df1c..cd5fb59 100644 --- a/extend/catcher/command/InstallCommand.php +++ b/extend/catcher/command/InstallCommand.php @@ -13,7 +13,7 @@ use think\facade\Db; class InstallCommand extends Command { - protected $dataInstall = true; + protected $databaseLink = []; protected function configure() { @@ -129,6 +129,8 @@ class InstallCommand extends Command } } + $this->databaseLink = [$host, $database, $username, $password, $port, $charset, $prefix]; + $this->generateEnvFile($host, $database, $username, $password, $port, $charset, $prefix); } } @@ -141,16 +143,28 @@ class InstallCommand extends Command */ protected function secondStep(): void { - $php = getenv('_'); + $connections = \config('database.connections'); - $think = root_path() . DIRECTORY_SEPARATOR . 'think'; + [ + $connections['mysql']['hostname'], + $connections['mysql']['database'], + $connections['mysql']['username'], + $connections['mysql']['password'], + $connections['mysql']['hostport'], + $connections['mysql']['charset'], + $connections['mysql']['prefix'], + ] = $this->databaseLink; + + \config([ + 'connections' => $connections, + ],'database'); foreach (CatchAdmin::getModulesDirectory() as $directory) { $moduleInfo = CatchAdmin::getModuleInfo($directory); - - $this->output->info( - sprintf('module [%s] migrations ', $moduleInfo['alias']) . - exec(sprintf('%s %s catch-migrate:run %s', $php, $think, $moduleInfo['alias']))); + if (is_dir(CatchAdmin::moduleMigrationsDirectory($moduleInfo['alias']))) { + $output = Console::call('catch-migrate:run', [$moduleInfo['alias']]); + $this->output->info(sprintf('module [%s] migrations %s', $moduleInfo['alias'], $output->fetch())); + } } } @@ -192,6 +206,7 @@ class InstallCommand extends Command */ protected function generateEnvFile($host, $database, $username, $password, $port, $charset, $prefix): void { + try { $env = \parse_ini_file(root_path() . '.example.env', true); $env['DATABASE']['HOSTNAME'] = $host; @@ -218,7 +233,6 @@ class InstallCommand extends Command } } - file_put_contents(root_path() . '.env', $dotEnv); if ($this->getEnvFile()) { $this->output->info('env file has been generated'); @@ -229,6 +243,12 @@ class InstallCommand extends Command } else { $this->output->warning(sprintf('create database %s failed,you need create database first by yourself', $database)); } + } catch (\Exception $e) { + $this->output->error($e->getMessage()); + exit(0); + } + + file_put_contents(root_path() . '.env', $dotEnv); } /** diff --git a/extend/catcher/command/MigrateRunCommand.php b/extend/catcher/command/MigrateRunCommand.php index ba808b8..95bd6b4 100644 --- a/extend/catcher/command/MigrateRunCommand.php +++ b/extend/catcher/command/MigrateRunCommand.php @@ -46,6 +46,8 @@ EOT } $end = microtime(true); + // 重置 migrations 在循环冲无法重复使用 + $this->migrations = null; $output->writeln(''); $output->writeln('All Done. Took ' . sprintf('%.4fs', $end - $start) . ''); }