新增安装模块命令
This commit is contained in:
parent
b9c156e5a7
commit
7adfc93190
@ -10,52 +10,187 @@
|
||||
*/
|
||||
namespace catcher\command\install;
|
||||
|
||||
use catcher\CatchAdmin;
|
||||
use catcher\exceptions\FailedException;
|
||||
use catcher\facade\Http;
|
||||
use catcher\library\Compress;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Argument;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\facade\Console;
|
||||
|
||||
class InstallCatchModuleCommand extends Command
|
||||
{
|
||||
protected $module;
|
||||
|
||||
protected $moduleZipPath;
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('install:module')
|
||||
->addArgument('module', Argument::REQUIRED, 'module name')
|
||||
->addOption('mode', '-r',Option::VALUE_NONE, 'reinstall back')
|
||||
->setDescription('install catch module');
|
||||
}
|
||||
|
||||
protected function execute(Input $input, Output $output)
|
||||
{
|
||||
$this->module = $input->getArgument('module');
|
||||
|
||||
$this->moduleZipPath = $this->installRootPath() . $this->module .'.zip';
|
||||
|
||||
try {
|
||||
if ($this->download()) {
|
||||
if ($this->install()) {
|
||||
$this->installComposerPackage();
|
||||
$this->createTable();
|
||||
$this->importData();
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$this->rollback();
|
||||
exit($output->error($exception->getMessage()));
|
||||
}
|
||||
|
||||
$output->info("install module [$this->module] successfully");
|
||||
}
|
||||
|
||||
protected function searchModule()
|
||||
{
|
||||
|
||||
return 'http://api.catchadmin.com/hello.zip';
|
||||
}
|
||||
|
||||
protected function getModule()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 下载扩展包
|
||||
*
|
||||
* @return bool
|
||||
* @author JaguarJack <njphper@gmail.com>
|
||||
* @date 2020/7/11
|
||||
*/
|
||||
protected function download()
|
||||
{
|
||||
if (!(new Compress())->savePath($this->moduleZipPath)->download($this->module, $this->searchModule())) {
|
||||
throw new FailedException('download module '.$this->module. ' failed');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 安装模块
|
||||
*
|
||||
* @author JaguarJack <njphper@gmail.com>
|
||||
* @date 2020/7/11
|
||||
*/
|
||||
protected function install()
|
||||
{
|
||||
if ($this->isFirstInstall()) {
|
||||
$zip = new \ZipArchive();
|
||||
$res = $zip->open($this->moduleZipPath);
|
||||
if ($res === true) {
|
||||
$zip->extractTo($this->installRootPath());
|
||||
$zip->close();
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (!(new Compress())->update($this->module)) {
|
||||
throw new FailedException('install module ' . $this->module . ' failed');
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected function installComposerPackage()
|
||||
{
|
||||
try {
|
||||
$moduleInfo = \json_decode(file_get_contents($this->installPath() . $this->module . DIRECTORY_SEPARATOR . 'module.json'), true);
|
||||
$requires = $moduleInfo['requires'];
|
||||
foreach ($requires as $require) {
|
||||
exec(sprintf('composer require "%s"', $require));
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
throw new FailedException($exception->getMessage());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建表
|
||||
*
|
||||
* @author JaguarJack <njphper@gmail.com>
|
||||
* @date 2020/7/11
|
||||
*/
|
||||
protected function createTable()
|
||||
{
|
||||
Console::call('catch-migrate:run', [$this->module]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导入数据
|
||||
*
|
||||
* @author JaguarJack <njphper@gmail.com>
|
||||
* @date 2020/7/11
|
||||
*/
|
||||
protected function importData()
|
||||
{
|
||||
Console::call('catch-seed:run', [$this->module]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 回滚
|
||||
*
|
||||
* @author JaguarJack <njphper@gmail.com>
|
||||
* @date 2020/7/11
|
||||
*/
|
||||
protected function rollback()
|
||||
{
|
||||
(new Compress())->rmDir($this->installPath() . $this->module);
|
||||
|
||||
Console::call('catch-migrate:rollback', [$this->module, '-f']);
|
||||
}
|
||||
|
||||
protected function update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 那安装根目录
|
||||
*
|
||||
* @return string
|
||||
* @author JaguarJack <njphper@gmail.com>
|
||||
* @date 2020/7/11
|
||||
*/
|
||||
protected function installRootPath()
|
||||
{
|
||||
return CatchAdmin::directory();
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装的模块目录
|
||||
*
|
||||
* @return string
|
||||
* @author JaguarJack <njphper@gmail.com>
|
||||
* @date 2020/7/11
|
||||
*/
|
||||
protected function installPath()
|
||||
{
|
||||
return CatchAdmin::moduleDirectory($this->module);
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否第一次安装
|
||||
*
|
||||
* @return bool
|
||||
* @author JaguarJack <njphper@gmail.com>
|
||||
* @date 2020/7/11
|
||||
*/
|
||||
protected function isFirstInstall()
|
||||
{
|
||||
return !is_dir($this->installRootPath() . $this->module);
|
||||
}
|
||||
}
|
||||
|
@ -329,7 +329,7 @@ class InstallProjectCommand extends Command
|
||||
版本: %s
|
||||
初始账号: admin@gmail.com
|
||||
初始密码: admin
|
||||
', CatchAdmin::VERSION, $year));
|
||||
', $year, CatchAdmin::VERSION));
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,7 @@ class Compress
|
||||
*
|
||||
* @time 2020年04月30日
|
||||
* @param $moduleName
|
||||
* @return void
|
||||
* @return bool
|
||||
*/
|
||||
public function update($moduleName)
|
||||
{
|
||||
@ -88,7 +88,10 @@ class Compress
|
||||
} catch (\Exception $exception) {
|
||||
$this->moduleUnzip($moduleName, $backupPath);
|
||||
$this->rmDir($this->getModuleBackupPath($moduleName));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,7 +111,6 @@ class Compress
|
||||
CatchAdmin::makeDirectory($tempExtractToPath);
|
||||
// 下载 zip 包
|
||||
$res = $zip->open($zipPath);
|
||||
|
||||
if ($res === true) {
|
||||
$zip->extractTo($tempExtractToPath);
|
||||
$zip->close();
|
||||
|
Loading…
x
Reference in New Issue
Block a user