-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathset_var.php
31 lines (24 loc) · 834 Bytes
/
set_var.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
<?php
declare(strict_types=1);
namespace Psl\Env;
use Psl;
use function putenv;
use function str_contains;
/**
* Sets the environment variable $key to the value $value for the currently running process.
*
* @param non-empty-string $key
*
* @throws Psl\Exception\InvariantViolationException If $key contains an ASCII equals sign `=`, the NUL character `\0`,
* or when the $value contains the NUL character.
*/
function set_var(string $key, string $value): void
{
if (str_contains($key, '=') || str_contains($key, "\0")) {
Psl\invariant_violation('Invalid environment variable key provided.');
}
if (str_contains($value, "\0")) {
Psl\invariant_violation('Invalid environment variable value provided.');
}
putenv($key . '=' . $value);
}