From 1e8b5510f16a473023b809c36ba35af53b905c7b Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Sat, 11 Jul 2020 21:44:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=BF=9C=E7=A8=8B=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../install/InstallCatchModuleCommand.php | 63 ++++++++++++++----- extend/catcher/library/Compress.php | 10 ++- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/extend/catcher/command/install/InstallCatchModuleCommand.php b/extend/catcher/command/install/InstallCatchModuleCommand.php index 6a979ec..687e062 100644 --- a/extend/catcher/command/install/InstallCatchModuleCommand.php +++ b/extend/catcher/command/install/InstallCatchModuleCommand.php @@ -27,6 +27,11 @@ class InstallCatchModuleCommand extends Command protected $moduleZipPath; + /** + * @var Compress + */ + protected $compress; + protected function configure() { $this->setName('install:module') @@ -40,8 +45,9 @@ class InstallCatchModuleCommand extends Command $this->moduleZipPath = $this->installRootPath() . $this->module .'.zip'; + $this->compress = new Compress(); try { - if ($this->download()) { + if ($this->download($this->searchModule())) { if ($this->install()) { $this->installComposerPackage(); $this->createTable(); @@ -58,23 +64,26 @@ class InstallCatchModuleCommand extends Command protected function searchModule() { + $this->output->info('find module zip'); return 'http://api.catchadmin.com/hello.zip'; } /** * 下载扩展包 * + * @param $resourceUrl * @return bool * @author JaguarJack * @date 2020/7/11 */ - protected function download() + protected function download($resourceUrl) { - if (!(new Compress())->savePath($this->moduleZipPath)->download($this->module, $this->searchModule())) { - throw new FailedException('download module '.$this->module. ' failed'); - } - - return true; + $this->output->info('download module zip'); + if (!$this->compress->savePath($this->moduleZipPath)->download($resourceUrl)) { + throw new FailedException('download module ' . $this->module . ' failed'); + } + $this->output->info('download module zip successfully'); + return true; } /** @@ -91,12 +100,13 @@ class InstallCatchModuleCommand extends Command if ($res === true) { $zip->extractTo($this->installRootPath()); $zip->close(); - return true; } + $this->output->info('install module successfully'); } else { - if (!(new Compress())->update($this->module)) { + if (!$this->compress->update($this->module)) { throw new FailedException('install module ' . $this->module . ' failed'); } + $this->output->info('update module successfully'); } return true; @@ -106,11 +116,19 @@ class InstallCatchModuleCommand extends Command 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)); - } + if (file_exists($this->installPath() . 'module.json')) { + $moduleInfo = \json_decode(file_get_contents($this->installPath() . 'module.json'), true); + $requires = $moduleInfo['requires']; + 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) { throw new FailedException($exception->getMessage()); } @@ -118,6 +136,21 @@ class InstallCatchModuleCommand extends Command return true; } + /** + * 是否安装 + * + * @param $package + * @return array|bool + * @author JaguarJack + * @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() { - (new Compress())->rmDir($this->installPath() . $this->module); + (new Compress())->rmDir($this->installPath()); Console::call('catch-migrate:rollback', [$this->module, '-f']); } diff --git a/extend/catcher/library/Compress.php b/extend/catcher/library/Compress.php index 1a79aab..93cef3f 100644 --- a/extend/catcher/library/Compress.php +++ b/extend/catcher/library/Compress.php @@ -57,14 +57,13 @@ class Compress * * @time 2020年04月30日 * @param $remotePackageUrl - * @param $moduleName * @return string */ - public function download($moduleName, $remotePackageUrl = '') + public function download($remotePackageUrl = '') { - $response = Http::timeout(5) + $response = Http::timeout(10) ->options([ - 'save_to' => stream_for(fopen($this->savePath, 'w+')) + 'save_to' => stream_for(fopen($this->savePath, 'w+')) ]) ->get($remotePackageUrl); @@ -80,11 +79,10 @@ class Compress */ public function update($moduleName) { - $moduleZip = $this->download($moduleName); // 备份 $backupPath = $this->backup($moduleName); try { - $this->moduleUnzip($moduleName, $moduleZip); + $this->moduleUnzip($moduleName, $this->savePath); } catch (\Exception $exception) { $this->moduleUnzip($moduleName, $backupPath); $this->rmDir($this->getModuleBackupPath($moduleName));