Refactor all (except command) modules using new exceptions

This commit is contained in:
crazywhalecc
2025-08-06 20:43:23 +08:00
committed by Jerry Ma
parent 722bb31815
commit f68f060be2
47 changed files with 335 additions and 291 deletions

View File

@@ -9,10 +9,6 @@ use SPC\doctor\AsCheckItem;
use SPC\doctor\AsFixItem;
use SPC\doctor\CheckResult;
use SPC\doctor\OptionalCheck;
use SPC\exception\DownloaderException;
use SPC\exception\FileSystemException;
use SPC\exception\RuntimeException;
use SPC\exception\WrongUsageException;
use SPC\store\Downloader;
use SPC\store\FileSystem;
use SPC\store\PackageManager;
@@ -58,55 +54,47 @@ class LinuxMuslCheck
#[AsFixItem('fix-musl-wrapper')]
public function fixMusl(): bool
{
try {
$prefix = '';
if (get_current_user() !== 'root') {
$prefix = 'sudo ';
logger()->warning('Current user is not root, using sudo for running command');
}
// The hardcoded version here is to be consistent with the version compiled by `musl-cross-toolchain`.
$musl_version_name = 'musl-1.2.5';
$musl_source = [
'type' => 'url',
'url' => "https://musl.libc.org/releases/{$musl_version_name}.tar.gz",
];
logger()->info('Downloading ' . $musl_source['url']);
Downloader::downloadSource($musl_version_name, $musl_source);
FileSystem::extractSource($musl_version_name, SPC_SOURCE_ARCHIVE, DOWNLOAD_PATH . "/{$musl_version_name}.tar.gz");
// Apply CVE-2025-26519 patch
SourcePatcher::patchFile('musl-1.2.5_CVE-2025-26519_0001.patch', SOURCE_PATH . "/{$musl_version_name}");
SourcePatcher::patchFile('musl-1.2.5_CVE-2025-26519_0002.patch', SOURCE_PATH . "/{$musl_version_name}");
logger()->info('Installing musl wrapper');
shell()->cd(SOURCE_PATH . "/{$musl_version_name}")
->exec('CC=gcc CXX=g++ AR=ar LD=ld ./configure --disable-gcc-wrapper')
->exec('CC=gcc CXX=g++ AR=ar LD=ld make -j')
->exec("CC=gcc CXX=g++ AR=ar LD=ld {$prefix}make install");
// TODO: add path using putenv instead of editing /etc/profile
return true;
} catch (RuntimeException) {
return false;
$prefix = '';
if (get_current_user() !== 'root') {
$prefix = 'sudo ';
logger()->warning('Current user is not root, using sudo for running command');
}
// The hardcoded version here is to be consistent with the version compiled by `musl-cross-toolchain`.
$musl_version_name = 'musl-1.2.5';
$musl_source = [
'type' => 'url',
'url' => "https://musl.libc.org/releases/{$musl_version_name}.tar.gz",
];
logger()->info('Downloading ' . $musl_source['url']);
Downloader::downloadSource($musl_version_name, $musl_source);
FileSystem::extractSource($musl_version_name, SPC_SOURCE_ARCHIVE, DOWNLOAD_PATH . "/{$musl_version_name}.tar.gz");
// Apply CVE-2025-26519 patch
SourcePatcher::patchFile('musl-1.2.5_CVE-2025-26519_0001.patch', SOURCE_PATH . "/{$musl_version_name}");
SourcePatcher::patchFile('musl-1.2.5_CVE-2025-26519_0002.patch', SOURCE_PATH . "/{$musl_version_name}");
logger()->info('Installing musl wrapper');
shell()->cd(SOURCE_PATH . "/{$musl_version_name}")
->exec('CC=gcc CXX=g++ AR=ar LD=ld ./configure --disable-gcc-wrapper')
->exec('CC=gcc CXX=g++ AR=ar LD=ld make -j')
->exec("CC=gcc CXX=g++ AR=ar LD=ld {$prefix}make install");
// TODO: add path using putenv instead of editing /etc/profile
return true;
}
#[AsFixItem('fix-musl-cross-make')]
public function fixMuslCrossMake(): bool
{
try {
$prefix = '';
if (get_current_user() !== 'root') {
$prefix = 'sudo ';
logger()->warning('Current user is not root, using sudo for running command');
}
$arch = arch2gnu(php_uname('m'));
logger()->info("Downloading package musl-toolchain-{$arch}-linux");
PackageManager::installPackage("musl-toolchain-{$arch}-linux");
$pkg_root = PKG_ROOT_PATH . "/musl-toolchain-{$arch}-linux";
shell()->exec("{$prefix}cp -rf {$pkg_root}/* /usr/local/musl");
FileSystem::removeDir($pkg_root);
return true;
} catch (RuntimeException) {
return false;
$prefix = '';
if (get_current_user() !== 'root') {
$prefix = 'sudo ';
logger()->warning('Current user is not root, using sudo for running command');
}
$arch = arch2gnu(php_uname('m'));
logger()->info("Downloading package musl-toolchain-{$arch}-linux");
PackageManager::installPackage("musl-toolchain-{$arch}-linux");
$pkg_root = PKG_ROOT_PATH . "/musl-toolchain-{$arch}-linux";
shell()->exec("{$prefix}cp -rf {$pkg_root}/* /usr/local/musl");
FileSystem::removeDir($pkg_root);
return true;
}
}

View File

@@ -9,7 +9,7 @@ use SPC\builder\traits\UnixSystemUtilTrait;
use SPC\doctor\AsCheckItem;
use SPC\doctor\AsFixItem;
use SPC\doctor\CheckResult;
use SPC\exception\RuntimeException;
use SPC\exception\EnvironmentException;
class LinuxToolCheckList
{
@@ -74,16 +74,7 @@ class LinuxToolCheckList
}
}
if (!empty($missing)) {
return match ($distro['dist']) {
'ubuntu',
'alpine',
'redhat',
'centos',
'Deepin',
'arch',
'debian' => CheckResult::fail(implode(', ', $missing) . ' not installed on your system', 'install-linux-tools', [$distro, $missing]),
default => CheckResult::fail(implode(', ', $missing) . ' not installed on your system'),
};
return CheckResult::fail(implode(', ', $missing) . ' not installed on your system', 'install-linux-tools', [$distro, $missing]);
}
return CheckResult::ok();
}
@@ -123,22 +114,23 @@ class LinuxToolCheckList
'redhat' => 'dnf install -y',
'centos' => 'yum install -y',
'arch' => 'pacman -S --noconfirm',
default => throw new RuntimeException('Current linux distro does not have an auto-install script for musl packages yet.'),
default => throw new EnvironmentException(
"Current linux distro [{$distro['dist']}] does not have an auto-install script for packages yet.",
'You can submit an issue to request support: https://github.com/crazywhalecc/static-php-cli/issues'
),
};
$prefix = '';
if (($user = exec('whoami')) !== 'root') {
$prefix = 'sudo ';
logger()->warning('Current user (' . $user . ') is not root, using sudo for running command');
}
try {
$is_debian = in_array($distro['dist'], ['debian', 'ubuntu', 'Deepin']);
$to_install = $is_debian ? str_replace('xz', 'xz-utils', $missing) : $missing;
// debian, alpine libtool -> libtoolize
$to_install = str_replace('libtoolize', 'libtool', $to_install);
shell(true)->exec($prefix . $install_cmd . ' ' . implode(' ', $to_install));
} catch (RuntimeException) {
return false;
logger()->warning('Current user (' . $user . ') is not root, using sudo for running command (may require password input)');
}
$is_debian = in_array($distro['dist'], ['debian', 'ubuntu', 'Deepin']);
$to_install = $is_debian ? str_replace('xz', 'xz-utils', $missing) : $missing;
// debian, alpine libtool -> libtoolize
$to_install = str_replace('libtoolize', 'libtool', $to_install);
shell(true)->exec($prefix . $install_cmd . ' ' . implode(' ', $to_install));
return true;
}
}

