60 lines
2.2 KiB
PHP
Raw Normal View History

2023-04-09 12:11:14 +08:00
<?php
declare(strict_types=1);
namespace SPC\store\source;
use JetBrains\PhpStorm\ArrayShape;
use SPC\exception\DownloaderException;
use SPC\exception\SPCException;
2023-04-09 12:11:14 +08:00
use SPC\store\Downloader;
class PhpSource extends CustomSourceBase
{
public const string NAME = 'php-src';
public const array WEB_PHP_DOMAINS = [
'https://www.php.net',
'https://phpmirror.static-php.dev',
];
2023-04-09 12:11:14 +08:00
2025-03-30 23:27:43 +08:00
public function fetch(bool $force = false, ?array $config = null, int $lock_as = SPC_DOWNLOAD_SOURCE): void
2023-04-09 12:11:14 +08:00
{
2026-03-11 09:42:39 +07:00
$major = defined('SPC_BUILD_PHP_VERSION') ? SPC_BUILD_PHP_VERSION : '8.5';
2025-11-20 18:19:41 +01:00
if ($major === 'git') {
2025-07-28 15:36:53 +08:00
Downloader::downloadSource('php-src', ['type' => 'git', 'url' => 'https://github.com/php/php-src.git', 'rev' => 'master'], $force);
2025-07-15 21:14:15 +08:00
} else {
2025-09-29 21:15:18 +02:00
Downloader::downloadSource('php-src', $this->getLatestPHPInfo($major), $force);
2025-07-15 21:14:15 +08:00
}
2023-04-09 12:11:14 +08:00
}
/**
* 获取 PHP x.y 的具体版本号,例如通过 8.1 来获取 8.1.10
*/
#[ArrayShape(['type' => 'string', 'path' => 'string', 'rev' => 'string', 'url' => 'string'])]
public function getLatestPHPInfo(string $major_version): array
{
foreach (self::WEB_PHP_DOMAINS as $domain) {
try {
$info = json_decode(Downloader::curlExec(
url: "{$domain}/releases/index.php?json&version={$major_version}",
retries: (int) getenv('SPC_DOWNLOAD_RETRIES') ?: 0
), true);
if (!isset($info['version'])) {
throw new DownloaderException("Version {$major_version} not found.");
}
$version = $info['version'];
return [
'type' => 'url',
'url' => "{$domain}/distributions/php-{$version}.tar.xz",
];
} catch (SPCException) {
logger()->warning('Failed to fetch latest PHP version for major version {$major_version} from {$domain}, trying next mirror if available.');
continue;
}
}
// exception if all mirrors failed
throw new DownloaderException("Failed to fetch latest PHP version for major version {$major_version} from all tried mirrors.");
2023-04-09 12:11:14 +08:00
}
}