|
20 | 20 | use function is_writable;
|
21 | 21 | use function mkdir;
|
22 | 22 | use function php_uname;
|
| 23 | +use function posix_getpwuid; |
23 | 24 | use function posix_getuid;
|
24 | 25 | use function putenv;
|
25 | 26 | use function rtrim;
|
|
34 | 35 | */
|
35 | 36 | class OS
|
36 | 37 | {
|
| 38 | + /************************************************************************** |
| 39 | + * user info |
| 40 | + *************************************************************************/ |
| 41 | + |
| 42 | + /** @var string|null */ |
| 43 | + private static $homeDir; |
| 44 | + |
37 | 45 | /**
|
| 46 | + * @param bool $refresh |
| 47 | + * |
38 | 48 | * @return string
|
39 | 49 | */
|
40 |
| - public static function name(): string |
| 50 | + public static function getUserHomeDir(bool $refresh = false): string |
41 | 51 | {
|
42 |
| - if (defined('PHP_OS_FAMILY')) { |
43 |
| - return PHP_OS_FAMILY; |
| 52 | + // has cache value. |
| 53 | + if (self::$homeDir && $refresh === false) { |
| 54 | + return self::$homeDir; |
44 | 55 | }
|
45 | 56 |
|
46 |
| - return PHP_OS; |
| 57 | + if (!$home = self::getEnvVal('HOME')) { |
| 58 | + $isWin = self::isWindows(); |
| 59 | + |
| 60 | + // home on windows |
| 61 | + if ($isWin && !empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) { |
| 62 | + $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH']; |
| 63 | + // If HOMEPATH is a root directory the path can end with a slash. |
| 64 | + // Make sure that doesn't happen. |
| 65 | + $home = rtrim($home, '\\/'); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + self::$homeDir = $home; |
| 70 | + return $home; |
47 | 71 | }
|
48 | 72 |
|
49 |
| - /************************************************************************** |
50 |
| - * system env |
51 |
| - *************************************************************************/ |
| 73 | + /** |
| 74 | + * @param string $path |
| 75 | + * @param bool $refresh |
| 76 | + * |
| 77 | + * @return string |
| 78 | + */ |
| 79 | + public static function userHomeDir(string $path = '', bool $refresh = false): string |
| 80 | + { |
| 81 | + return self::getUserHomeDir($refresh) . ($path ? "/$path" : ''); |
| 82 | + } |
52 | 83 |
|
53 | 84 | /**
|
54 |
| - * @return bool |
| 85 | + * @param string $path |
| 86 | + * |
| 87 | + * @return string eg: ~/.config/kite.php |
55 | 88 | */
|
56 |
| - public static function isUnix(): bool |
| 89 | + public static function userConfigDir(string $path = ''): string |
57 | 90 | {
|
58 |
| - $uNames = ['CYG', 'DAR', 'FRE', 'HP-', 'IRI', 'LIN', 'NET', 'OPE', 'SUN', 'UNI']; |
| 91 | + return self::getUserHomeDir() . '/.config' . ($path ? "/$path" : ''); |
| 92 | + } |
59 | 93 |
|
60 |
| - return in_array(strtoupper(substr(PHP_OS, 0, 3)), $uNames, true); |
| 94 | + /** |
| 95 | + * @param string $path |
| 96 | + * |
| 97 | + * @return string eg: ~/.cache/kite/some.log |
| 98 | + */ |
| 99 | + public static function userCacheDir(string $path = ''): string |
| 100 | + { |
| 101 | + return self::getUserHomeDir() . '/.cache' . ($path ? "/$path" : ''); |
61 | 102 | }
|
62 | 103 |
|
63 | 104 | /**
|
64 | 105 | * @return bool
|
65 | 106 | */
|
66 |
| - public static function isLinux(): bool |
| 107 | + public static function isRoot(): bool |
67 | 108 | {
|
68 |
| - return stripos(self::name(), 'LIN') !== false; |
| 109 | + return self::isRootUser(); |
69 | 110 | }
|
70 | 111 |
|
71 | 112 | /**
|
72 | 113 | * @return bool
|
73 | 114 | */
|
74 |
| - public static function isWin(): bool |
| 115 | + public static function isRootUser(): bool |
75 | 116 | {
|
76 |
| - return self::isWindows(); |
| 117 | + if (function_exists('posix_getuid')) { |
| 118 | + return posix_getuid() === 0; |
| 119 | + } |
| 120 | + |
| 121 | + return getmyuid() === 0; |
77 | 122 | }
|
78 | 123 |
|
79 | 124 | /**
|
80 |
| - * @return bool |
| 125 | + * Get unix user of current process. |
| 126 | + * |
| 127 | + * @return array |
81 | 128 | */
|
82 |
| - public static function isWindows(): bool |
| 129 | + public static function getCurrentUser(): array |
83 | 130 | {
|
84 |
| - return stripos(self::name(), 'Windows') !== false; |
| 131 | + return posix_getpwuid(posix_getuid()); |
| 132 | + } |
| 133 | + |
| 134 | + /************************************************************************** |
| 135 | + * system env |
| 136 | + *************************************************************************/ |
| 137 | + |
| 138 | + /** |
| 139 | + * @return string |
| 140 | + */ |
| 141 | + public static function name(): string |
| 142 | + { |
| 143 | + if (defined('PHP_OS_FAMILY')) { |
| 144 | + return PHP_OS_FAMILY; |
| 145 | + } |
| 146 | + |
| 147 | + return PHP_OS; |
85 | 148 | }
|
86 | 149 |
|
87 | 150 | /**
|
88 | 151 | * @return bool
|
89 | 152 | */
|
90 |
| - public static function isMac(): bool |
| 153 | + public static function isUnix(): bool |
91 | 154 | {
|
92 |
| - return self::isMacOS(); |
| 155 | + $uNames = ['CYG', 'DAR', 'FRE', 'HP-', 'IRI', 'LIN', 'NET', 'OPE', 'SUN', 'UNI']; |
| 156 | + |
| 157 | + return in_array(strtoupper(substr(PHP_OS, 0, 3)), $uNames, true); |
93 | 158 | }
|
94 | 159 |
|
95 | 160 | /**
|
96 | 161 | * @return bool
|
97 | 162 | */
|
98 |
| - public static function isMacOS(): bool |
| 163 | + public static function isLinux(): bool |
99 | 164 | {
|
100 |
| - return stripos(self::name(), 'Darwin') !== false; |
| 165 | + return stripos(self::name(), 'LIN') !== false; |
101 | 166 | }
|
102 | 167 |
|
103 | 168 | /**
|
104 | 169 | * @return bool
|
105 | 170 | */
|
106 |
| - public static function isRoot(): bool |
| 171 | + public static function isWin(): bool |
107 | 172 | {
|
108 |
| - return self::isRootUser(); |
| 173 | + return self::isWindows(); |
109 | 174 | }
|
110 | 175 |
|
111 | 176 | /**
|
112 | 177 | * @return bool
|
113 | 178 | */
|
114 |
| - public static function isRootUser(): bool |
| 179 | + public static function isWindows(): bool |
115 | 180 | {
|
116 |
| - if (function_exists('posix_getuid')) { |
117 |
| - return posix_getuid() === 0; |
118 |
| - } |
| 181 | + return stripos(self::name(), 'Windows') !== false; |
| 182 | + } |
119 | 183 |
|
120 |
| - return getmyuid() === 0; |
| 184 | + /** |
| 185 | + * @return bool |
| 186 | + */ |
| 187 | + public static function isMac(): bool |
| 188 | + { |
| 189 | + return self::isMacOS(); |
121 | 190 | }
|
122 | 191 |
|
123 | 192 | /**
|
124 |
| - * Get unix user of current process. |
125 |
| - * |
126 |
| - * @return array |
| 193 | + * @return bool |
127 | 194 | */
|
128 |
| - public static function getCurrentUser(): array |
| 195 | + public static function isMacOS(): bool |
129 | 196 | {
|
130 |
| - return posix_getpwuid(posix_getuid()); |
| 197 | + return stripos(self::name(), 'Darwin') !== false; |
131 | 198 | }
|
132 | 199 |
|
133 | 200 | /**
|
@@ -168,47 +235,6 @@ public static function getTempDir(): string
|
168 | 235 | return $tmp;
|
169 | 236 | }
|
170 | 237 |
|
171 |
| - /** @var string|null */ |
172 |
| - private static $homeDir; |
173 |
| - |
174 |
| - /** |
175 |
| - * @param bool $refresh |
176 |
| - * |
177 |
| - * @return string |
178 |
| - */ |
179 |
| - public static function useHomeDir(bool $refresh = false): string |
180 |
| - { |
181 |
| - return self::getUserHomeDir($refresh); |
182 |
| - } |
183 |
| - |
184 |
| - /** |
185 |
| - * @param bool $refresh |
186 |
| - * |
187 |
| - * @return string |
188 |
| - */ |
189 |
| - public static function getUserHomeDir(bool $refresh = false): string |
190 |
| - { |
191 |
| - // has cache value. |
192 |
| - if (self::$homeDir && $refresh === false) { |
193 |
| - return self::$homeDir; |
194 |
| - } |
195 |
| - |
196 |
| - if (!$home = self::getEnvVal('HOME')) { |
197 |
| - $isWin = self::isWindows(); |
198 |
| - |
199 |
| - // home on windows |
200 |
| - if ($isWin && !empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) { |
201 |
| - $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH']; |
202 |
| - // If HOMEPATH is a root directory the path can end with a slash. |
203 |
| - // Make sure that doesn't happen. |
204 |
| - $home = rtrim($home, '\\/'); |
205 |
| - } |
206 |
| - } |
207 |
| - |
208 |
| - self::$homeDir = $home; |
209 |
| - return $home; |
210 |
| - } |
211 |
| - |
212 | 238 | /**
|
213 | 239 | * @param string $key
|
214 | 240 | * @param string $default
|
|
0 commit comments