From c3f61dfdf6befe9fe89e25e7827dfcaf82143146 Mon Sep 17 00:00:00 2001 From: Antoine Makdessi Date: Mon, 8 Jan 2024 18:36:14 +0100 Subject: [PATCH] feat(search): add snapshoted latest version and date of a package --- js/search.js | 2 ++ src/Command/IndexPackagesCommand.php | 8 ++++++++ src/Entity/Package.php | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/js/search.js b/js/search.js index 45836c4a5f..04e6a20943 100644 --- a/js/search.js +++ b/js/search.js @@ -174,6 +174,8 @@ search.addWidget(
{{#meta}} diff --git a/src/Command/IndexPackagesCommand.php b/src/Command/IndexPackagesCommand.php index 2dc48b44ee..b2d9c4b152 100644 --- a/src/Command/IndexPackagesCommand.php +++ b/src/Command/IndexPackagesCommand.php @@ -233,6 +233,14 @@ private function packageToSearchableArray(Package $package, array $tags): array $record['replacementPackage'] = ''; } + if (null !== $snapshotedLatestVersion = $package->getSnapshotedLatestVersion()) { + $snapshotedLatestVersion = \exploded('&', $snapshotedLatestVersion); + $record['meta'] = [ + 'latest_version' => $snapshotedLatestVersion[0], + 'latest_date' => $snapshotedLatestVersion[1], + ]; + } + $record['tags'] = $tags; return $record; diff --git a/src/Entity/Package.php b/src/Entity/Package.php index 8f718d15a8..7a5ee81f17 100644 --- a/src/Entity/Package.php +++ b/src/Entity/Package.php @@ -178,6 +178,9 @@ class Package */ private array|null $cachedVersions = null; + #[ORM\Column(type: 'string', length: 255, nullable: true)] + private string|null $snapshotedLatestVersion = null; + public function __construct() { $this->versions = new ArrayCollection(); @@ -539,6 +542,18 @@ public function getBrowsableRepository(): string public function addVersion(Version $version): void { $this->versions[] = $version; + + $newVersion = strtolower($version->getNormalizedVersion()); + + if (null === $this->snapshotedLatestVersion) { + $this->snapshotedLatestVersion = $newVersion . '&' . $version->getReleasedAt()->format('Y-m-d'); + } + + $actualVersion = explode('&', $this->snapshotedLatestVersion)[0]; + + if (version_compare($newVersion, $actualVersion, '>=')) { + $this->snapshotedLatestVersion = $newVersion . '&' . $version->getReleasedAt()->format('Y-m-d') + } } /** @@ -565,6 +580,11 @@ public function getVersion(string $normalizedVersion): Version|null return null; } + public function getSnapshotedLatestVersion(): ?string + { + return $this->snapshotedLatestVersion; + } + public function setUpdatedAt(DateTimeInterface $updatedAt): void { $this->updatedAt = $updatedAt;