From a370c1c0c94319a731d1307538068d42d5f712be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Monta=C3=B1ez?= Date: Sun, 10 Apr 2022 18:53:26 -0300 Subject: [PATCH] [Galactica] Improve Windows compatibility --- src/Runtime/Runtime.php | 15 ++++++++++----- tests/Runtime/RuntimeWindowsMockup.php | 5 +++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Runtime/Runtime.php b/src/Runtime/Runtime.php index f0f30bb..6b68611 100644 --- a/src/Runtime/Runtime.php +++ b/src/Runtime/Runtime.php @@ -75,6 +75,11 @@ class Runtime return stripos(PHP_OS, 'WIN') === 0; } + public function hasPosix(): bool + { + return function_exists('posix_getpwuid'); + } + /** * Generate the Release ID */ @@ -471,13 +476,13 @@ class Runtime */ public function getCurrentUser(): string { - // Windows fallback - if (!function_exists('posix_getpwuid')) { - return getenv('USERNAME') ?: ''; + if ($this->hasPosix()) { + $userData = posix_getpwuid(posix_geteuid()); + return $userData['name']; } - $userData = posix_getpwuid(posix_geteuid()); - return $userData['name']; + // Windows fallback + return strval(getenv('USERNAME')); } /** diff --git a/tests/Runtime/RuntimeWindowsMockup.php b/tests/Runtime/RuntimeWindowsMockup.php index 0a9c640..050aaa1 100644 --- a/tests/Runtime/RuntimeWindowsMockup.php +++ b/tests/Runtime/RuntimeWindowsMockup.php @@ -16,4 +16,9 @@ class RuntimeWindowsMockup extends RuntimeMockup { return true; } + + public function hasPosix(): bool + { + return false; + } }