update:更新安装&新增本地模块安装

This commit is contained in:
JaguarJack 2020-09-11 07:42:38 +08:00
parent 818ffb2ce6
commit 40276babfb
5 changed files with 258 additions and 24 deletions

View File

@ -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");
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1,68 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catcher\command\install;
use catcher\library\InstallLocalModule;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
class InstallLocalModuleCommand extends Command
{
protected function configure()
{
$this->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));
}
}

View File

@ -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,14 +217,10 @@ 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');
}
}

View File

@ -0,0 +1,176 @@
<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catcher\library;
use catchAdmin\permissions\model\Permissions;
use catcher\CatchAdmin;
use think\facade\Console;
class InstallLocalModule
{
protected $module;
public function __construct($module)
{
$this->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']);
}
}