View File

@@ -8,7 +8,6 @@ use SPC\builder\traits\UnixSystemUtilTrait;
use SPC\doctor\AsCheckItem;
use SPC\doctor\AsFixItem;
use SPC\doctor\CheckResult;
use SPC\exception\RuntimeException;
class MacOSToolCheckList
{
@@ -89,11 +88,7 @@ class MacOSToolCheckList
#[AsFixItem('brew')]
public function fixBrew(): bool
{
try {
shell(true)->exec('/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"');
} catch (RuntimeException) {
return false;
}
shell(true)->exec('/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"');
return true;
}
@@ -104,14 +99,10 @@ class MacOSToolCheckList
'glibtoolize' => 'libtool',
];
foreach ($missing as $cmd) {
try {
if (isset($replacement[$cmd])) {
$cmd = $replacement[$cmd];
}
shell(true)->exec('brew install --formula ' . escapeshellarg($cmd));
} catch (RuntimeException) {
return false;
if (isset($replacement[$cmd])) {
$cmd = $replacement[$cmd];
}
shell(true)->exec('brew install --formula ' . escapeshellarg($cmd));
}
return true;
}

View File

@@ -8,7 +8,6 @@ use SPC\builder\windows\SystemUtil;
use SPC\doctor\AsCheckItem;
use SPC\doctor\AsFixItem;
use SPC\doctor\CheckResult;
use SPC\exception\RuntimeException;
use SPC\store\FileSystem;
use SPC\store\PackageManager;
@@ -80,12 +79,8 @@ class WindowsToolCheckList
#[AsFixItem('install-php-sdk')]
public function installPhpSdk(): bool
{
try {
FileSystem::removeDir(getenv('PHP_SDK_PATH'));
cmd(true)->exec('git.exe clone --depth 1 https://github.com/php/php-sdk-binary-tools.git ' . getenv('PHP_SDK_PATH'));
} catch (RuntimeException) {
return false;
}
FileSystem::removeDir(getenv('PHP_SDK_PATH'));
cmd(true)->exec('git.exe clone --depth 1 https://github.com/php/php-sdk-binary-tools.git ' . getenv('PHP_SDK_PATH'));
return true;
}