-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotenv.php
More file actions
134 lines (115 loc) · 4.64 KB
/
dotenv.php
File metadata and controls
134 lines (115 loc) · 4.64 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types = 1);
if (count(get_included_files()) === 1) {
header("HTTP/1.1 404 Not Found");
die();
}
class DotEnv {
private string $env_file_name;
private array $env_vars;
private bool $modify_env_file;
private bool $using_dot_env;
private static string $env_var_separator = "=";
public function __construct(bool $using_dot_env = true, string $env_file_name = ".env") {
$this->env_vars = [];
$this->modify_env_file = false;
$this->using_dot_env = $using_dot_env;
if ($using_dot_env) {
$this->env_file_name = $env_file_name;
$env_lines = @file($env_file_name, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
//Only throw this exception if $env_file_name doesn't actually exist
if ($env_lines === false) {
throw new DotEnvException("Cannot open file \"$env_file_name\" or it doesn't exist");
}
foreach ($env_lines as $line) {
$equal_sign_pos = strpos($line, self::$env_var_separator);
$key = trim(substr($line, 0, $equal_sign_pos));
if ($key[0] !== "#") { //Treat lines beginning with "#" as comments
$value = trim(substr($line, $equal_sign_pos + 1));
$this->env_vars[$key] = $value;
putenv("$key=$value");
}
}
} else {
$this->env_file_name = "";
}
}
public function contains(string $key): bool {
return $this->indexOf($key) >= 0;
}
public function delete(string $key): void {
if ($this->using_dot_env) {
unset($this->env_vars[$key]);
if ($this->modify_env_file) {
$env_lines = file($this->env_file_name, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$fp = fopen($this->env_file_name, "w");
foreach ($env_lines as $line) {
$equal_sign_pos = strpos($line, self::$env_var_separator);
$line_key = rtrim(substr($line, 0, $equal_sign_pos));
if ($key !== $line_key) {
fwrite($fp, "$line\n");
}
}
fclose($fp);
}
}
putenv($key);
}
public function get(string $key): mixed {
return $this->using_dot_env
? ($this->env_vars[$key] ?? null)
: (getenv($key) ?: null);
}
public function get_env_file_name(): string {
return $this->env_file_name;
}
public function get_keys_by_value(mixed $value): array {
$arr = [];
foreach ($this->env_vars as $env_key => $env_value) {
if ($value === $env_value) {
$arr[] = $env_key;
}
}
return $arr;
}
public function indexOf(string $key): int {
return array_search($key, array_keys($this->env_vars)) ?: -1;
}
public function is_modifying_env_file(): bool {
return $this->modify_env_file;
}
public function is_using_dot_env(): bool {
return $this->using_dot_env;
}
public function num_vars(): int {
return count($this->using_dot_env ? $this->env_vars : $_ENV);
}
public function set(string $key, mixed $value): void {
if ($this->using_dot_env) {
$this->env_vars[$key] = $value;
if ($this->modify_env_file) {
$fp = fopen($this->env_file_name, "w");
fwrite($fp, $this->to_env_format());
fclose($fp);
}
}
putenv("$key=$value");
}
public function set_modify_env_file(bool $modify_env_file): void {
$this->modify_env_file = $modify_env_file;
}
public function set_using_dot_env(bool $using_dot_env): void {
$this->using_dot_env = $using_dot_env;
}
private function to_env_format(): string {
$str = "";
foreach ($this->env_vars as $key => $value) {
$str .= $key . self::$env_var_separator . $value . "\n";
}
return $str;
}
public function __toString(): string {
return var_export($this, true);
}
}
final class DotEnvException extends Exception {}