Enhance Linux distribution detection and package installation commands (forward-port #1109)

This commit is contained in:
crazywhalecc
2026-04-29 15:53:52 +08:00
parent 9ed2b18d06
commit c454a1dc8e
3 changed files with 22 additions and 6 deletions

View File

@@ -61,9 +61,19 @@ class LinuxToolCheck
'alpine' => self::TOOLS_ALPINE, 'alpine' => self::TOOLS_ALPINE,
'redhat' => self::TOOLS_RHEL, 'redhat' => self::TOOLS_RHEL,
'centos' => array_merge(self::TOOLS_RHEL, ['perl-IPC-Cmd', 'perl-Time-Piece']), 'centos' => array_merge(self::TOOLS_RHEL, ['perl-IPC-Cmd', 'perl-Time-Piece']),
'arch' => self::TOOLS_ARCH, 'arch', 'manjaro' => self::TOOLS_ARCH,
default => self::TOOLS_DEBIAN, default => null,
}; };
if ($required === null) {
// fallback to family-based detection
$family = explode(' ', strtolower($distro['family']));
$required = match (true) {
in_array('alpine', $family) => self::TOOLS_ALPINE,
in_array('rhel', $family) || in_array('fedora', $family) => self::TOOLS_RHEL,
in_array('arch', $family) => self::TOOLS_ARCH,
default => self::TOOLS_DEBIAN,
};
}
$missing = []; $missing = [];
foreach ($required as $package) { foreach ($required as $package) {
if (LinuxUtil::findCommand(self::PROVIDED_COMMAND[$package] ?? $package) === null) { if (LinuxUtil::findCommand(self::PROVIDED_COMMAND[$package] ?? $package) === null) {
@@ -116,10 +126,12 @@ class LinuxToolCheck
if ($install_cmd === null) { if ($install_cmd === null) {
// try family // try family
$family = explode(' ', strtolower($distro['family'])); $family = explode(' ', strtolower($distro['family']));
if (in_array('debian', $family)) { if (in_array('debian', $family) || in_array('ubuntu', $family)) {
$install_cmd = 'apt-get install -y'; $install_cmd = 'apt-get install -y';
} elseif (in_array('rhel', $family) || in_array('fedora', $family)) { } elseif (in_array('rhel', $family) || in_array('fedora', $family)) {
$install_cmd = 'dnf install -y'; $install_cmd = 'dnf install -y';
} elseif (in_array('arch', $family)) {
$install_cmd = 'pacman -S --noconfirm';
} else { } else {
throw new EnvironmentException( throw new EnvironmentException(
"Current linux distro [{$distro['dist']}] does not have an auto-install script for packages yet.", "Current linux distro [{$distro['dist']}] does not have an auto-install script for packages yet.",

View File

@@ -16,8 +16,11 @@ class OSCheck
if (!in_array(PHP_OS_FAMILY, ['Darwin', 'Linux', 'Windows'])) { if (!in_array(PHP_OS_FAMILY, ['Darwin', 'Linux', 'Windows'])) {
return CheckResult::fail('Current OS is not supported: ' . PHP_OS_FAMILY); return CheckResult::fail('Current OS is not supported: ' . PHP_OS_FAMILY);
} }
$distro = PHP_OS_FAMILY === 'Linux' ? (' ' . LinuxUtil::getOSRelease()['dist']) : ''; $release = PHP_OS_FAMILY === 'Linux' ? LinuxUtil::getOSRelease() : null;
$known_distro = PHP_OS_FAMILY !== 'Linux' || in_array(LinuxUtil::getOSRelease()['dist'], LinuxUtil::getSupportedDistros()); $distro = $release !== null ? (' ' . $release['dist']) : '';
$known_distro = $release === null
|| in_array($release['dist'], LinuxUtil::getSupportedDistros())
|| count(array_intersect(explode(' ', $release['family']), LinuxUtil::getSupportedDistros())) > 0;
return CheckResult::ok(PHP_OS_FAMILY . ' ' . php_uname('m') . $distro . ', supported' . ($known_distro ? '' : ' (but not tested on this distro)')); return CheckResult::ok(PHP_OS_FAMILY . ' ' . php_uname('m') . $distro . ', supported' . ($known_distro ? '' : ' (but not tested on this distro)'));
} }
} }

View File

@@ -50,6 +50,7 @@ class LinuxUtil extends UnixUtil
$ret['dist'] = trim($ret['dist'], '"\''); $ret['dist'] = trim($ret['dist'], '"\'');
$ret['ver'] = trim($ret['ver'], '"\''); $ret['ver'] = trim($ret['ver'], '"\'');
$ret['family'] = trim($ret['family'], '"\'');
if (strcasecmp($ret['dist'], 'centos') === 0) { if (strcasecmp($ret['dist'], 'centos') === 0) {
$ret['dist'] = 'redhat'; $ret['dist'] = 'redhat';
@@ -91,7 +92,7 @@ class LinuxUtil extends UnixUtil
{ {
return [ return [
// debian-like // debian-like
'debian', 'ubuntu', 'Deepin', 'neon', 'debian', 'ubuntu', 'Deepin',
// rhel-like // rhel-like
'redhat', 'redhat',
// centos // centos