66
77use JetBrains \PhpStorm \ArrayShape ;
88use SPC \exception \DownloaderException ;
9+ use SPC \exception \SPCException ;
910use SPC \store \Downloader ;
1011
1112class 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