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