1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace msztorc\LaravelEnv; |
4
|
|
|
|
5
|
|
|
class Env { |
6
|
|
|
|
7
|
|
|
private $_envContent = null; |
8
|
|
|
private $_envVars = null; |
9
|
|
|
private $_envPath = null; |
10
|
|
|
private $_saved = false; |
11
|
|
|
private $_changed = false; |
12
|
|
|
|
13
|
|
|
public function __construct() |
14
|
|
|
{ |
15
|
|
|
/** @scrutinizer ignore-call */ $this->_envPath = app()->environmentFilePath(); |
16
|
|
|
$this->_envContent = file_get_contents($this->_envPath); |
17
|
|
|
|
18
|
|
|
$this->_parse(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Parse env content into array |
23
|
|
|
*/ |
24
|
|
|
private function _parse() { |
25
|
|
|
|
26
|
|
|
$env_lines = preg_split('/\r\n|\r|\n/', $this->_envContent); |
27
|
|
|
|
28
|
|
|
foreach ($env_lines as $line) |
29
|
|
|
{ |
30
|
|
|
if (strlen(trim($line)) && !(strpos(trim($line), '#') === 0)) { |
31
|
|
|
[$key, $val] = explode('=', (string)$line); |
32
|
|
|
$this->_envVars[$key] = $this->_stripValue($val); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check if the variable exists |
40
|
|
|
* @param string $key Environment variable key |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function exists(string $key): bool |
44
|
|
|
{ |
45
|
|
|
if (is_null($this->_envVars)) |
46
|
|
|
$this->_parse(); |
47
|
|
|
|
48
|
|
|
return isset($this->_envVars[$key]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the current env variable value |
53
|
|
|
* |
54
|
|
|
* @param string $key Environment variable key |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getValue(string $key): string |
58
|
|
|
{ |
59
|
|
|
if (is_null($this->_envVars)) |
60
|
|
|
$this->_parse(); |
61
|
|
|
|
62
|
|
|
return $this->_envVars[$key] ?? ''; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get env key-value |
68
|
|
|
* |
69
|
|
|
* @param string $key Environment variable key |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function getKeyValue(string $key): array |
73
|
|
|
{ |
74
|
|
|
if (is_null($this->_envVars)) |
75
|
|
|
$this->_parse(); |
76
|
|
|
|
77
|
|
|
return [$key => $this->_envVars[$key]] ?? []; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set env variable value |
83
|
|
|
* @param string $key Environment variable key |
84
|
|
|
* @param string $value Variable value |
85
|
|
|
* @param bool $write Write changes to .env file |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function setValue(string $key, string $value, $write = true): string |
89
|
|
|
{ |
90
|
|
|
$value = $this->_prepareValue($value); |
91
|
|
|
|
92
|
|
|
if ($this->exists($key)) { |
93
|
|
|
$this->_envContent = preg_replace("/^{$key}=.*$/m", "{$key}={$value}", $this->_envContent); |
94
|
|
|
} else { |
95
|
|
|
$this->_envContent .= PHP_EOL . "{$key}={$value}" . PHP_EOL; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->_changed = true; |
99
|
|
|
$this->_saved = false; |
100
|
|
|
|
101
|
|
|
$this->_parse(); |
102
|
|
|
if ($write) $this->write(); |
103
|
|
|
|
104
|
|
|
return $this->getValue($key); |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Delete environment variable |
111
|
|
|
* @param string $key Environment variable key |
112
|
|
|
* @param bool $write Write changes to .env file |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function deleteVariable(string $key, bool $write = true): bool |
116
|
|
|
{ |
117
|
|
|
if ($this->exists($key)) { |
118
|
|
|
$this->_envContent = preg_replace("/^{$key}=.*\s{0,1}/m", '', $this->_envContent); |
119
|
|
|
|
120
|
|
|
$this->_changed = true; |
121
|
|
|
$this->_saved = false; |
122
|
|
|
|
123
|
|
|
if ($write) |
124
|
|
|
$this->write(); |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return true; |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Check and prepare value to be safe |
134
|
|
|
* @param string $value |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
private function _prepareValue(string $value): string |
138
|
|
|
{ |
139
|
|
|
if (false !== strpos($value, ' ') || in_array($value[0], ['=', '$'])) { |
140
|
|
|
$value = '"' . $value . '"'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return preg_quote($value); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
private function _stripQuotes($value) { |
147
|
|
|
return preg_replace('/^(\'(.*)\'|"(.*)")$/', '$2$3', $value); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Strip output value from quotes and inline comments |
152
|
|
|
* @param string $value |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
private function _stripValue(string $value): string |
156
|
|
|
{ |
157
|
|
|
$val = trim(explode('#', trim($value))[0]); |
158
|
|
|
return stripslashes($this->_stripQuotes($val)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get all env variables |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
|
|
public function getVariables(): array |
166
|
|
|
{ |
167
|
|
|
return $this->_envVars; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get current env entire content from memory |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getEnvContent(): string |
175
|
|
|
{ |
176
|
|
|
return $this->_envContent; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Write env config to file |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
public function write(): bool |
184
|
|
|
{ |
185
|
|
|
$this->_saved = (false !== file_put_contents($this->_envPath, $this->_envContent) ?? true); |
186
|
|
|
return $this->_saved; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Check if the changes has been saved |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function isSaved(): bool |
194
|
|
|
{ |
195
|
|
|
return $this->_saved; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Check if there were any env content changes |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
|
|
public function wasChanged(): bool |
203
|
|
|
{ |
204
|
|
|
return $this->_changed; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
} |
208
|
|
|
|