Skip to content

Commit 234bbaf

Browse files
committed
feat: add new methods for get user info
Signed-off-by: inhere <[email protected]>
1 parent 5b799a1 commit 234bbaf

File tree

3 files changed

+115
-76
lines changed

3 files changed

+115
-76
lines changed

src/OS.php

+101-75
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use function is_writable;
2121
use function mkdir;
2222
use function php_uname;
23+
use function posix_getpwuid;
2324
use function posix_getuid;
2425
use function putenv;
2526
use function rtrim;
@@ -34,100 +35,166 @@
3435
*/
3536
class OS
3637
{
38+
/**************************************************************************
39+
* user info
40+
*************************************************************************/
41+
42+
/** @var string|null */
43+
private static $homeDir;
44+
3745
/**
46+
* @param bool $refresh
47+
*
3848
* @return string
3949
*/
40-
public static function name(): string
50+
public static function getUserHomeDir(bool $refresh = false): string
4151
{
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;
4455
}
4556

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;
4771
}
4872

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+
}
5283

5384
/**
54-
* @return bool
85+
* @param string $path
86+
*
87+
* @return string eg: ~/.config/kite.php
5588
*/
56-
public static function isUnix(): bool
89+
public static function userConfigDir(string $path = ''): string
5790
{
58-
$uNames = ['CYG', 'DAR', 'FRE', 'HP-', 'IRI', 'LIN', 'NET', 'OPE', 'SUN', 'UNI'];
91+
return self::getUserHomeDir() . '/.config' . ($path ? "/$path" : '');
92+
}
5993

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" : '');
61102
}
62103

63104
/**
64105
* @return bool
65106
*/
66-
public static function isLinux(): bool
107+
public static function isRoot(): bool
67108
{
68-
return stripos(self::name(), 'LIN') !== false;
109+
return self::isRootUser();
69110
}
70111

71112
/**
72113
* @return bool
73114
*/
74-
public static function isWin(): bool
115+
public static function isRootUser(): bool
75116
{
76-
return self::isWindows();
117+
if (function_exists('posix_getuid')) {
118+
return posix_getuid() === 0;
119+
}
120+
121+
return getmyuid() === 0;
77122
}
78123

79124
/**
80-
* @return bool
125+
* Get unix user of current process.
126+
*
127+
* @return array
81128
*/
82-
public static function isWindows(): bool
129+
public static function getCurrentUser(): array
83130
{
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;
85148
}
86149

87150
/**
88151
* @return bool
89152
*/
90-
public static function isMac(): bool
153+
public static function isUnix(): bool
91154
{
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);
93158
}
94159

95160
/**
96161
* @return bool
97162
*/
98-
public static function isMacOS(): bool
163+
public static function isLinux(): bool
99164
{
100-
return stripos(self::name(), 'Darwin') !== false;
165+
return stripos(self::name(), 'LIN') !== false;
101166
}
102167

103168
/**
104169
* @return bool
105170
*/
106-
public static function isRoot(): bool
171+
public static function isWin(): bool
107172
{
108-
return self::isRootUser();
173+
return self::isWindows();
109174
}
110175

111176
/**
112177
* @return bool
113178
*/
114-
public static function isRootUser(): bool
179+
public static function isWindows(): bool
115180
{
116-
if (function_exists('posix_getuid')) {
117-
return posix_getuid() === 0;
118-
}
181+
return stripos(self::name(), 'Windows') !== false;
182+
}
119183

120-
return getmyuid() === 0;
184+
/**
185+
* @return bool
186+
*/
187+
public static function isMac(): bool
188+
{
189+
return self::isMacOS();
121190
}
122191

123192
/**
124-
* Get unix user of current process.
125-
*
126-
* @return array
193+
* @return bool
127194
*/
128-
public static function getCurrentUser(): array
195+
public static function isMacOS(): bool
129196
{
130-
return posix_getpwuid(posix_getuid());
197+
return stripos(self::name(), 'Darwin') !== false;
131198
}
132199

133200
/**
@@ -168,47 +235,6 @@ public static function getTempDir(): string
168235
return $tmp;
169236
}
170237

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-
212238
/**
213239
* @param string $key
214240
* @param string $default

src/Std.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib;
4+
5+
/**
6+
* Class Std
7+
*
8+
* @package Toolkit\Stdlib
9+
*/
10+
class Std
11+
{
12+
13+
}

test/OSTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ class OSTest extends TestCase
2121
{
2222
public function testGetUserHomeDir(): void
2323
{
24-
self::assertNotEmpty(OS::useHomeDir());
24+
self::assertNotEmpty(OS::userHomeDir());
2525
}
2626
}

0 commit comments

Comments
 (0)