diff --git a/CHANGELOG.md b/CHANGELOG.md index 60a3776..7c35976 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [1.1.0] - 2022-10-20 +### Added +- Get the latest version of a partial version string (e.g. get version `6.4.15.2` if you request version `6.4.15`) + ### Changed - Return the real Amazon S3 download url instead of the redirect url from Shopware @@ -12,5 +15,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Controller for getting install urls for specific Shopware 6 versions or the latest version -[Unreleased]: https://github.com/jdambacher/shopware-versions/compare/1.0.0...HEAD +[1.1.0]: https://github.com/jdambacher/shopware-versions/compare/1.0.0...1.1.0 [1.0.0]: https://github.com/jdambacher/shopware-versions/releases/tag/1.0.0 \ No newline at end of file diff --git a/README.md b/README.md index 0ab3f11..06a78ca 100644 --- a/README.md +++ b/README.md @@ -29,13 +29,28 @@ curl -s https://sw-version.dambacher.net/version ``` or ```curl -curl -s https://sw-version.dambacher.net/version/latest +curl -s https://sw-version.dambacher.net/version/6 ``` +### For partial version strings +Get the download url for the latest version of a partial version string (e.g. use `6.4.16` to get the latest patch version of `6.4.16`). + +If `6.4.16.3` is the last patch version in this minor version, this will give you the download url for Shopware 6.4.16.3: + +```curl +curl -s https://sw-version.dambacher.net/version/6.4.16 +``` + +You can also request the latest version of a major version (e.g. `6.4`): + +```curl +curl -s https://sw-version.dambacher.net/version/6.4 +``` + + ## Future ideas Here's a list of ideas for future releases: - Cache the changelog page to reduce the load on Shopware's servers and maintain a download link even if the changelog page is not accessible -- Add a parameter to get the download url for the latest minor or patch version of a specific major version - Listen for the `Accept` header and return the download url as plain text, json or xml - Collect more data from the changelog page (e.g. hash, release date, bugfixes etc.) - Enable to use this package via composer to get the download url in your own project without rely on the webservice \ No newline at end of file diff --git a/src/Controller/VersionController.php b/src/Controller/VersionController.php index 0ca2979..aaade74 100644 --- a/src/Controller/VersionController.php +++ b/src/Controller/VersionController.php @@ -18,29 +18,14 @@ public function __construct( } #[Route('/version/{versionNo}', name: 'version')] - public function version(string $versionNo = 'latest'): Response + public function version(string $versionNo = '6'): Response { - $versions = $this->versionLoader->getChangelog(); - - // Simply return latest version if version no. is not given in request - if (!$versionNo || $versionNo === 'latest') { - return new RedirectResponse($versions[0]->getInstallUrl()); - } - - // Easy solution - TODO: Refacator later - /** @var Version $version */ - foreach ($versions as $version) { - if ($version->getVersionNo() === $versionNo) { - return new RedirectResponse($version->getInstallUrl()); - } + try { + $version = $this->versionLoader->getSpecificVersion($versionNo); + } catch (\Exception $e) { + return new Response('Version not found', Response::HTTP_NOT_FOUND); } - return new Response('Version not found', Response::HTTP_NOT_FOUND); - } - - #[Route('/version', name: 'latest-version')] - public function latestVersion(): Response - { - return $this->redirectToRoute('version', ['versionNo' => 'latest']); + return new RedirectResponse($version->getInstallUrl()); } } \ No newline at end of file diff --git a/src/VersionLoader.php b/src/VersionLoader.php index 4c6239e..6d65999 100644 --- a/src/VersionLoader.php +++ b/src/VersionLoader.php @@ -4,7 +4,6 @@ namespace App; use App\Entity\Version; -use App\Factory\VersionFactory; use Symfony\Component\DomCrawler\Crawler; class VersionLoader @@ -18,13 +17,6 @@ public function getChangelog(): array $crawler = new Crawler($html); - /*$crawler - ->filter('.release-details--cta > a') - ->reduce(function (Crawler $node, $i) { - //$versions[] = $node->attr('href')); - VersionFactory::create($node->attr('href')); - });*/ - $accordions = $crawler ->filter('.accordion--column > .accordion'); @@ -40,6 +32,39 @@ public function getChangelog(): array return $versions; } + public function getSpecificVersion(string $versionNo): Version + { + $versions = $this->splitChangelog($this->getChangelog()); + $splittedVersionNo = $this->splitVersionNo($versionNo); + + // TODO: This is ugly, but works. @future me: Please refactor this. + if (!$splittedVersionNo['base']) { + $base = array_shift($versions); + } else { + $base = $versions[$splittedVersionNo['base']]; + } + + if (!$splittedVersionNo['major']) { + $major = array_shift($base); + } else { + $major = $base[$splittedVersionNo['major']]; + } + + if (!$splittedVersionNo['minor']) { + $minor = array_shift($major); + } else { + $minor = $major[$splittedVersionNo['minor']]; + } + + if (!$splittedVersionNo['patch']) { + $patch = array_shift($minor); + } else { + $patch = $minor[$splittedVersionNo['patch']]; + } + + return $patch; + } + /* * Returns the real url at Amazon S3 after the redirect */ @@ -54,4 +79,30 @@ private function getRealUrl(?string $url): ?string return self::S3_BASE_URL . '/' . $url; } + + private function splitChangelog(array $changelog): array + { + $splittedVersions = []; + + /** @var Version $version */ + foreach ($changelog as $version) { + $splittedVersionNo = $this->splitVersionNo($version->getVersionNo()); + $splittedVersions[$splittedVersionNo['base']][$splittedVersionNo['major']][$splittedVersionNo['minor']][$splittedVersionNo['patch']] = $version; + } + + return $splittedVersions; + } + + private function splitVersionNo(string $versionNo): array + { + // TODO: Use regex here + $versionNo = explode('.', $versionNo); + + return [ + 'base' => $versionNo[0] ?? null, + 'major' => $versionNo[1] ?? null, + 'minor' => $versionNo[2] ?? null, + 'patch' => $versionNo[3] ?? null, + ]; + } } \ No newline at end of file