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"]); $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; $this->type = self::TYPE_READLINE;
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

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