mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-09 01:45:36 +08:00
Merge branch 'main' of https://github.com/crazywhalecc/static-php-cli into zig
This commit is contained in:
@@ -368,6 +368,11 @@ class Downloader
|
||||
break;
|
||||
case 'custom': // Custom download method, like API-based download or other
|
||||
$classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/store/pkg', 'SPC\store\pkg');
|
||||
if (isset($pkg['func']) && is_callable($pkg['func'])) {
|
||||
$pkg['name'] = $name;
|
||||
$pkg['func']($force, $pkg, SPC_DOWNLOAD_PACKAGE);
|
||||
break;
|
||||
}
|
||||
foreach ($classes as $class) {
|
||||
if (is_a($class, CustomPackage::class, true) && $class !== CustomPackage::class) {
|
||||
$cls = new $class();
|
||||
@@ -608,6 +613,29 @@ class Downloader
|
||||
return "{$source}-{$os_family}-{$gnu_arch}-{$libc}-{$libc_version}";
|
||||
}
|
||||
|
||||
public static function getDefaultAlternativeSource(string $source_name): array
|
||||
{
|
||||
return [
|
||||
'type' => 'custom',
|
||||
'func' => function (bool $force, array $source, int $download_as) use ($source_name) {
|
||||
logger()->debug("Fetching alternative source for {$source_name}");
|
||||
// get from dl.static-php.dev
|
||||
$url = "https://dl.static-php.dev/static-php-cli/deps/spc-download-mirror/{$source_name}/?format=json";
|
||||
$json = json_decode(Downloader::curlExec(url: $url, retries: intval(getenv('SPC_DOWNLOAD_RETRIES') ?: 0)), true);
|
||||
if (!is_array($json)) {
|
||||
throw new RuntimeException('failed http fetch');
|
||||
}
|
||||
$item = $json[0] ?? null;
|
||||
if ($item === null) {
|
||||
throw new RuntimeException('failed to parse json');
|
||||
}
|
||||
$full_url = 'https://dl.static-php.dev' . $item['full_path'];
|
||||
$filename = basename($item['full_path']);
|
||||
Downloader::downloadFile($source_name, $full_url, $filename, $source['path'] ?? null, $download_as);
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register CTRL+C event for different OS.
|
||||
*
|
||||
|
||||
@@ -4,13 +4,14 @@ declare(strict_types=1);
|
||||
|
||||
namespace SPC\store;
|
||||
|
||||
use SPC\exception\DownloaderException;
|
||||
use SPC\exception\FileSystemException;
|
||||
use SPC\exception\WrongUsageException;
|
||||
use SPC\store\pkg\CustomPackage;
|
||||
|
||||
class PackageManager
|
||||
{
|
||||
public static function installPackage(string $pkg_name, ?array $config = null, bool $force = false): void
|
||||
public static function installPackage(string $pkg_name, ?array $config = null, bool $force = false, bool $allow_alt = true, bool $extract = true): void
|
||||
{
|
||||
if ($config === null) {
|
||||
$config = Config::getPkg($pkg_name);
|
||||
@@ -32,7 +33,31 @@ class PackageManager
|
||||
}
|
||||
|
||||
// Download package
|
||||
Downloader::downloadPackage($pkg_name, $config, $force);
|
||||
try {
|
||||
Downloader::downloadPackage($pkg_name, $config, $force);
|
||||
} catch (\Throwable $e) {
|
||||
if (!$allow_alt) {
|
||||
throw new DownloaderException("Download package {$pkg_name} failed: " . $e->getMessage());
|
||||
}
|
||||
// if download failed, we will try to download alternative packages
|
||||
logger()->warning("Download package {$pkg_name} failed: " . $e->getMessage());
|
||||
$alt = $config['alt'] ?? null;
|
||||
if ($alt === null) {
|
||||
logger()->warning("No alternative package found for {$pkg_name}, using default mirror.");
|
||||
$alt_config = array_merge($config, Downloader::getDefaultAlternativeSource($pkg_name));
|
||||
} elseif ($alt === false) {
|
||||
logger()->error("No alternative package found for {$pkg_name}.");
|
||||
throw $e;
|
||||
} else {
|
||||
logger()->notice("Trying alternative package for {$pkg_name}.");
|
||||
$alt_config = array_merge($config, $alt);
|
||||
}
|
||||
Downloader::downloadPackage($pkg_name, $alt_config, $force);
|
||||
}
|
||||
if (!$extract) {
|
||||
logger()->info("Package [{$pkg_name}] downloaded, but extraction is skipped.");
|
||||
return;
|
||||
}
|
||||
if (Config::getPkg($pkg_name)['type'] === 'custom') {
|
||||
// Custom extract function
|
||||
$classes = FileSystem::getClassesPsr4(ROOT_DIR . '/src/SPC/store/pkg', 'SPC\store\pkg');
|
||||
|
||||
Reference in New Issue
Block a user