Ver código fonte

feat: 新增命令generate:installed-sixshop type改为library

runphp 1 mês atrás
pai
commit
0c333caae7

+ 2 - 4
composer.json

@@ -1,7 +1,7 @@
 {
   "name": "six-shop/core",
   "description": "核心扩展",
-  "type": "composer-plugin",
+  "type": "library",
   "keywords": [
     "sixshop",
     "thinkphp"
@@ -21,7 +21,6 @@
   ],
   "require": {
     "php": ">=8.3",
-    "composer-plugin-api": "^2.0",
     "composer/composer": "^2.9.2",
     "polyfills/array-first-array-last": "^1.0.1",
     "topthink/framework": "^8.1",
@@ -38,7 +37,6 @@
       "services": [
         "SixShop\\Core\\Service\\CoreService"
       ]
-    },
-    "class": "SixShop\\Core\\Plugin"
+    }
   }
 }

+ 40 - 0
src/Command/GenerateInstalledSixShopCommand.php

@@ -0,0 +1,40 @@
+<?php
+
+declare(strict_types=1);
+
+namespace SixShop\Core\Command;
+
+use Composer\InstalledVersions;
+use SixShop\Core\Plugin;
+use think\console\Command;
+
+class GenerateInstalledSixShopCommand extends Command
+{
+    public function configure(): void
+    {
+        $this->setName('generate:installed-sixshop')
+            ->setDescription('Generate vendor/composer/installedSixShop.php');
+    }
+
+    public function handle(): void
+    {
+        $installedSixShopExtensions = [
+            'root' => [],
+            'versions' => []
+        ];
+        $referenceMap = [];
+        foreach (InstalledVersions::getAllRawData() as $installed) {
+            foreach ($installed['versions'] as $name => $package) {
+                if (isset($package['type']) && $package['type'] === Plugin::EXTENSION_TYPE) {
+                    $installedSixShopExtensions['versions'][$name] = $package;
+                    $referenceMap[$name] = $package['reference'];
+                }
+            }
+        }
+        $installedSixShopExtensions['root']['reference'] = hash('md5', json_encode($referenceMap));
+
+        $filePath = dirname(InstalledVersions::getInstallPath('composer/composer'), 2) . '/installedSixShop.php';
+        file_put_contents($filePath, '<?php return ' . var_export($installedSixShopExtensions, true) . ';');
+        $this->output->writeln("Generated: $filePath");
+    }
+}

+ 1 - 46
src/Plugin.php

@@ -4,28 +4,15 @@ declare(strict_types=1);
 
 namespace SixShop\Core;
 
-use Composer\Composer;
-use Composer\EventDispatcher\EventSubscriberInterface;
 use Composer\InstalledVersions;
-use Composer\IO\IOInterface;
-use Composer\Plugin\PluginInterface;
-use Composer\Script\Event;
-use Composer\Script\ScriptEvents;
 use RuntimeException;
 
-class Plugin implements PluginInterface, EventSubscriberInterface
+class Plugin
 {
     public const string EXTENSION_TYPE = 'sixshop-extension';
 
     public static array $installedSixShopExtensions = [];
 
-    public static function getSubscribedEvents(): array
-    {
-        return [
-            ScriptEvents::POST_AUTOLOAD_DUMP => 'onPostAutoloadDump',
-        ];
-    }
-
     /**
      * @return array{root: array{reference: string}, versions: array<string, array>}
      */
@@ -41,36 +28,4 @@ class Plugin implements PluginInterface, EventSubscriberInterface
         }
         throw new RuntimeException('Please run "composer dump-autoload" to generate installedSixShop.php');
     }
-
-    public function activate(Composer $composer, IOInterface $io): void
-    {
-    }
-
-    public function deactivate(Composer $composer, IOInterface $io): void
-    {
-    }
-
-    public function uninstall(Composer $composer, IOInterface $io): void
-    {
-    }
-
-    public function onPostAutoloadDump(Event $event): void
-    {
-        $installedSixShopExtensions = [
-            'root' => [],
-            'versions' => []
-        ];
-        $referenceMap = [];
-        foreach (InstalledVersions::getAllRawData() as $installed) {
-            foreach ($installed['versions'] as $name => $package) {
-                if (isset($package['type']) && $package['type'] === self::EXTENSION_TYPE) {
-                    $installedSixShopExtensions['versions'][$name] = $package;
-                    $referenceMap[$name] = $package['reference'];
-                }
-            }
-        }
-        $installedSixShopExtensions['root']['reference'] = hash('md5', json_encode($referenceMap));
-        $filePath = $event->getComposer()->getConfig()->get('vendor-dir') . '/composer/installedSixShop.php';
-        file_put_contents($filePath, '<?php return ' . var_export($installedSixShopExtensions, true) . ';');
-    }
 }

+ 4 - 0
src/Service/CoreService.php

@@ -8,6 +8,7 @@ use Composer\InstalledVersions;
 use Composer\Json\JsonFile;
 use ReflectionException;
 use Seld\JsonLint\ParsingException;
+use SixShop\Core\Command\GenerateInstalledSixShopCommand;
 use SixShop\Core\Exception\ExceptionHandle;
 use SixShop\Core\Plugin;
 use SixShop\Core\Request;
@@ -132,6 +133,9 @@ class CoreService extends Service
             $this->registerRoutes($this->app->make(RegisterRouteService::class)->init());
         });
 
+        $this->commands([
+            GenerateInstalledSixShopCommand::class,
+        ]);
         $this->app->make(CommandService::class)->init(function ($commands) {
             $this->commands($commands);
         });