| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- 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
- {
- 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>}
- */
- public static function getInstalledSixShopExtensions(): array
- {
- if (self::$installedSixShopExtensions) {
- return self::$installedSixShopExtensions;
- }
- $composerDir = dirname(InstalledVersions::getInstallPath('composer/composer'),2);
- $filePath = $composerDir . '/installedSixShop.php';
- if (file_exists($filePath)) {
- return self::$installedSixShopExtensions = require $filePath;
- }
- throw new RuntimeException('Please run "composer dump-autoload" to generate installedSixShop.php');
- }
- public function activate(Composer $composer, IOInterface $io): void
- {
- $installer = new ExtensionInstaller($io, $composer, self::EXTENSION_TYPE);
- $composer->getInstallationManager()->addInstaller($installer);
- }
- 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) . ';');
- }
- }
|