Utils: added OS constants, remove hardcoded OS strings everywhere

This commit is contained in:
Dylan K. Taylor 2020-05-23 11:05:58 +01:00
parent 45c89d084c
commit ecbf21acea
6 changed files with 41 additions and 33 deletions

View File

@ -68,7 +68,7 @@ class CommandReader extends Thread{
$opts = getopt("", ["disable-readline", "enable-readline"]);
if(extension_loaded("readline") and (Utils::getOS() === "win" ? isset($opts["enable-readline"]) : !isset($opts["disable-readline"])) and !$this->isPipe(STDIN)){
if(extension_loaded("readline") and (Utils::getOS() === Utils::OS_WINDOWS ? isset($opts["enable-readline"]) : !isset($opts["disable-readline"])) and !$this->isPipe(STDIN)){
$this->type = self::TYPE_READLINE;
}
}

View File

@ -37,7 +37,7 @@ abstract class UPnP{
if(!Internet::$online){
throw new \RuntimeException("Server is offline");
}
if(Utils::getOS() !== "win"){
if(Utils::getOS() !== Utils::OS_WINDOWS){
throw new \RuntimeException("UPnP is only supported on Windows");
}
if(!class_exists("COM")){
@ -62,7 +62,7 @@ abstract class UPnP{
if(!Internet::$online){
return false;
}
if(Utils::getOS() != "win" or !class_exists("COM")){
if(Utils::getOS() !== Utils::OS_WINDOWS or !class_exists("COM")){
return false;
}

View File

@ -54,7 +54,7 @@ final class Process{
$reserved = memory_get_usage();
$VmSize = null;
$VmRSS = null;
if(Utils::getOS() === "linux" or Utils::getOS() === "android"){
if(Utils::getOS() === Utils::OS_LINUX or Utils::getOS() === Utils::OS_ANDROID){
$status = @file_get_contents("/proc/self/status");
if($status === false) throw new AssumptionFailedError("/proc/self/status should always be accessible");
@ -92,7 +92,7 @@ final class Process{
$stack = 0;
$heap = 0;
if(Utils::getOS() === "linux" or Utils::getOS() === "android"){
if(Utils::getOS() === Utils::OS_LINUX or Utils::getOS() === Utils::OS_ANDROID){
$mappings = @file("/proc/self/maps");
if($mappings === false) throw new AssumptionFailedError("/proc/self/maps should always be accessible");
foreach($mappings as $line){
@ -110,7 +110,7 @@ final class Process{
}
public static function getThreadCount() : int{
if(Utils::getOS() === "linux" or Utils::getOS() === "android"){
if(Utils::getOS() === Utils::OS_LINUX or Utils::getOS() === Utils::OS_ANDROID){
$status = @file_get_contents("/proc/self/status");
if($status === false) throw new AssumptionFailedError("/proc/self/status should always be accessible");
if(preg_match("/Threads:[ \t]+([0-9]+)/", $status, $matches) > 0){
@ -131,11 +131,11 @@ final class Process{
MainLogger::getLogger()->syncFlushBuffer();
}
switch(Utils::getOS()){
case "win":
case Utils::OS_WINDOWS:
exec("taskkill.exe /F /PID $pid > NUL");
break;
case "mac":
case "linux":
case Utils::OS_MACOS:
case Utils::OS_LINUX:
default:
if(function_exists("posix_kill")){
posix_kill($pid, 9); //SIGKILL

View File

@ -182,14 +182,14 @@ abstract class Terminal{
}
switch(Utils::getOS()){
case "linux":
case "mac":
case "bsd":
case Utils::OS_LINUX:
case Utils::OS_MACOS:
case Utils::OS_BSD:
self::getEscapeCodes();
return;
case "win":
case "android":
case Utils::OS_WINDOWS:
case Utils::OS_ANDROID:
self::getFallbackEscapeCodes();
return;
}

View File

@ -113,7 +113,7 @@ abstract class Timezone{
*/
public static function detectSystemTimezone(){
switch(Utils::getOS()){
case 'win':
case Utils::OS_WINDOWS:
$regex = '/(UTC)(\+*\-*\d*\d*\:*\d*\d*)/';
/*
@ -148,7 +148,7 @@ abstract class Timezone{
}
return self::parseOffset($offset);
case 'linux':
case Utils::OS_LINUX:
// Ubuntu / Debian.
$data = @file_get_contents('/etc/timezone');
if($data !== false){
@ -170,7 +170,7 @@ abstract class Timezone{
}
return self::parseOffset($offset);
case 'mac':
case Utils::OS_MACOS:
$filename = @readlink('/etc/localtime');
if($filename !== false and strpos($filename, '/usr/share/zoneinfo/') === 0){
$timezone = substr($filename, 20);

View File

@ -99,6 +99,14 @@ use const STR_PAD_RIGHT;
* Big collection of functions
*/
class Utils{
public const OS_WINDOWS = "win";
public const OS_IOS = "ios";
public const OS_MACOS = "mac";
public const OS_ANDROID = "android";
public const OS_LINUX = "linux";
public const OS_BSD = "bsd";
public const OS_UNKNOWN = "other";
/** @var string|null */
public static $os;
/** @var UUID|null */
@ -186,7 +194,7 @@ class Utils{
$machine .= sys_get_temp_dir();
$machine .= $extra;
$os = Utils::getOS();
if($os === "win"){
if($os === Utils::OS_WINDOWS){
@exec("ipconfig /ALL", $mac);
$mac = implode("\n", $mac);
if(preg_match_all("#Physical Address[. ]{1,}: ([0-9A-F\\-]{17})#", $mac, $matches) > 0){
@ -197,7 +205,7 @@ class Utils{
}
$machine .= implode(" ", $matches[1]); //Mac Addresses
}
}elseif($os === "linux"){
}elseif($os === Utils::OS_LINUX){
if(file_exists("/etc/machine-id")){
$machine .= file_get_contents("/etc/machine-id");
}else{
@ -212,9 +220,9 @@ class Utils{
$machine .= implode(" ", $matches[1]); //Mac Addresses
}
}
}elseif($os === "android"){
}elseif($os === Utils::OS_ANDROID){
$machine .= @file_get_contents("/system/build.prop");
}elseif($os === "mac"){
}elseif($os === Utils::OS_MACOS){
$machine .= `system_profiler SPHardwareDataType | grep UUID`;
}
$data = $machine . PHP_MAXPATHLEN;
@ -261,22 +269,22 @@ class Utils{
$uname = php_uname("s");
if(stripos($uname, "Darwin") !== false){
if(strpos(php_uname("m"), "iP") === 0){
self::$os = "ios";
self::$os = self::OS_IOS;
}else{
self::$os = "mac";
self::$os = self::OS_MACOS;
}
}elseif(stripos($uname, "Win") !== false or $uname === "Msys"){
self::$os = "win";
self::$os = self::OS_WINDOWS;
}elseif(stripos($uname, "Linux") !== false){
if(@file_exists("/system/build.prop")){
self::$os = "android";
self::$os = self::OS_ANDROID;
}else{
self::$os = "linux";
self::$os = self::OS_LINUX;
}
}elseif(stripos($uname, "BSD") !== false or $uname === "DragonFly"){
self::$os = "bsd";
self::$os = self::OS_BSD;
}else{
self::$os = "other";
self::$os = self::OS_UNKNOWN;
}
}
@ -322,8 +330,8 @@ class Utils{
}
switch(Utils::getOS()){
case "linux":
case "android":
case Utils::OS_LINUX:
case Utils::OS_ANDROID:
if(($cpuinfo = @file('/proc/cpuinfo')) !== false){
foreach($cpuinfo as $l){
if(preg_match('/^processor[ \t]*:[ \t]*[0-9]+$/m', $l) > 0){
@ -336,11 +344,11 @@ class Utils{
}
}
break;
case "bsd":
case "mac":
case Utils::OS_BSD:
case Utils::OS_MACOS:
$processors = (int) `sysctl -n hw.ncpu`;
break;
case "win":
case Utils::OS_WINDOWS:
$processors = (int) getenv("NUMBER_OF_PROCESSORS");
break;
}