2020-02-26 09:06:35 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @filename InstallCatchModuleCommand.php
|
|
|
|
* @createdAt 2020/2/24
|
|
|
|
* @project https://github.com/yanwenwu/catch-admin
|
|
|
|
* @document http://doc.catchadmin.com
|
|
|
|
* @author JaguarJack <njphper@gmail.com>
|
|
|
|
* @copyright By CatchAdmin
|
|
|
|
* @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
|
|
|
|
*/
|
2020-06-20 08:50:21 +08:00
|
|
|
namespace catcher\command\install;
|
2020-02-26 09:06:35 +08:00
|
|
|
|
2020-07-11 16:13:06 +08:00
|
|
|
use catcher\CatchAdmin;
|
|
|
|
use catcher\exceptions\FailedException;
|
|
|
|
use catcher\facade\Http;
|
|
|
|
use catcher\library\Compress;
|
2020-02-26 09:06:35 +08:00
|
|
|
use think\console\Command;
|
|
|
|
use think\console\Input;
|
|
|
|
use think\console\input\Argument;
|
|
|
|
use think\console\input\Option;
|
|
|
|
use think\console\Output;
|
2020-07-11 16:13:06 +08:00
|
|
|
use think\facade\Console;
|
2020-02-26 09:06:35 +08:00
|
|
|
|
|
|
|
class InstallCatchModuleCommand extends Command
|
|
|
|
{
|
2020-07-11 16:13:06 +08:00
|
|
|
protected $module;
|
|
|
|
|
|
|
|
protected $moduleZipPath;
|
|
|
|
|
2020-02-26 09:06:35 +08:00
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this->setName('install:module')
|
|
|
|
->addArgument('module', Argument::REQUIRED, 'module name')
|
|
|
|
->setDescription('install catch module');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(Input $input, Output $output)
|
|
|
|
{
|
2020-07-11 16:13:06 +08:00
|
|
|
$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");
|
2020-02-26 09:06:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function searchModule()
|
|
|
|
{
|
2020-07-11 16:13:06 +08:00
|
|
|
return 'http://api.catchadmin.com/hello.zip';
|
2020-02-26 09:06:35 +08:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:13:06 +08:00
|
|
|
/**
|
|
|
|
* 下载扩展包
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @author JaguarJack <njphper@gmail.com>
|
|
|
|
* @date 2020/7/11
|
|
|
|
*/
|
|
|
|
protected function download()
|
2020-02-26 09:06:35 +08:00
|
|
|
{
|
2020-07-11 16:13:06 +08:00
|
|
|
if (!(new Compress())->savePath($this->moduleZipPath)->download($this->module, $this->searchModule())) {
|
|
|
|
throw new FailedException('download module '.$this->module. ' failed');
|
|
|
|
}
|
2020-02-26 09:06:35 +08:00
|
|
|
|
2020-07-11 16:13:06 +08:00
|
|
|
return true;
|
2020-02-26 09:06:35 +08:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:13:06 +08:00
|
|
|
/**
|
|
|
|
* 安装模块
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
2020-02-26 09:06:35 +08:00
|
|
|
|
2020-07-11 16:13:06 +08:00
|
|
|
|
|
|
|
protected function installComposerPackage()
|
2020-02-26 09:06:35 +08:00
|
|
|
{
|
2020-07-11 16:13:06 +08:00
|
|
|
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;
|
|
|
|
}
|
2020-02-26 09:06:35 +08:00
|
|
|
|
2020-07-11 16:13:06 +08:00
|
|
|
/**
|
|
|
|
* 创建表
|
|
|
|
*
|
|
|
|
* @author JaguarJack <njphper@gmail.com>
|
|
|
|
* @date 2020/7/11
|
|
|
|
*/
|
|
|
|
protected function createTable()
|
|
|
|
{
|
|
|
|
Console::call('catch-migrate:run', [$this->module]);
|
2020-02-26 09:06:35 +08:00
|
|
|
}
|
|
|
|
|
2020-07-11 16:13:06 +08:00
|
|
|
/**
|
|
|
|
* 导入数据
|
|
|
|
*
|
|
|
|
* @author JaguarJack <njphper@gmail.com>
|
|
|
|
* @date 2020/7/11
|
|
|
|
*/
|
|
|
|
protected function importData()
|
|
|
|
{
|
|
|
|
Console::call('catch-seed:run', [$this->module]);
|
|
|
|
}
|
2020-02-26 09:06:35 +08:00
|
|
|
|
2020-07-11 16:13:06 +08:00
|
|
|
/**
|
|
|
|
* 回滚
|
|
|
|
*
|
|
|
|
* @author JaguarJack <njphper@gmail.com>
|
|
|
|
* @date 2020/7/11
|
|
|
|
*/
|
|
|
|
protected function rollback()
|
2020-02-26 09:06:35 +08:00
|
|
|
{
|
2020-07-11 16:13:06 +08:00
|
|
|
(new Compress())->rmDir($this->installPath() . $this->module);
|
2020-02-26 09:06:35 +08:00
|
|
|
|
2020-07-11 16:13:06 +08:00
|
|
|
Console::call('catch-migrate:rollback', [$this->module, '-f']);
|
2020-02-26 09:06:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function update()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-11 16:13:06 +08:00
|
|
|
/**
|
|
|
|
* 那安装根目录
|
|
|
|
*
|
|
|
|
* @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);
|
|
|
|
}
|
2020-02-26 09:06:35 +08:00
|
|
|
}
|