Skip to content

Commit 8c4e3d5

Browse files
authored
Add php-src mirror and use gmp mirror site (#1048)
1 parent 8a51d64 commit 8c4e3d5

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

config/source.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@
361361
},
362362
"gmp": {
363363
"type": "filelist",
364-
"url": "https://gmplib.org/download/gmp/",
364+
"url": "https://ftp.gnu.org/gnu/gmp/",
365365
"regex": "/href=\"(?<file>gmp-(?<version>[^\"]+)\\.tar\\.xz)\"/",
366366
"provide-pre-built": true,
367367
"alt": {

src/SPC/store/source/PhpSource.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66

77
use JetBrains\PhpStorm\ArrayShape;
88
use SPC\exception\DownloaderException;
9+
use SPC\exception\SPCException;
910
use SPC\store\Downloader;
1011

1112
class PhpSource extends CustomSourceBase
1213
{
13-
public const NAME = 'php-src';
14+
public const string NAME = 'php-src';
15+
16+
public const array WEB_PHP_DOMAINS = [
17+
'https://www.php.net',
18+
'https://phpmirror.static-php.dev',
19+
];
1420

1521
public function fetch(bool $force = false, ?array $config = null, int $lock_as = SPC_DOWNLOAD_SOURCE): void
1622
{
@@ -28,21 +34,26 @@ public function fetch(bool $force = false, ?array $config = null, int $lock_as =
2834
#[ArrayShape(['type' => 'string', 'path' => 'string', 'rev' => 'string', 'url' => 'string'])]
2935
public function getLatestPHPInfo(string $major_version): array
3036
{
31-
// 查找最新的小版本号
32-
$info = json_decode(Downloader::curlExec(
33-
url: "https://www.php.net/releases/index.php?json&version={$major_version}",
34-
retries: (int) getenv('SPC_DOWNLOAD_RETRIES') ?: 0
35-
), true);
36-
if (!isset($info['version'])) {
37-
throw new DownloaderException("Version {$major_version} not found.");
37+
foreach (self::WEB_PHP_DOMAINS as $domain) {
38+
try {
39+
$info = json_decode(Downloader::curlExec(
40+
url: "{$domain}/releases/index.php?json&version={$major_version}",
41+
retries: (int) getenv('SPC_DOWNLOAD_RETRIES') ?: 0
42+
), true);
43+
if (!isset($info['version'])) {
44+
throw new DownloaderException("Version {$major_version} not found.");
45+
}
46+
$version = $info['version'];
47+
return [
48+
'type' => 'url',
49+
'url' => "{$domain}/distributions/php-{$version}.tar.xz",
50+
];
51+
} catch (SPCException) {
52+
logger()->warning('Failed to fetch latest PHP version for major version {$major_version} from {$domain}, trying next mirror if available.');
53+
continue;
54+
}
3855
}
39-
40-
$version = $info['version'];
41-
42-
// 从官网直接下载
43-
return [
44-
'type' => 'url',
45-
'url' => "https://www.php.net/distributions/php-{$version}.tar.xz",
46-
];
56+
// exception if all mirrors failed
57+
throw new DownloaderException("Failed to fetch latest PHP version for major version {$major_version} from all tried mirrors.");
4758
}
4859
}

0 commit comments

Comments
 (0)