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); } }