forked from laysense/laysite
163 lines
6.1 KiB
PHP
163 lines
6.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Content;
|
|
|
|
use CurlHandle;
|
|
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';
|
|
|
|
public static function current(): string
|
|
{
|
|
$version = trim((string) @file_get_contents(dirname(__DIR__, 2) . '/VERSION'));
|
|
|
|
return preg_match('/^\d+\.\d+\.\d+$/D', $version) ? $version : '0.0.0';
|
|
}
|
|
|
|
public static function check(): array
|
|
{
|
|
$directory = dirname(__DIR__, 2) . '/runtime';
|
|
if (!is_dir($directory) && !@mkdir($directory, 0750, true) && !is_dir($directory)) {
|
|
throw new RuntimeException('无法创建版本检查缓存。');
|
|
}
|
|
$path = $directory . '/laysite-update.json';
|
|
$record = self::cached($path);
|
|
if (($record['until'] ?? 0) <= time()) {
|
|
$lock = @fopen($path . '.lock', 'c');
|
|
if (!$lock) {
|
|
throw new RuntimeException('无法打开版本检查缓存。');
|
|
}
|
|
try {
|
|
if (!flock($lock, LOCK_EX | LOCK_NB)) {
|
|
if (!array_key_exists('latest', $record)) {
|
|
throw new RuntimeException('版本检查正在进行,请稍后重试。');
|
|
}
|
|
} else {
|
|
$record = self::cached($path);
|
|
if (($record['until'] ?? 0) <= time()) {
|
|
try {
|
|
$record = ['latest' => self::latestTag(), 'until' => time() + 300];
|
|
} catch (RuntimeException $exception) {
|
|
$record = ['error' => $exception->getMessage(), 'until' => time() + 30];
|
|
}
|
|
self::writeCache($path, $record);
|
|
}
|
|
}
|
|
} finally {
|
|
fclose($lock);
|
|
}
|
|
}
|
|
if (isset($record['error'])) {
|
|
throw new RuntimeException($record['error']);
|
|
}
|
|
$current = self::current();
|
|
$latest = $record['latest'];
|
|
|
|
return ['current' => $current, 'latest' => $latest, 'comparison' => $latest === null ? null : version_compare($latest, $current)];
|
|
}
|
|
|
|
private static function cached(string $path): array
|
|
{
|
|
$record = json_decode((string) @file_get_contents($path), true);
|
|
|
|
if (!is_array($record) || !is_int($record['until'] ?? null)) {
|
|
return [];
|
|
}
|
|
if (is_string($record['error'] ?? null)) {
|
|
return $record;
|
|
}
|
|
$latest = $record['latest'] ?? null;
|
|
|
|
return array_key_exists('latest', $record) && ($latest === null || (is_string($latest)
|
|
&& preg_match('/^\d+\.\d+\.\d+$/D', $latest))) ? $record : [];
|
|
}
|
|
|
|
private static function writeCache(string $path, array $record): void
|
|
{
|
|
$temporary = @tempnam(dirname($path), 'laysite-update-');
|
|
if ($temporary === false) {
|
|
throw new RuntimeException('无法写入版本检查缓存。');
|
|
}
|
|
try {
|
|
if (file_put_contents($temporary, Database::json($record)) === false || !rename($temporary, $path)) {
|
|
throw new RuntimeException('无法写入版本检查缓存。');
|
|
}
|
|
} finally {
|
|
if (is_file($temporary)) {
|
|
unlink($temporary);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static function latestTag(): ?string
|
|
{
|
|
$latest = null;
|
|
$deadline = hrtime(true) + 3_000_000_000;
|
|
$page = 1;
|
|
do {
|
|
$remaining = (int) (($deadline - hrtime(true)) / 1_000_000);
|
|
if ($remaining <= 0) {
|
|
throw new RuntimeException('读取上游版本超时,请稍后重试。');
|
|
}
|
|
$tags = self::tags($page++, $remaining);
|
|
foreach ($tags as $tag) {
|
|
$name = $tag['name'] ?? null;
|
|
if (!is_string($name)) {
|
|
throw new RuntimeException('上游仓库返回的版本列表无效。');
|
|
}
|
|
if (preg_match('/^v(\d+\.\d+\.\d+)$/D', $name, $match)
|
|
&& ($latest === null || version_compare($match[1], $latest, '>'))) {
|
|
$latest = $match[1];
|
|
}
|
|
}
|
|
} while (count($tags) === 50);
|
|
|
|
return $latest;
|
|
}
|
|
|
|
private static function tags(int $page, int $timeout): array
|
|
{
|
|
$curl = curl_init(self::TAGS . '?limit=50&page=' . $page);
|
|
if ($curl === false) {
|
|
throw new RuntimeException('无法连接上游仓库,请稍后重试。');
|
|
}
|
|
$body = '';
|
|
$overflow = false;
|
|
curl_setopt_array($curl, [
|
|
CURLOPT_CONNECTTIMEOUT_MS => min(1500, $timeout),
|
|
CURLOPT_TIMEOUT_MS => $timeout,
|
|
CURLOPT_FOLLOWLOCATION => false,
|
|
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) {
|
|
$overflow = true;
|
|
return 0;
|
|
}
|
|
$body .= $chunk;
|
|
|
|
return strlen($chunk);
|
|
},
|
|
]);
|
|
if (curl_exec($curl) === false) {
|
|
throw new RuntimeException($overflow ? '上游版本数据超出长度限制。'
|
|
: (curl_errno($curl) === CURLE_OPERATION_TIMEDOUT ? '读取上游版本超时,请稍后重试。' : '无法连接上游仓库,请稍后重试。'));
|
|
}
|
|
$status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
|
|
if ($status !== 200) {
|
|
throw new RuntimeException('上游版本列表暂不可用(HTTP ' . $status . ')。');
|
|
}
|
|
$tags = json_decode($body);
|
|
if (!is_array($tags) || !array_is_list($tags)) {
|
|
throw new RuntimeException('上游仓库返回的版本列表无效。');
|
|
}
|
|
|
|
return array_map(static fn ($tag): array => is_object($tag) ? get_object_vars($tag) : [], $tags);
|
|
}
|
|
}
|