From 40276babfb66fa901274599cd8cdf30c12481733 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Fri, 11 Sep 2020 07:42:38 +0800 Subject: [PATCH] =?UTF-8?q?update:=E6=9B=B4=E6=96=B0=E5=AE=89=E8=A3=85&?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=9C=AC=E5=9C=B0=E6=A8=A1=E5=9D=97=E5=AE=89?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/install/DisableModuleCommand.php | 6 +- .../command/install/EnableModuleCommand.php | 4 +- .../install/InstallLocalModuleCommand.php | 68 +++++++ .../command/install/InstallProjectCommand.php | 28 +-- extend/catcher/library/InstallLocalModule.php | 176 ++++++++++++++++++ 5 files changed, 258 insertions(+), 24 deletions(-) create mode 100644 extend/catcher/command/install/InstallLocalModuleCommand.php create mode 100644 extend/catcher/library/InstallLocalModule.php diff --git a/extend/catcher/command/install/DisableModuleCommand.php b/extend/catcher/command/install/DisableModuleCommand.php index 7dd91a6..95d7c7e 100644 --- a/extend/catcher/command/install/DisableModuleCommand.php +++ b/extend/catcher/command/install/DisableModuleCommand.php @@ -12,6 +12,7 @@ namespace catcher\command\install; use catchAdmin\permissions\model\Permissions; use catcher\CatchAdmin; +use catcher\library\InstallLocalModule; use think\console\Command; use think\console\Input; use think\console\input\Argument; @@ -34,10 +35,7 @@ class DisableModuleCommand extends Command if (empty(CatchAdmin::getModuleInfo(CatchAdmin::directory() .$module))) { $output->error("module [$module] not exist"); } else { - CatchAdmin::disableModule($module); - Permissions::destroy(function ($query) use ($module){ - $query->where('module', trim($module)); - }); + (new InstallLocalModule($module))->disableModule(); $output->info("module [$module] disabled"); } } diff --git a/extend/catcher/command/install/EnableModuleCommand.php b/extend/catcher/command/install/EnableModuleCommand.php index 2a14dc2..d62eb66 100644 --- a/extend/catcher/command/install/EnableModuleCommand.php +++ b/extend/catcher/command/install/EnableModuleCommand.php @@ -12,6 +12,7 @@ namespace catcher\command\install; use catchAdmin\permissions\model\Permissions; use catcher\CatchAdmin; +use catcher\library\InstallLocalModule; use think\console\Command; use think\console\Input; use think\console\input\Argument; @@ -33,8 +34,7 @@ class EnableModuleCommand extends Command if (empty(CatchAdmin::getModuleInfo(CatchAdmin::directory() .$module))) { $output->error("module [$module] not exist"); } else { - CatchAdmin::enableModule($module); - app(Permissions::class)->restore(['module' => trim($module)]); + (new InstallLocalModule($module))->enableModule(); $output->info("module [$module] enabled"); } } diff --git a/extend/catcher/command/install/InstallLocalModuleCommand.php b/extend/catcher/command/install/InstallLocalModuleCommand.php new file mode 100644 index 0000000..a465ea0 --- /dev/null +++ b/extend/catcher/command/install/InstallLocalModuleCommand.php @@ -0,0 +1,68 @@ +setName('local:install') + ->addArgument('module', Argument::REQUIRED, 'module name') + ->setDescription('install catch local module'); + } + + protected function execute(Input $input, Output $output) + { + $installedModule = $input->getArgument('module'); + + $install = new InstallLocalModule($installedModule); + + if (!$install->localModuleExist()) { + while (true) { + $modules = $install->getLocalModulesInfo(false); + if (!count($modules)) { + $output->error('Input module not found and All local modules had been enabled');exit; + } + $choose = ''; + $i = 1; + foreach ($modules as $k => $module) { + $choose .= ($i++) . ':' . ($module['name']) . ($module['enable'] ? '(开启)' : '(未开启)') . PHP_EOL; + } + $answer = $output->ask($input, $choose); + if (isset($modules[$answer-1])) { + $installedModule = $modules[$answer - 1]['name']; + break; + } + } + } + + $install = new InstallLocalModule($installedModule); + + if (!$install->isModuleEnabled()) { + $output->error($installedModule . ' has been enabled!'); + exit; + } + + if (!$install->done()) { + $output->error(sprintf('module [%s] has been installed, You can use [php think enable:module $module] to start it.', $installedModule)); + } + + $output->info(sprintf('module [%s] installed successfully', $installedModule)); + } +} \ No newline at end of file diff --git a/extend/catcher/command/install/InstallProjectCommand.php b/extend/catcher/command/install/InstallProjectCommand.php index d80a174..e649d4b 100644 --- a/extend/catcher/command/install/InstallProjectCommand.php +++ b/extend/catcher/command/install/InstallProjectCommand.php @@ -2,6 +2,7 @@ namespace catcher\command\install; use catcher\CatchAdmin; +use catcher\library\InstallLocalModule; use think\console\Command; use think\console\Input; use think\console\input\Option; @@ -199,17 +200,12 @@ class InstallProjectCommand extends Command */ protected function migrateAndSeeds(): void { - foreach (CatchAdmin::getModulesDirectory() as $directory) { - $moduleInfo = CatchAdmin::getModuleInfo($directory); - if (!empty($moduleInfo) && is_dir(CatchAdmin::moduleMigrationsDirectory($moduleInfo['alias']))) { - if (in_array($moduleInfo['alias'], $this->defaultModule)) { - $output = Console::call('catch-migrate:run', [$moduleInfo['alias']]); - $this->output->info(sprintf('module [%s] migrations %s', $moduleInfo['alias'], $output->fetch())); - $seedOut = Console::call('catch-seed:run', [$moduleInfo['alias']]); - $this->output->info(sprintf('module [%s] seeds %s', $moduleInfo['alias'], $seedOut->fetch())); - } - } + foreach ($this->defaultModule as $m) { + $module = new InstallLocalModule($m); + $module->installModuleTables(); + $module->installModuleSeeds(); + $this->output->info('🎉 module [' . $m . '] installed successfully'); } } @@ -221,15 +217,11 @@ class InstallProjectCommand extends Command */ protected function migrateRollback() { - foreach (CatchAdmin::getModulesDirectory() as $directory) { - $moduleInfo = CatchAdmin::getModuleInfo($directory); - if (!empty($moduleInfo) && is_dir(CatchAdmin::moduleMigrationsDirectory($moduleInfo['alias']))) { - if (in_array($moduleInfo['alias'], $this->defaultModule)) { - $rollbackOut = Console::call('catch-migrate:rollback', [$moduleInfo['alias'], '-f']); - // $this->output->info(sprintf('module [%s] [%s] rollback %s', $moduleInfo['alias'], basename($migration), $rollbackOut->fetch())); - } + foreach ($this->defaultModule as $m) { + $module = new InstallLocalModule($m); + $module->rollbackModuleTable(); + $this->output->info('🎉' . $m . ' tables rollback successfully'); } - } } /** diff --git a/extend/catcher/library/InstallLocalModule.php b/extend/catcher/library/InstallLocalModule.php new file mode 100644 index 0000000..88472dc --- /dev/null +++ b/extend/catcher/library/InstallLocalModule.php @@ -0,0 +1,176 @@ +module = $module; + } + + /** + * 查找 + * + * @time 2020年09月10日 + * @return bool + */ + public function done() + { + if ($this->findModuleInPermissions()) { + return false; + } else { + $this->installModuleTables(); + $this->installModuleSeeds(); + return true; + } + } + + /** + * 本地模块是否存在 + * + * @time 2020年09月10日 + * @return bool + */ + public function localModuleExist() + { + return in_array($this->module, array_column(CatchAdmin::getModulesInfo(true), 'value')); + } + + + /** + * 模块是否开启 + * + * @time 2020年09月10日 + * @return false|mixed + */ + public function isModuleEnabled() + { + return in_array($this->module, array_column($this->getLocalModulesInfo(false), 'name')); + } + + /** + * 获取本地模块信息 + * + * @param bool $enabled + * @time 2020年09月10日 + * @return array + */ + public function getLocalModulesInfo($enabled = true) + { + $modules = CatchAdmin::getModulesInfo(true); + + $info = []; + foreach ($modules as $module) { + $moduleInfo = CatchAdmin::getModuleInfo(CatchAdmin::directory() . $module['value']); + // 获取全部 + if ($enabled) { + $info[] = [ + 'name' => $module['value'], + 'title' => $module['title'], + 'enable' => $moduleInfo['enable'], + ]; + } else { + // 获取未开启的 + if (!$moduleInfo['enable']) { + $info[] = [ + 'name' => $module['value'], + 'title' => $module['title'], + 'enable' => $moduleInfo['enable'], + ]; + } + } + } + + return $info; + } + + /** + * 查找模块 + * + * @time 2020年09月10日 + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @return bool + */ + protected function findModuleInPermissions() + { + return Permissions::where('module', $this->module)->find() ? true : false; + } + + /** + * 启用模块 + * + * @time 2020年09月10日 + * @return void + */ + public function enableModule() + { + CatchAdmin::enableModule($this->module); + app(Permissions::class)->restore(['module' => trim($this->module)]); + } + + /** + * 禁用模块 + * + * @time 2020年09月10日 + * @return void + */ + public function disableModule() + { + CatchAdmin::disableModule($this->module); + + Permissions::destroy(function ($query) { + $query->where('module', trim($this->module)); + }); + } + + /** + * 创建模块表 + * + * @time 2020年09月10日 + * @return void + */ + public function installModuleTables() + { + Console::call('catch-migrate:run', [$this->module]); + } + + /** + * 初始化模块数据 + * + * @time 2020年09月10日 + * @return void + */ + public function installModuleSeeds() + { + Console::call('catch-seed:run', [$this->module]); + } + + /** + * 回滚模块表 + * + * @time 2020年09月10日 + * @return void + */ + public function rollbackModuleTable() + { + Console::call('catch-migrate:rollback', [$this->module, '-f']); + } +} \ No newline at end of file