Skip to content

Commit

Permalink
feat(search): add snapshoted latest version and date of a package
Browse files Browse the repository at this point in the history
  • Loading branch information
94noni committed Jan 8, 2024
1 parent f1c5a5a commit c3f61df
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ search.addWidget(
<div class="col-sm-3 col-lg-2">
{{#meta}}
<p class="metadata">
<span class="metadata-block"><i class="glyphicon glyphicon-tags"></i> {{ meta.latest_version }}</span>
<span class="metadata-block"><i class="glyphicon glyphicon-calendar"></i> {{ meta.latest_date }}</span>
<span class="metadata-block"><i class="glyphicon glyphicon-download"></i> {{ meta.downloads_formatted }}</span>
<span class="metadata-block"><i class="glyphicon glyphicon-star"></i> {{ meta.favers_formatted }}</span>
</p>
Expand Down
8 changes: 8 additions & 0 deletions src/Command/IndexPackagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions src/Entity/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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')
}
}

/**
Expand All @@ -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;
Expand Down

0 comments on commit c3f61df

Please sign in to comment.