Просмотр исходного кода

fix(extension): 修复获取插件路径错误

runphp 7 месяцев назад
Родитель
Сommit
711fd89b7b
3 измененных файлов с 22 добавлено и 7 удалено
  1. 11 4
      src/Helper.php
  2. 6 1
      src/Plugin.php
  3. 5 2
      src/Service/CoreService.php

+ 11 - 4
src/Helper.php

@@ -3,7 +3,6 @@ declare(strict_types=1);
 
 namespace SixShop\Core;
 
-use phpDocumentor\Reflection\Types\Self_;
 use SixShop\Core\Exception\LogicException;
 use SixShop\Core\Response\Xml;
 use SixShop\Core\Service\CoreService;
@@ -155,10 +154,18 @@ final class Helper
         return str_shuffle($password);
     }
 
-
-    public static function extension_path(string $module = ''): string
+    public static function extension_path(string $extensionID = ''): string
     {
-        return CoreService::$extensionPath . $module . '/';
+        if (!$extensionID) {
+            return CoreService::$extensionPath;
+        }
+        $extensionName = CoreService::$extensionComposerMap[$extensionID]['name'];
+        $versions = Plugin::getInstalledSixShopExtensions()['versions'];
+
+        return isset($versions[$extensionName]['install_path'])
+            ? realpath($versions[$extensionName]['install_path'])
+            : CoreService::$extensionPath . $extensionID . DIRECTORY_SEPARATOR;
+
     }
 
     public static function extension_name_list()

+ 6 - 1
src/Plugin.php

@@ -16,6 +16,8 @@ class Plugin implements PluginInterface, EventSubscriberInterface
 {
     public const EXTENSION_TYPE = 'sixshop-extension';
 
+    public static array $installedSixShopExtensions = [];
+
     public function activate(Composer $composer, IOInterface $io): void
     {
         $installer = new ExtensionInstaller($io, $composer, self::EXTENSION_TYPE);
@@ -66,10 +68,13 @@ class Plugin implements PluginInterface, EventSubscriberInterface
      */
     public static function getInstalledSixShopExtensions(): array
     {
+        if (self::$installedSixShopExtensions) {
+            return self::$installedSixShopExtensions;
+        }
         $vendorDir = key(ClassLoader::getRegisteredLoaders());
         $filePath = $vendorDir . '/composer/installedSixShop.php';
         if (file_exists($filePath)) {
-            return require $filePath;
+            return self::$installedSixShopExtensions = require $filePath;
         }
         throw new \RuntimeException('Please run "composer dump-autoload" to generate installedSixShop.php');
     }

+ 5 - 2
src/Service/CoreService.php

@@ -18,6 +18,9 @@ class CoreService extends Service
 {
     public static string $extensionPath;
 
+    /**
+     * @var array<string, array{name:string}>
+     */
     public static array $extensionComposerMap = [];
 
     /* @deprecated */
@@ -33,7 +36,7 @@ class CoreService extends Service
             require $this->app->getRootPath() . 'vendor/autoload.php');
         self::$extensionPath = $this->app->getRootPath() . 'extension' . DIRECTORY_SEPARATOR;
         $this->initExtensionList();
-        $this->compatibleExtensNameList();
+        $this->compatibleExtensionNameList();
     }
 
     public function boot(): void
@@ -75,7 +78,7 @@ class CoreService extends Service
     }
 
     /* @deprecated */
-    private function compatibleExtensNameList(): void
+    private function compatibleExtensionNameList(): void
     {
         if (empty(self::$extensionNameList)) {
             self::$extensionNameList = array_keys(self::$extensionComposerMap);