mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-09 01:45:36 +08:00
Refactor getOSRelease method for improved readability and efficiency
This commit is contained in:
@@ -12,7 +12,6 @@ class LinuxUtil extends UnixUtil
|
|||||||
/**
|
/**
|
||||||
* Get current linux distro name and version.
|
* Get current linux distro name and version.
|
||||||
*
|
*
|
||||||
* @noinspection PhpMissingBreakStatementInspection
|
|
||||||
* @return array{dist: string, ver: string, family: string} Linux distro info (unknown if not found)
|
* @return array{dist: string, ver: string, family: string} Linux distro info (unknown if not found)
|
||||||
*/
|
*/
|
||||||
public static function getOSRelease(): array
|
public static function getOSRelease(): array
|
||||||
@@ -22,42 +21,39 @@ class LinuxUtil extends UnixUtil
|
|||||||
'ver' => 'unknown',
|
'ver' => 'unknown',
|
||||||
'family' => 'unknown',
|
'family' => 'unknown',
|
||||||
];
|
];
|
||||||
switch (true) {
|
|
||||||
case file_exists('/etc/centos-release'):
|
if (file_exists('/etc/centos-release') || file_exists('/etc/redhat-release')) {
|
||||||
$lines = file('/etc/centos-release');
|
$is_centos = file_exists('/etc/centos-release');
|
||||||
$centos = true;
|
$file = $is_centos ? '/etc/centos-release' : '/etc/redhat-release';
|
||||||
goto rh;
|
$lines = file($file);
|
||||||
case file_exists('/etc/redhat-release'):
|
|
||||||
$lines = file('/etc/redhat-release');
|
foreach ($lines as $line) {
|
||||||
$centos = false;
|
if (preg_match('/release\s+(\d*(\.\d+)*)/', $line, $matches)) {
|
||||||
rh:
|
$ret['dist'] = $is_centos ? 'centos' : 'redhat';
|
||||||
foreach ($lines as $line) {
|
$ret['ver'] = $matches[1];
|
||||||
if (preg_match('/release\s+(\d*(\.\d+)*)/', $line, $matches)) {
|
|
||||||
/* @phpstan-ignore-next-line */
|
|
||||||
$ret['dist'] = $centos ? 'centos' : 'redhat';
|
|
||||||
$ret['ver'] = $matches[1];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
case file_exists('/etc/os-release'):
|
} elseif (file_exists('/etc/os-release')) {
|
||||||
$lines = file('/etc/os-release');
|
$lines = file('/etc/os-release');
|
||||||
foreach ($lines as $line) {
|
|
||||||
if (preg_match('/^ID=(.*)$/', $line, $matches)) {
|
foreach ($lines as $line) {
|
||||||
$ret['dist'] = $matches[1];
|
if (preg_match('/^ID=(.*)$/', $line, $matches)) {
|
||||||
}
|
$ret['dist'] = $matches[1];
|
||||||
if (preg_match('/^ID_LIKE=(.*)$/', $line, $matches)) {
|
|
||||||
$ret['family'] = $matches[1];
|
|
||||||
}
|
|
||||||
if (preg_match('/^VERSION_ID=(.*)$/', $line, $matches)) {
|
|
||||||
$ret['ver'] = $matches[1];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
$ret['dist'] = trim($ret['dist'], '"\'');
|
if (preg_match('/^ID_LIKE=(.*)$/', $line, $matches)) {
|
||||||
$ret['ver'] = trim($ret['ver'], '"\'');
|
$ret['family'] = $matches[1];
|
||||||
if (strcasecmp($ret['dist'], 'centos') === 0) {
|
|
||||||
$ret['dist'] = 'redhat';
|
|
||||||
}
|
}
|
||||||
break;
|
if (preg_match('/^VERSION_ID=(.*)$/', $line, $matches)) {
|
||||||
|
$ret['ver'] = $matches[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$ret['dist'] = trim($ret['dist'], '"\'');
|
||||||
|
$ret['ver'] = trim($ret['ver'], '"\'');
|
||||||
|
|
||||||
|
if (strcasecmp($ret['dist'], 'centos') === 0) {
|
||||||
|
$ret['dist'] = 'redhat';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user