Răsfoiți Sursa

feat(installer): 优化扩展安装流程并添加预安装检查

- 修改 ExtensionInstaller 类,使用包的 extra.sixshop.id 作为安装路径
- 在 Plugin 类中添加 onPrePackageInstall 事件处理方法
- 增加对扩展包配置的验证,确保包含 sixshop 相关信息
- 优化错误处理,提高安装过程的稳定性和可靠性
runphp 7 luni în urmă
părinte
comite
bb45cac3cc
2 a modificat fișierele cu 27 adăugiri și 10 ștergeri
  1. 9 4
      src/ExtensionInstaller.php
  2. 18 6
      src/Plugin.php

+ 9 - 4
src/ExtensionInstaller.php

@@ -1,5 +1,6 @@
 <?php
 declare(strict_types=1);
+
 namespace SixShop\Core;
 
 use Composer\Installer\LibraryInstaller;
@@ -7,13 +8,17 @@ use Composer\Package\PackageInterface;
 
 class ExtensionInstaller extends LibraryInstaller
 {
-    public function supports(string $packageType)
+    public function supports(string $packageType): bool
     {
-        return $packageType === 'sixshop-extension';
+        return $packageType === $this->type;
     }
 
-    public function getInstallPath(PackageInterface $package)
+    public function getInstallPath(PackageInterface $package): string
     {
-        return 'extension/' . $package->getPrettyName();
+        if (!isset($package->getExtra()['sixshop']['id'])) {
+            throw new \InvalidArgumentException('Extension id not found in extra.sixshop.id');
+        }
+        $id = $package->getExtra()['sixshop']['id'];
+        return 'extension/' . $id;
     }
 }

+ 18 - 6
src/Plugin.php

@@ -5,37 +5,49 @@ namespace SixShop\Core;
 
 use Composer\Composer;
 use Composer\EventDispatcher\EventSubscriberInterface;
-use Composer\Installer\LibraryInstaller;
 use Composer\Installer\PackageEvent;
 use Composer\Installer\PackageEvents;
 use Composer\IO\IOInterface;
-use Composer\Package\PackageInterface;
 use Composer\Plugin\PluginInterface;
 
 class Plugin implements PluginInterface, EventSubscriberInterface
 {
 
 
-    public function activate(Composer $composer, IOInterface $io)
+    public function activate(Composer $composer, IOInterface $io): void
     {
         $installer = new ExtensionInstaller($io, $composer, 'sixshop-extension');
         $composer->getInstallationManager()->addInstaller($installer);
     }
 
 
-    public static function getSubscribedEvents()
+    public static function getSubscribedEvents(): array
     {
         return [
+            PackageEvents::PRE_PACKAGE_INSTALL => 'onPrePackageInstall',
             PackageEvents::POST_PACKAGE_INSTALL => 'onPostPackageInstall',
         ];
     }
 
-    public function onPostPackageInstall(PackageEvent $event, IOInterface $io)
+    public function onPrePackageInstall(PackageEvent $event): bool
     {
         $package = $event->getOperation()->getPackage();
-        $io->write('Installing sixshop extension ' . $package->getPrettyName());
+        $extra = $package->getExtra();
+        if (!isset($extra['sixshop'])) {
+            throw new \RuntimeException('SixShop extension must have "sixshop" extra section.');
+        }
+        if (!isset($extra['sixshop']['id']) || !isset($extra['sixshop']['class'])) {
+            throw new \RuntimeException('Invalid sixshop extension configuration');
+        }
+        return true;
     }
 
+    public function onPostPackageInstall(PackageEvent $event): bool
+    {
+        return true;
+    }
+
+
     public function deactivate(Composer $composer, IOInterface $io)
     {
         // TODO: Implement deactivate() method.