-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnv.php
More file actions
69 lines (59 loc) · 1.2 KB
/
Env.php
File metadata and controls
69 lines (59 loc) · 1.2 KB
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
65
66
67
68
69
<?php
namespace Teleport\Utils;
class Env
{
/**
* Load env from directory.
*
* @param string $dir
* @param string $envFile
* @param bool $typed
* @return void
*/
public static function load(string $dir, string $envFile = ".env.ini", bool $typed = true): void
{
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $envFile) === false) {
throw new \Exception("Unable to load env file: {$file}");
}
foreach (parse_ini_file($file, false, $typed ? INI_SCANNER_TYPED : INI_SCANNER_NORMAL) as $k => $v)
{
static::set($k, $v);
}
}
/**
* Get form env
*
* @param string $k
* @param scalar $default
* @return mixed
*/
public static function get(string $k, $default = null)
{
if (isset($_ENV[$k])) {
return $_ENV[$k];
}
return getenv($k) ?: $default;
}
/**
* Set env value
*
* @param string $k
* @param scalar $v
* @return bool
*/
public static function set(string $k, $v): bool
{
$_ENV[$k] = $v;
return putenv("{$k}={$v}");
}
/**
* Check server api type (eg: apache, cli, cgi).
*
* @param string $sapi
* @return bool
*/
public static function is(string $sapi): bool
{
return (substr(PHP_SAPI, 0, strlen($sapi)) === $sapi);
}
}