Files
laysite/app/Content/Updates.php
T

52 lines
1.7 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';
public static function current(): string
{
return trim(file_get_contents(dirname(__DIR__, 2) . '/VERSION'));
}
public static function check(): array
{
$curl = curl_init(self::REPOSITORY . '/raw/branch/main/VERSION');
if ($curl === false) {
throw new RuntimeException('无法连接上游仓库,请稍后重试。');
}
$body = '';
curl_setopt_array($curl, [
CURLOPT_CONNECTTIMEOUT_MS => 2500,
CURLOPT_TIMEOUT_MS => 6000,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_USERAGENT => 'LaySite version check',
CURLOPT_WRITEFUNCTION => static function (CurlHandle $handle, string $chunk) use (&$body): int {
if (strlen($body) + strlen($chunk) > 128) {
return 0;
}
$body .= $chunk;
return strlen($chunk);
},
]);
if (curl_exec($curl) === false || curl_getinfo($curl, CURLINFO_RESPONSE_CODE) !== 200) {
throw new RuntimeException('无法读取上游版本,请稍后重试。');
}
if (!preg_match('/^v?(\d+\.\d+\.\d+)$/D', trim($body), $version)) {
throw new RuntimeException('上游仓库返回的版本格式无效。');
}
$current = self::current();
$latest = $version[1];
return ['current' => $current, 'latest' => $latest, 'comparison' => version_compare($latest, $current)];
}
}