|
7 | 7 | use Composer\Semver\Semver; |
8 | 8 | use NuonicPluginInstaller\Service\IndexFileServiceInterface; |
9 | 9 | use NuonicPluginInstaller\Struct\PackageIndexEntry; |
| 10 | +use Shopware\Core\Framework\Adapter\Cache\CacheValueCompressor; |
10 | 11 | use Shopware\Core\Framework\Context; |
11 | 12 | use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; |
12 | 13 | use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; |
13 | 14 | use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; |
14 | 15 | use Shopware\Core\Framework\Uuid\Uuid; |
| 16 | +use Symfony\Component\Yaml\Yaml; |
| 17 | +use Symfony\Contracts\Cache\CacheInterface; |
| 18 | +use Symfony\Contracts\Cache\ItemInterface; |
| 19 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
15 | 20 |
|
16 | 21 | readonly class LoadPluginAction |
17 | 22 | { |
18 | | - public function __construct( |
19 | | - private EntityRepository $availableOpensourcePluginRepository, |
20 | | - private IndexFileServiceInterface $indexFileService, |
21 | | - ) { |
22 | | - } |
| 23 | + public function __construct( |
| 24 | + private readonly EntityRepository $availableOpensourcePluginRepository, |
| 25 | + private readonly IndexFileServiceInterface $indexFileService, |
| 26 | + private HttpClientInterface $httpClient, |
| 27 | + private readonly CacheInterface $cache, |
| 28 | + private readonly EntityRepository $languageRepository, |
| 29 | + private readonly string $shopwareVersion, |
| 30 | + ) { |
| 31 | + // This is a constructor |
| 32 | + } |
| 33 | + |
| 34 | + public function execute(string|PackageIndexEntry $packageInformation): void |
| 35 | + { |
| 36 | + if (!$packageInformation instanceof PackageIndexEntry) { |
| 37 | + $packageInformation = $this->indexFileService->getPackageInformation($packageInformation); |
| 38 | + } |
| 39 | + |
| 40 | + if (null === $packageInformation) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + $repositoryUrl = $packageInformation->repositoryUrl; |
| 45 | + $ref = $packageInformation->ref; |
| 46 | + |
| 47 | + $packagistData = $this->getPackagistData($ref); |
| 48 | + |
| 49 | + $version = $this->findSuitableVersion($packagistData); |
| 50 | + |
| 51 | + if ($version === null) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + $githubUrl = \str_replace('.git', '', $version['source']['url']); |
| 56 | + $githubUrl = \str_replace('https://github.com/', 'https://raw.githubusercontent.com/', $githubUrl . '/refs/heads/main/'); |
| 57 | + $mainExtensionYmlUrl = $githubUrl . '.shopware-extension.yml'; |
| 58 | + |
| 59 | + $extensionYmlUrl = null; |
| 60 | + |
| 61 | + $isComposer = false; |
| 62 | + |
| 63 | + $mainExtensionYmlResponse = $this->httpClient->request('GET', $mainExtensionYmlUrl); |
| 64 | + if ($mainExtensionYmlResponse->getStatusCode() === 200) { |
| 65 | + $extensionYmlUrl = $mainExtensionYmlUrl; |
| 66 | + } else { |
| 67 | + $isComposer = true; |
| 68 | + } |
| 69 | + |
| 70 | + $pluginData = [ |
| 71 | + 'packageName' => $packageInformation->packageName, |
| 72 | + 'manufacturer' => \implode(', ', \array_column($version['authors'], 'name')), |
| 73 | + 'manufacturerLink' => $version['extra']['manufacturerLink']['en-GB'], |
| 74 | + 'license' => $version['license'][0], |
| 75 | + 'link' => $repositoryUrl, |
| 76 | + 'availableVersion' => $version['version'] |
| 77 | + ]; |
| 78 | + |
| 79 | + |
| 80 | + if (!$isComposer) { |
| 81 | + $extensionYmlResponse = $this->httpClient->request('GET', $extensionYmlUrl); |
| 82 | + if ($extensionYmlResponse->getStatusCode() === 200) { |
| 83 | + $extensionYmlData = Yaml::parse($extensionYmlResponse->getContent()); |
| 84 | + if (isset($extensionYmlData['store'])) { |
| 85 | + if (isset($extensionYmlData['store']['icon'])) { |
| 86 | + $pluginData['icon'] = $githubUrl . '/' . $extensionYmlData['store']['icon']; |
| 87 | + } else { |
| 88 | + $pluginData['icon'] = $githubUrl . '/src/Resources/config/plugin.png'; |
| 89 | + } |
| 90 | + foreach ($extensionYmlData['store']['images'] as $images) { |
| 91 | + $pluginData['images'][] = $githubUrl . '/' . $images['file']; |
| 92 | + } |
| 93 | + if (isset($extensionYmlData['store']['description']['en'])) { |
| 94 | + $pluginData['description']['en-GB'] = $extensionYmlData['store']['description']['en']; |
| 95 | + } |
| 96 | + if (isset($extensionYmlData['store']['description']['de'])) { |
| 97 | + $pluginData['description']['de-DE'] = $extensionYmlData['store']['description']['de']; |
| 98 | + } |
| 99 | + |
| 100 | + // TODO Load from markdown files |
| 101 | + |
| 102 | + // TODO load images from folder |
| 103 | + |
| 104 | + } else { |
| 105 | + $pluginData['icon'] = $githubUrl . '/src/Resources/config/plugin.png'; |
| 106 | + $isComposer = true; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if ($isComposer) { |
| 112 | + $pluginData['description']['en-GB'] = isset($version['extra']['description']['en-GB']) ? $version['extra']['description']['en-GB'] : $packageInformation->packageName; |
| 113 | + $pluginData['description']['de-DE'] = isset($version['extra']['description']['de-DE']) ? $version['extra']['description']['de-DE'] : $packageInformation->packageName; |
| 114 | + } |
| 115 | + |
| 116 | + $pluginData['name']['de-DE'] = isset($version['extra']['label']['de-DE']) ? $version['extra']['label']['de-DE'] : $packageInformation->packageName; |
| 117 | + $pluginData['name']['en-GB'] = isset($version['extra']['label']['en-GB']) ? $version['extra']['label']['en-GB'] : $packageInformation->packageName; |
| 118 | + |
| 119 | + $langIdDe = $this->loadLanguageId('de-DE'); |
| 120 | + $langIdEn = $this->loadLanguageId('en-GB'); |
| 121 | + |
| 122 | + $pluginData['translations'] = [ |
| 123 | + ['languageId' => $langIdDe, 'name' => $pluginData['name']['de-DE'], 'description' => $pluginData['description']['de-DE']], |
| 124 | + ['languageId' => $langIdEn, 'name' => $pluginData['name']['en-GB'], 'description' => $pluginData['description']['en-GB']], |
| 125 | + ]; |
| 126 | + |
| 127 | + unset($pluginData['description']); |
| 128 | + unset($pluginData['name']); |
| 129 | + |
| 130 | + /** @var \NuonicPluginInstaller\Core\Framework\Plugin\AvailableOpensourcePlugin\AvailableOpensourcePluginEntity|null */ |
| 131 | + $plugin = $this->availableOpensourcePluginRepository->search( |
| 132 | + (new Criteria())->addFilter(new EqualsFilter('packageName', $packageInformation->packageName)), |
| 133 | + Context::createDefaultContext() |
| 134 | + )->first(); |
| 135 | + |
| 136 | + $id = $plugin?->getId() ?? Uuid::randomHex(); |
23 | 137 |
|
24 | | - public function execute(string|PackageIndexEntry $packageInformation): void |
25 | | - { |
26 | | - if (!$packageInformation instanceof PackageIndexEntry) { |
27 | | - $packageInformation = $this->indexFileService->getPackageInformation($packageInformation); |
28 | | - } |
| 138 | + $pluginData['id'] = $id; |
| 139 | + $pluginData['isInstalled'] = $plugin ? $plugin->isInstalled() : false; |
| 140 | + |
| 141 | + $this->availableOpensourcePluginRepository->upsert([$pluginData], Context::createDefaultContext()); |
| 142 | + } |
29 | 143 |
|
30 | | - if (null === $packageInformation) { |
31 | | - return; |
32 | | - } |
33 | 144 |
|
34 | | - $repositoryUrl = $packageInformation->repositoryUrl; |
35 | | - $ref = $packageInformation->ref; |
| 145 | + private function findSuitableVersion(array $packagistData): ?array |
| 146 | + { |
| 147 | + $versions = $packagistData['package']['versions']; |
| 148 | + \ksort($versions, SORT_NATURAL); |
| 149 | + |
| 150 | + $versions = \array_reverse($versions, true); |
| 151 | + |
| 152 | + foreach ($versions as $version => $versionData) { |
| 153 | + if (\str_contains($version, 'dev') || \str_contains($version, 'alpha') || \str_contains($version, 'beta') || \str_contains($version, 'rc') || \str_contains($version, 'main')) { |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + if ($this->checkVersionConstraint($versionData['require']['shopware/core'], $this->shopwareVersion)) { |
| 158 | + return $versionData; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return null; |
| 163 | + } |
| 164 | + |
| 165 | + private function getPackagistData(string $ref) |
| 166 | + { |
| 167 | + $response = $this->httpClient->request('GET', $ref); |
| 168 | + |
| 169 | + if ($response->getStatusCode() !== 200) { |
| 170 | + return null; |
| 171 | + } |
| 172 | + |
| 173 | + $packagistData = json_decode($response->getContent(), true); |
| 174 | + return $packagistData; |
| 175 | + } |
| 176 | + |
| 177 | + private function checkVersionConstraint(string $constraint, string $version): bool |
| 178 | + { |
| 179 | + return Semver::satisfies($version, $constraint); |
| 180 | + } |
| 181 | + |
| 182 | + private function loadLanguageId(string $locale) |
| 183 | + { |
| 184 | + $key = 'nuonic-plugin-locale-' . $locale; |
| 185 | + $context = Context::createDefaultContext(); |
| 186 | + |
| 187 | + $value = $this->cache->get($key, function (ItemInterface $item) use ($locale, $context) { |
36 | 188 |
|
37 | | - $packagistData = $this->getPackagistData($ref); |
| 189 | + $languageCriteria = new Criteria(); |
| 190 | + $languageCriteria->addFilter(new EqualsFilter('locale.code', $locale)); |
| 191 | + $languageCriteria->setLimit(1); |
38 | 192 |
|
39 | | - $version = $this->findSuitableVersion($packagistData); |
| 193 | + $languageId = $this->languageRepository->searchIds($languageCriteria, $context)->firstId(); |
40 | 194 |
|
41 | | - $plugin = $this->availableOpensourcePluginRepository->search( |
42 | | - (new Criteria())->addFilter(new EqualsFilter('packageName', $packageInformation->packageName)), |
43 | | - Context::createDefaultContext() |
44 | | - )->first(); |
| 195 | + return CacheValueCompressor::compress($languageId); |
| 196 | + }); |
45 | 197 |
|
46 | | - $id = $plugin?->getId() ?? Uuid::randomHex(); |
47 | | - } |
48 | | - |
49 | | - private function findSuitableVersion(array $packagistData): string |
50 | | - { |
51 | | - $test = 1; |
52 | | - |
53 | | - return ''; |
54 | | - } |
55 | | - |
56 | | - private function getPackagistData(string $ref) |
57 | | - { |
58 | | - $packagistData = file_get_contents($ref); |
59 | | - $packagistData = json_decode($packagistData, true); |
60 | | - |
61 | | - return $packagistData; |
62 | | - } |
63 | | - |
64 | | - private function checkVersionConstraint(string $constraint, string $version): bool |
65 | | - { |
66 | | - return Semver::satisfies($version, $constraint); |
67 | | - } |
| 198 | + return CacheValueCompressor::uncompress($value); |
| 199 | + } |
68 | 200 | } |
0 commit comments