Skip to content

Commit b353979

Browse files
committed
up: add methods for set ENv
1 parent 23ceab2 commit b353979

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/Proc/ProcessUtil.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
use function posix_setuid;
4040
use function posix_geteuid;
4141
use function function_exists;
42+
use const DIRECTORY_SEPARATOR;
4243
use const PHP_OS;
4344

4445
/**
@@ -66,7 +67,11 @@ public static function isTtySupported(): bool
6667
static $isTtySupported;
6768

6869
if (null === $isTtySupported) {
69-
$isTtySupported = (bool) @proc_open('echo 1 >/dev/null', [['file', '/dev/tty', 'r'], ['file', '/dev/tty', 'w'], ['file', '/dev/tty', 'w']], $pipes);
70+
$isTtySupported = (bool)@proc_open('echo 1 >/dev/null', [
71+
['file', '/dev/tty', 'r'],
72+
['file', '/dev/tty', 'w'],
73+
['file', '/dev/tty', 'w']
74+
], $pipes);
7075
}
7176

7277
return $isTtySupported;
@@ -85,7 +90,7 @@ public static function isPtySupported(): bool
8590
return $result;
8691
}
8792

88-
if ('\\' === \DIRECTORY_SEPARATOR) {
93+
if ('\\' === DIRECTORY_SEPARATOR) {
8994
return $result = false;
9095
}
9196

@@ -180,17 +185,13 @@ public static function daemonRun(Closure $beforeQuit = null): int
180185
// chdir('/');
181186
// umask(0);
182187
break;
183-
184188
case -1: // fork failed.
185189
throw new RuntimeException('Fork new process is failed! exiting');
186-
break;
187-
188190
default: // at parent
189191
if ($beforeQuit) {
190192
$beforeQuit($pid);
191193
}
192-
193-
exit;
194+
exit(0);
194195
}
195196

196197
return $pid;
@@ -231,9 +232,9 @@ public static function forks(int $number, callable $onStart = null, callable $on
231232
}
232233

233234
$pidAry = [];
234-
235235
for ($id = 0; $id < $number; $id++) {
236-
$info = self::fork($onStart, $onError, $id);
236+
$info = self::fork($onStart, $onError, $id);
237+
// log
237238
$pidAry[$info['pid']] = $info;
238239
}
239240

src/SysEnv.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
use Toolkit\Stdlib\OS;
1313
use function defined;
14+
use function getenv;
15+
use function putenv;
1416

1517
/**
1618
* Class EnvHelper
@@ -47,4 +49,26 @@ public static function isSupportColor(): bool
4749

4850
return self::isInteractive(STDOUT);
4951
}
52+
53+
/**
54+
* @param string $key
55+
* @param string $default
56+
*
57+
* @return string
58+
*/
59+
public static function getEnv(string $key, string $default = ''): string
60+
{
61+
return getenv($key) ?: $default;
62+
}
63+
64+
/**
65+
* @param string $key
66+
* @param string|int $value
67+
*
68+
* @return bool
69+
*/
70+
public static function setEnv(string $key, $value): bool
71+
{
72+
return putenv($key . '=' . $value);
73+
}
5074
}

0 commit comments

Comments
 (0)