|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
namespace Enjoys\Dotenv; |
|
7
|
|
|
|
|
8
|
|
|
use Enjoys\Dotenv\Parser\Parser; |
|
9
|
|
|
use Enjoys\Dotenv\Parser\ParserInterface; |
|
10
|
|
|
|
|
11
|
|
|
final class Dotenv |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public const CLEAR_MEMORY_AFTER_LOAD_ENV = 1; |
|
15
|
|
|
public const CAST_TYPE_ENV_VALUE = 2; |
|
16
|
|
|
public const POPULATE_PUTENV = 4; |
|
17
|
|
|
public const POPULATE_SERVER = 8; |
|
18
|
|
|
|
|
19
|
|
|
private const CHARACTER_MAP = [ |
|
20
|
|
|
"\\n" => "\n", |
|
21
|
|
|
"\\\"" => "\"", |
|
22
|
|
|
"\\\\" => "\\", |
|
23
|
|
|
'\\\'' => "'", |
|
24
|
|
|
'\\t' => "\t" |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array<string, string|null> |
|
29
|
|
|
*/ |
|
30
|
|
|
private array $envRawArray = []; |
|
31
|
|
|
|
|
32
|
|
|
private EnvCollection $envCollection; |
|
33
|
|
|
private ParserInterface $parser; |
|
34
|
|
|
private Variables $variablesResolver; |
|
35
|
|
|
private StorageInterface $storage; |
|
36
|
|
|
|
|
37
|
52 |
|
public function __construct( |
|
38
|
|
|
private string $envFilePath, |
|
39
|
|
|
?StorageInterface $storage = null, |
|
40
|
|
|
?ParserInterface $parser = null, |
|
41
|
|
|
private int $flags = 0 |
|
42
|
|
|
) { |
|
43
|
52 |
|
$this->envCollection = new EnvCollection(); |
|
44
|
52 |
|
$this->parser = $parser ?? new Parser(); |
|
45
|
52 |
|
$this->storage = $storage ?? new Storage(); |
|
46
|
52 |
|
$this->variablesResolver = new Variables($this); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
45 |
|
public function loadEnv(bool $usePutEnv = false): void |
|
50
|
|
|
{ |
|
51
|
45 |
|
if ($usePutEnv) { |
|
52
|
4 |
|
$this->flags = $this->flags | self::POPULATE_PUTENV; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
45 |
|
$this->readFiles(); |
|
56
|
43 |
|
$this->writeEnvs(); |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
41 |
|
putenv(sprintf('ENJOYS_DOTENV=%s', implode(',', $this->envCollection->getKeys()))); |
|
60
|
|
|
|
|
61
|
41 |
|
if ($this->isClearMemory()) { |
|
62
|
1 |
|
$this->clearMemory(); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
45 |
|
private function readFiles(): void |
|
67
|
|
|
{ |
|
68
|
45 |
|
$this->storage->addPath($this->envFilePath . '.dist'); |
|
69
|
45 |
|
$this->storage->addPath($this->envFilePath); |
|
70
|
|
|
|
|
71
|
45 |
|
while (false !== $path = $this->storage->getPath()) { |
|
72
|
44 |
|
if (!$this->storage->isLoaded($path)) { |
|
73
|
44 |
|
$contents = file_get_contents($path); |
|
74
|
44 |
|
$this->envRawArray = array_merge($this->envRawArray, $this->parser->parseEnv($contents !== false ? $contents : '')); |
|
75
|
42 |
|
$this->storage->markLoaded($path); |
|
76
|
42 |
|
$this->storage->addPath( |
|
77
|
42 |
|
$this->envFilePath . '.' . ((getenv('APP_ENV') !== false ? (string)getenv('APP_ENV') : null) ?? $this->envRawArray['APP_ENV'] ?? '') |
|
78
|
42 |
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
43 |
|
private function writeEnvs(): void |
|
84
|
|
|
{ |
|
85
|
43 |
|
foreach ($this->envRawArray as $key => $value) { |
|
86
|
41 |
|
$this->populate($key, $value); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
48 |
|
public function handleValue(string $key, ?string $value): float|bool|int|string|null |
|
91
|
|
|
{ |
|
92
|
48 |
|
if ($value !== null) { |
|
93
|
47 |
|
$quoted = null; |
|
94
|
47 |
|
$value = preg_replace_callback('/^(?<quote>[\'"])?(?<value>.*)\1/', function ($matches) { |
|
95
|
24 |
|
return match ($matches['quote']) { |
|
96
|
24 |
|
"'" => $matches['value'], |
|
97
|
24 |
|
"\"" => strtr($matches['value'], self::CHARACTER_MAP) |
|
98
|
24 |
|
}; |
|
99
|
47 |
|
}, $value, count: $quoted); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
48 |
|
$value = $this->variablesResolver->resolve($key, $value); |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
47 |
|
if (getenv($key) !== false) { |
|
106
|
6 |
|
$value = getenv($key); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
47 |
|
return ($this->isCastType() && ($quoted ?? null) === 0) ? ValueTypeCasting::castType($value) : $value; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
41 |
|
public function populate( |
|
113
|
|
|
string $key, |
|
114
|
|
|
string|null $value, |
|
115
|
|
|
): void { |
|
116
|
41 |
|
$value = $this->handleValue($key, $value); |
|
117
|
|
|
|
|
118
|
40 |
|
$_ENV[$key] = $value; |
|
119
|
40 |
|
$this->envCollection->add($key, $value); |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
40 |
|
if (getenv($key) === false && $this->isUsePutEnv() === true) { |
|
123
|
5 |
|
putenv(sprintf("%s=%s", $key, Variables::scalarValueToString($value))); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
40 |
|
if ($this->isPopulateToServer()) { |
|
127
|
1 |
|
$_SERVER[$key] = $value; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
15 |
|
public function getEnvRawArray(): array |
|
132
|
|
|
{ |
|
133
|
15 |
|
return $this->envRawArray; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
23 |
|
public function getEnvCollection(): EnvCollection |
|
137
|
|
|
{ |
|
138
|
23 |
|
return $this->envCollection; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return string[] |
|
143
|
|
|
*/ |
|
144
|
3 |
|
public function getLoadedPaths(): array |
|
145
|
|
|
{ |
|
146
|
3 |
|
return $this->storage->getLoadedPaths(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
47 |
|
public static function clear(): void |
|
151
|
|
|
{ |
|
152
|
47 |
|
if (false !== $envs = getenv('ENJOYS_DOTENV')) { |
|
153
|
36 |
|
foreach (explode(',', $envs) as $key) { |
|
154
|
36 |
|
if (!empty($key)) { |
|
155
|
|
|
//unset |
|
156
|
34 |
|
putenv($key); |
|
157
|
34 |
|
unset($_ENV[$key], $_SERVER[$key]); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
47 |
|
putenv('ENJOYS_DOTENV'); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
7 |
|
public function enableCastType(): void |
|
165
|
|
|
{ |
|
166
|
7 |
|
$this->flags = $this->flags | self::CAST_TYPE_ENV_VALUE; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
1 |
|
public function disableCastType(): void |
|
170
|
|
|
{ |
|
171
|
1 |
|
$this->flags = $this->flags ^ self::CAST_TYPE_ENV_VALUE; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
1 |
|
private function clearMemory(): void |
|
175
|
|
|
{ |
|
176
|
1 |
|
unset($this->envCollection, $this->variablesResolver); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
39 |
|
private function isUsePutEnv(): bool |
|
180
|
|
|
{ |
|
181
|
39 |
|
return ($this->flags & self::POPULATE_PUTENV) === self::POPULATE_PUTENV; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
40 |
|
private function isPopulateToServer(): bool |
|
185
|
|
|
{ |
|
186
|
40 |
|
return ($this->flags & self::POPULATE_SERVER) === self::POPULATE_SERVER; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
47 |
|
private function isCastType(): bool |
|
190
|
|
|
{ |
|
191
|
47 |
|
return ($this->flags & self::CAST_TYPE_ENV_VALUE) === self::CAST_TYPE_ENV_VALUE; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
41 |
|
private function isClearMemory(): bool |
|
195
|
|
|
{ |
|
196
|
41 |
|
return ($this->flags & self::CLEAR_MEMORY_AFTER_LOAD_ENV) === self::CLEAR_MEMORY_AFTER_LOAD_ENV; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|