Use a standalone PHP updater with release compatibility rules

This commit is contained in:
2026-11-27 17:19:02 +08:00
parent b77d742698
commit 9fb69f4d00
5 changed files with 239 additions and 77 deletions
+15 -15
View File
@@ -10,7 +10,7 @@ use RuntimeException;
final class Updates
{
public const REPOSITORY = 'https://git.laysense.com/laysense/laysite';
private const TAGS = 'https://git.laysense.com/api/v1/repos/laysense/laysite/tags';
private const RELEASES = 'https://git.laysense.com/api/v1/repos/laysense/laysite/releases';
public static function current(): string
{
@@ -44,7 +44,7 @@ final class Updates
$record = self::cached($path);
if (($record['until'] ?? 0) <= time()) {
try {
$record = ['latest' => self::latestTag(), 'until' => time() + 300];
$record = ['latest' => self::latestRelease(), 'until' => time() + 300];
} catch (RuntimeException $exception) {
$record = ['error' => $exception->getMessage(), 'until' => time() + 30];
}
@@ -97,7 +97,7 @@ final class Updates
}
}
private static function latestTag(): ?string
private static function latestRelease(): ?string
{
$latest = null;
$deadline = hrtime(true) + 3_000_000_000;
@@ -107,25 +107,25 @@ final class Updates
if ($remaining <= 0) {
throw new RuntimeException('读取上游版本超时,请稍后重试。');
}
$tags = self::tags($page++, $remaining);
foreach ($tags as $tag) {
$name = $tag['name'] ?? null;
if (!is_string($name)) {
$releases = self::releases($page++, $remaining);
foreach ($releases as $release) {
$name = $release['tag_name'] ?? null;
if (!is_string($name) || !is_bool($release['draft'] ?? null) || !is_bool($release['prerelease'] ?? null)) {
throw new RuntimeException('上游仓库返回的版本列表无效。');
}
if (preg_match('/^v(\d+\.\d+\.\d+)$/D', $name, $match)
if (!$release['draft'] && !$release['prerelease'] && preg_match('/^v(\d+\.\d+\.\d+)$/D', $name, $match)
&& ($latest === null || version_compare($match[1], $latest, '>'))) {
$latest = $match[1];
}
}
} while (count($tags) === 50);
} while (count($releases) === 50);
return $latest;
}
private static function tags(int $page, int $timeout): array
private static function releases(int $page, int $timeout): array
{
$curl = curl_init(self::TAGS . '?limit=50&page=' . $page);
$curl = curl_init(self::RELEASES . '?limit=50&page=' . $page);
if ($curl === false) {
throw new RuntimeException('无法连接上游仓库,请稍后重试。');
}
@@ -138,7 +138,7 @@ final class Updates
CURLOPT_HTTPHEADER => ['Accept: application/json'],
CURLOPT_USERAGENT => 'LaySite version check',
CURLOPT_WRITEFUNCTION => static function (CurlHandle $handle, string $chunk) use (&$body, &$overflow): int {
if (strlen($body) + strlen($chunk) > 256 * 1024) {
if (strlen($body) + strlen($chunk) > 1024 * 1024) {
$overflow = true;
return 0;
}
@@ -155,11 +155,11 @@ final class Updates
if ($status !== 200) {
throw new RuntimeException('上游版本列表暂不可用(HTTP ' . $status . ')。');
}
$tags = json_decode($body);
if (!is_array($tags) || !array_is_list($tags)) {
$releases = json_decode($body);
if (!is_array($releases) || !array_is_list($releases)) {
throw new RuntimeException('上游仓库返回的版本列表无效。');
}
return array_map(static fn ($tag): array => is_object($tag) ? get_object_vars($tag) : [], $tags);
return array_map(static fn ($release): array => is_object($release) ? get_object_vars($release) : [], $releases);
}
}