修复远程下载模块

This commit is contained in:
JaguarJack 2020-07-11 21:44:39 +08:00
parent 7adfc93190
commit 1e8b5510f1
2 changed files with 52 additions and 21 deletions

View File

@ -27,6 +27,11 @@ class InstallCatchModuleCommand extends Command
protected $moduleZipPath; protected $moduleZipPath;
/**
* @var Compress
*/
protected $compress;
protected function configure() protected function configure()
{ {
$this->setName('install:module') $this->setName('install:module')
@ -40,8 +45,9 @@ class InstallCatchModuleCommand extends Command
$this->moduleZipPath = $this->installRootPath() . $this->module .'.zip'; $this->moduleZipPath = $this->installRootPath() . $this->module .'.zip';
$this->compress = new Compress();
try { try {
if ($this->download()) { if ($this->download($this->searchModule())) {
if ($this->install()) { if ($this->install()) {
$this->installComposerPackage(); $this->installComposerPackage();
$this->createTable(); $this->createTable();
@ -58,23 +64,26 @@ class InstallCatchModuleCommand extends Command
protected function searchModule() protected function searchModule()
{ {
$this->output->info('find module zip');
return 'http://api.catchadmin.com/hello.zip'; return 'http://api.catchadmin.com/hello.zip';
} }
/** /**
* 下载扩展包 * 下载扩展包
* *
* @param $resourceUrl
* @return bool * @return bool
* @author JaguarJack <njphper@gmail.com> * @author JaguarJack <njphper@gmail.com>
* @date 2020/7/11 * @date 2020/7/11
*/ */
protected function download() protected function download($resourceUrl)
{ {
if (!(new Compress())->savePath($this->moduleZipPath)->download($this->module, $this->searchModule())) { $this->output->info('download module zip');
throw new FailedException('download module '.$this->module. ' failed'); if (!$this->compress->savePath($this->moduleZipPath)->download($resourceUrl)) {
} throw new FailedException('download module ' . $this->module . ' failed');
}
return true; $this->output->info('download module zip successfully');
return true;
} }
/** /**
@ -91,12 +100,13 @@ class InstallCatchModuleCommand extends Command
if ($res === true) { if ($res === true) {
$zip->extractTo($this->installRootPath()); $zip->extractTo($this->installRootPath());
$zip->close(); $zip->close();
return true;
} }
$this->output->info('install module successfully');
} else { } else {
if (!(new Compress())->update($this->module)) { if (!$this->compress->update($this->module)) {
throw new FailedException('install module ' . $this->module . ' failed'); throw new FailedException('install module ' . $this->module . ' failed');
} }
$this->output->info('update module successfully');
} }
return true; return true;
@ -106,11 +116,19 @@ class InstallCatchModuleCommand extends Command
protected function installComposerPackage() protected function installComposerPackage()
{ {
try { try {
$moduleInfo = \json_decode(file_get_contents($this->installPath() . $this->module . DIRECTORY_SEPARATOR . 'module.json'), true); if (file_exists($this->installPath() . 'module.json')) {
$requires = $moduleInfo['requires']; $moduleInfo = \json_decode(file_get_contents($this->installPath() . 'module.json'), true);
foreach ($requires as $require) { $requires = $moduleInfo['requires'];
exec(sprintf('composer require "%s"', $require)); if (count($requires)) {
} foreach ($requires as $require) {
list($package, $version) = explode(':', $require);
if (!$this->isInstalledProjectComposerPackage($package)) {
exec(sprintf('composer require "%s"', $require));
$this->output->info('install composer package ['.$package.']');
}
}
}
}
} catch (\Exception $exception) { } catch (\Exception $exception) {
throw new FailedException($exception->getMessage()); throw new FailedException($exception->getMessage());
} }
@ -118,6 +136,21 @@ class InstallCatchModuleCommand extends Command
return true; return true;
} }
/**
* 是否安装
*
* @param $package
* @return array|bool
* @author JaguarJack <njphper@gmail.com>
* @date 2020/7/11
*/
protected function isInstalledProjectComposerPackage($package)
{
$composer = \json_decode(file_get_contents(root_path() . 'composer.json'), true);
return in_array($package, array_keys($composer['require']));
}
/** /**
* 创建表 * 创建表
* *
@ -148,7 +181,7 @@ class InstallCatchModuleCommand extends Command
*/ */
protected function rollback() protected function rollback()
{ {
(new Compress())->rmDir($this->installPath() . $this->module); (new Compress())->rmDir($this->installPath());
Console::call('catch-migrate:rollback', [$this->module, '-f']); Console::call('catch-migrate:rollback', [$this->module, '-f']);
} }

View File

@ -57,14 +57,13 @@ class Compress
* *
* @time 2020年04月30日 * @time 2020年04月30日
* @param $remotePackageUrl * @param $remotePackageUrl
* @param $moduleName
* @return string * @return string
*/ */
public function download($moduleName, $remotePackageUrl = '') public function download($remotePackageUrl = '')
{ {
$response = Http::timeout(5) $response = Http::timeout(10)
->options([ ->options([
'save_to' => stream_for(fopen($this->savePath, 'w+')) 'save_to' => stream_for(fopen($this->savePath, 'w+'))
]) ])
->get($remotePackageUrl); ->get($remotePackageUrl);
@ -80,11 +79,10 @@ class Compress
*/ */
public function update($moduleName) public function update($moduleName)
{ {
$moduleZip = $this->download($moduleName);
// 备份 // 备份
$backupPath = $this->backup($moduleName); $backupPath = $this->backup($moduleName);
try { try {
$this->moduleUnzip($moduleName, $moduleZip); $this->moduleUnzip($moduleName, $this->savePath);
} catch (\Exception $exception) { } catch (\Exception $exception) {
$this->moduleUnzip($moduleName, $backupPath); $this->moduleUnzip($moduleName, $backupPath);
$this->rmDir($this->getModuleBackupPath($moduleName)); $this->rmDir($this->getModuleBackupPath($moduleName));