Skip to content

Commit 919cc56

Browse files
committed
feat: add new method for get use home dir
1 parent b8e2534 commit 919cc56

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

src/OS.php

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
namespace Toolkit\Stdlib;
1111

1212
use function defined;
13+
use function explode;
1314
use function function_exists;
15+
use function getenv;
1416
use function getmyuid;
1517
use function in_array;
1618
use function php_uname;
1719
use function posix_getuid;
20+
use function putenv;
21+
use function rtrim;
1822
use function stripos;
1923
use const PHP_OS;
2024
use const PHP_OS_FAMILY;
@@ -152,6 +156,97 @@ public static function getTempDir(): string
152156
return $tmp;
153157
}
154158

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+
155250
/**
156251
* @return string
157252
*/
@@ -179,7 +274,7 @@ public static function getNullDevice(): string
179274
*
180275
* @return boolean
181276
*/
182-
public static function isInteractive($fileDescriptor): bool
277+
public static function isInteractive(int $fileDescriptor): bool
183278
{
184279
return function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
185280
}

test/OSTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\StdlibTest;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Toolkit\Stdlib\OS;
7+
8+
/**
9+
* Class OSTest
10+
*
11+
* @package Toolkit\StdlibTest
12+
*/
13+
class OSTest extends TestCase
14+
{
15+
public function testGetUserHomeDir(): void
16+
{
17+
self::assertNotEmpty(OS::useHomeDir());
18+
}
19+
}

test/bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515

1616
if (0 === strpos($class, 'Toolkit\Stdlib\Example\\')) {
1717
$path = str_replace('\\', '/', substr($class, strlen('Toolkit\Stdlib\Example\\')));
18-
$file = dirname(__DIR__) . "/example/{$path}.php";
18+
$file = dirname(__DIR__) . "/example/$path.php";
1919
} elseif (0 === strpos($class, 'Toolkit\StdlibTest\\')) {
2020
$path = str_replace('\\', '/', substr($class, strlen('Toolkit\StdlibTest\\')));
2121
$file = __DIR__ . "/{$path}.php";
2222
} elseif (0 === strpos($class, 'Toolkit\Stdlib\\')) {
2323
$path = str_replace('\\', '/', substr($class, strlen('Toolkit\Stdlib\\')));
24-
$file = dirname(__DIR__) . "/src/{$path}.php";
24+
$file = dirname(__DIR__) . "/src/$path.php";
2525
}
2626

2727
if ($file && is_file($file)) {

0 commit comments

Comments
 (0)