-
Notifications
You must be signed in to change notification settings - Fork 16
/
PlatformHelper.php
64 lines (53 loc) 路 1.56 KB
/
PlatformHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace Castor;
use JoliCode\PhpOsHelper\OsHelper;
/**
* Platform helper inspired by Composer's Platform class.
*
* @internal
*/
final class PlatformHelper
{
/**
* getenv() equivalent but reads from the runtime global variables first.
*/
public static function getEnv(string $name): string|false
{
if (\array_key_exists($name, $_SERVER)) {
return (string) $_SERVER[$name];
}
if (\array_key_exists($name, $_ENV)) {
return (string) $_ENV[$name];
}
return getenv($name);
}
public static function getCacheDirectory(): string
{
try {
$home = self::getUserDirectory();
$directory = $home ? $home . '/.cache' : sys_get_temp_dir();
} catch (\RuntimeException $e) {
$directory = sys_get_temp_dir();
}
return $directory . '/castor';
}
/**
* @throws \RuntimeException If the user home could not reliably be determined
*/
private static function getUserDirectory(): string
{
if (false !== ($home = self::getEnv('HOME'))) {
return $home;
}
if (OsHelper::isWindows() && false !== ($home = self::getEnv('USERPROFILE'))) {
return $home;
}
if (\function_exists('posix_getuid') && \function_exists('posix_getpwuid')) {
$info = posix_getpwuid(posix_getuid());
if ($info) {
return $info['dir'];
}
}
throw new \RuntimeException('Could not determine user directory.');
}
}