Browse Source

:construction: 本地安装功能优化

jqh 4 years ago
parent
commit
c04d1d55b3
2 changed files with 24 additions and 17 deletions
  1. 20 6
      src/Extend/Manager.php
  2. 4 11
      src/Http/Forms/InstallFromLocal.php

+ 20 - 6
src/Extend/Manager.php

@@ -113,7 +113,7 @@ class Manager
     /**
      * 加载扩展,注册自动加载规则.
      *
-     * @return void
+     * @return $this
      */
     public function load()
     {
@@ -124,6 +124,8 @@ class Manager
                 $this->reportException($e);
             }
         }
+
+        return $this;
     }
 
     /**
@@ -333,22 +335,30 @@ class Manager
 
     /**
      * 解压缩扩展包.
+     *
+     * @param string $filePath
+     * @param bool   $force
+     *
+     * @return string
      */
-    public function extract($filePath)
+    public function extract($filePath, bool $force = false)
     {
         $filePath = is_file($filePath) ? $filePath : $this->getFilePath($filePath);
 
-        $this->extractZip($filePath);
+        $name = $this->extractZip($filePath, $force);
 
         @unlink($filePath);
+
+        return $name;
     }
 
     /**
      * @param string $filePath
+     * @param bool   $force
      *
      * @return bool
      */
-    public function extractZip($filePath)
+    public function extractZip($filePath, bool $force = false)
     {
         // 创建临时目录.
         $tempPath = $this->makeTempDirectory();
@@ -382,13 +392,17 @@ class Manager
 
             $extensionDir = admin_extension_path($composerProperty->name);
 
-            if (is_dir($extensionDir)) {
+            if (! $force && is_dir($extensionDir)) {
                 throw new RuntimeException(sprintf('The extension [%s] already exist!', $composerProperty->name));
             }
 
-            $this->files->makeDirectory($extensionDir, 0755, true);
+            if (! is_dir($extensionDir)) {
+                $this->files->makeDirectory($extensionDir, 0755, true);
+            }
 
             $this->files->copyDirectory($directory, $extensionDir);
+
+            return $composerProperty->name;
         } finally {
             $this->files->deleteDirectory($tempPath);
         }

+ 4 - 11
src/Http/Forms/InstallFromLocal.php

@@ -25,23 +25,16 @@ class InstallFromLocal extends Form implements LazyRenderable
 
             $manager = Admin::extension();
 
-            $allNames = $manager->all()->keys()->toArray();
+            $extensionName = $manager->extract($path, true);
 
-            $manager->extract($path);
-
-            $manager->load();
-
-            $newAllNames = $manager->all()->keys()->toArray();
-
-            $diff = array_diff($newAllNames, $allNames);
-
-            if (! $diff) {
+            if (! $extensionName) {
                 return $this->response()->error(trans('admin.invalid_extension_package'));
             }
 
             $manager
+                ->load()
                 ->updateManager()
-                ->update(current($diff));
+                ->update($extensionName);
 
             return $this->response()
                 ->success(implode('<br>', $manager->updateManager()->notes))