|
10 | 10 | namespace Toolkit\Stdlib;
|
11 | 11 |
|
12 | 12 | use function defined;
|
| 13 | +use function explode; |
13 | 14 | use function function_exists;
|
| 15 | +use function getenv; |
14 | 16 | use function getmyuid;
|
15 | 17 | use function in_array;
|
16 | 18 | use function php_uname;
|
17 | 19 | use function posix_getuid;
|
| 20 | +use function putenv; |
| 21 | +use function rtrim; |
18 | 22 | use function stripos;
|
19 | 23 | use const PHP_OS;
|
20 | 24 | use const PHP_OS_FAMILY;
|
@@ -152,6 +156,97 @@ public static function getTempDir(): string
|
152 | 156 | return $tmp;
|
153 | 157 | }
|
154 | 158 |
|
| 159 | + /** @var string|null */ |
| 160 | + private static $homeDir; |
| 161 | + |
| 162 | + /** |
| 163 | + * @param bool $refresh |
| 164 | + * |
| 165 | + * @return string |
| 166 | + */ |
| 167 | + public static function useHomeDir(bool $refresh = false): string |
| 168 | + { |
| 169 | + return self::getUserHomeDir($refresh); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @param bool $refresh |
| 174 | + * |
| 175 | + * @return string |
| 176 | + */ |
| 177 | + public static function getUserHomeDir(bool $refresh = false): string |
| 178 | + { |
| 179 | + // has cache value. |
| 180 | + if (self::$homeDir && $refresh === false) { |
| 181 | + return self::$homeDir; |
| 182 | + } |
| 183 | + |
| 184 | + if (!$home = self::getEnvVal('HOME')) { |
| 185 | + $isWin = self::isWindows(); |
| 186 | + |
| 187 | + // home on windows |
| 188 | + if ($isWin && !empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) { |
| 189 | + $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH']; |
| 190 | + // If HOMEPATH is a root directory the path can end with a slash. |
| 191 | + // Make sure that doesn't happen. |
| 192 | + $home = rtrim($home, '\\/'); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + self::$homeDir = $home; |
| 197 | + return $home; |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * @param string $key |
| 202 | + * @param string $default |
| 203 | + * |
| 204 | + * @return string |
| 205 | + */ |
| 206 | + public static function getEnvVal(string $key, string $default = ''): string |
| 207 | + { |
| 208 | + return getenv($key) ?: (string)($_SERVER[$key] ?? $default); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * @param string $key |
| 213 | + * @param string|int $value |
| 214 | + * |
| 215 | + * @return bool |
| 216 | + */ |
| 217 | + public static function setEnvVar(string $key, $value): bool |
| 218 | + { |
| 219 | + $_ENV[$key] = $value; |
| 220 | + $_SERVER[$key] = $value; |
| 221 | + return putenv($key . '=' . $value); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * @param array $kvMap |
| 226 | + * |
| 227 | + * @return void |
| 228 | + */ |
| 229 | + public static function setEnvVars(array $kvMap): void |
| 230 | + { |
| 231 | + foreach ($kvMap as $key => $value) { |
| 232 | + self::setEnvVar($key, $value); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * @return array |
| 238 | + */ |
| 239 | + public static function getEnvPaths(): array |
| 240 | + { |
| 241 | + $pathStr = $_SERVER['PATH'] ?? ''; |
| 242 | + if (!$pathStr) { |
| 243 | + return []; |
| 244 | + } |
| 245 | + |
| 246 | + $sepChar = self::isWindows() ? ';' : ':'; |
| 247 | + return explode($sepChar, $pathStr); |
| 248 | + } |
| 249 | + |
155 | 250 | /**
|
156 | 251 | * @return string
|
157 | 252 | */
|
@@ -179,7 +274,7 @@ public static function getNullDevice(): string
|
179 | 274 | *
|
180 | 275 | * @return boolean
|
181 | 276 | */
|
182 |
| - public static function isInteractive($fileDescriptor): bool |
| 277 | + public static function isInteractive(int $fileDescriptor): bool |
183 | 278 | {
|
184 | 279 | return function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
|
185 | 280 | }
|
|
0 commit comments