Loader.php
6.27 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace Dotenv;
use Dotenv\Environment\FactoryInterface;
use Dotenv\Exception\InvalidPathException;
use Dotenv\Regex\Regex;
use PhpOption\Option;
/**
* This is the loader class.
*
* It's responsible for loading variables by reading a file from disk and:
* - stripping comments beginning with a `#`,
* - parsing lines that look shell variable setters, e.g `export key = value`, `key="value"`.
* - multiline variable look always start with a " and end with it, e.g: `key="value
* value"`
*/
class Loader
{
/**
* The file paths.
*
* @var string[]
*/
protected $filePaths;
/**
* The environment factory instance.
*
* @var \Dotenv\Environment\FactoryInterface
*/
protected $envFactory;
/**
* The environment variables instance.
*
* @var \Dotenv\Environment\VariablesInterface
*/
protected $envVariables;
/**
* The list of environment variables declared inside the 'env' file.
*
* @var string[]
*/
protected $variableNames = [];
/**
* Create a new loader instance.
*
* @param string[] $filePaths
* @param \Dotenv\Environment\FactoryInterface $envFactory
* @param bool $immutable
*
* @return void
*/
public function __construct(array $filePaths, FactoryInterface $envFactory, $immutable = false)
{
$this->filePaths = $filePaths;
$this->envFactory = $envFactory;
$this->setImmutable($immutable);
}
/**
* Set immutable value.
*
* @param bool $immutable
*
* @return $this
*/
public function setImmutable($immutable = false)
{
$this->envVariables = $immutable
? $this->envFactory->createImmutable()
: $this->envFactory->create();
return $this;
}
/**
* Load the environment file from disk.
*
* @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException
*
* @return array<string|null>
*/
public function load()
{
return $this->loadDirect(
self::findAndRead($this->filePaths)
);
}
/**
* Directly load the given string.
*
* @param string $content
*
* @throws \Dotenv\Exception\InvalidFileException
*
* @return array<string|null>
*/
public function loadDirect($content)
{
return $this->processEntries(
Lines::process(preg_split("/(\r\n|\n|\r)/", $content))
);
}
/**
* Attempt to read the files in order.
*
* @param string[] $filePaths
*
* @throws \Dotenv\Exception\InvalidPathException
*
* @return string[]
*/
private static function findAndRead(array $filePaths)
{
if ($filePaths === []) {
throw new InvalidPathException('At least one environment file path must be provided.');
}
foreach ($filePaths as $filePath) {
$lines = self::readFromFile($filePath);
if ($lines->isDefined()) {
return $lines->get();
}
}
throw new InvalidPathException(
sprintf('Unable to read any of the environment file(s) at [%s].', implode(', ', $filePaths))
);
}
/**
* Read the given file.
*
* @param string $filePath
*
* @return \PhpOption\Option
*/
private static function readFromFile($filePath)
{
$content = @file_get_contents($filePath);
return Option::fromValue($content, false);
}
/**
* Process the environment variable entries.
*
* We'll fill out any nested variables, and acually set the variable using
* the underlying environment variables instance.
*
* @param string[] $entries
*
* @throws \Dotenv\Exception\InvalidFileException
*
* @return array<string|null>
*/
private function processEntries(array $entries)
{
$vars = [];
foreach ($entries as $entry) {
list($name, $value) = Parser::parse($entry);
$vars[$name] = $this->resolveNestedVariables($value);
$this->setEnvironmentVariable($name, $vars[$name]);
}
return $vars;
}
/**
* Resolve the nested variables.
*
* Look for ${varname} patterns in the variable value and replace with an
* existing environment variable.
*
* @param string|null $value
*
* @return string|null
*/
private function resolveNestedVariables($value = null)
{
return Option::fromValue($value)
->filter(function ($str) {
return strpos($str, '$') !== false;
})
->flatMap(function ($str) {
return Regex::replaceCallback(
'/\${([a-zA-Z0-9_.]+)}/',
function (array $matches) {
return Option::fromValue($this->getEnvironmentVariable($matches[1]))
->getOrElse($matches[0]);
},
$str
)->success();
})
->getOrElse($value);
}
/**
* Search the different places for environment variables and return first value found.
*
* @param string $name
*
* @return string|null
*/
public function getEnvironmentVariable($name)
{
return $this->envVariables->get($name);
}
/**
* Set an environment variable.
*
* @param string $name
* @param string|null $value
*
* @return void
*/
public function setEnvironmentVariable($name, $value = null)
{
$this->variableNames[] = $name;
$this->envVariables->set($name, $value);
}
/**
* Clear an environment variable.
*
* This method only expects names in normal form.
*
* @param string $name
*
* @return void
*/
public function clearEnvironmentVariable($name)
{
$this->envVariables->clear($name);
}
/**
* Get the list of environment variables names.
*
* @return string[]
*/
public function getEnvironmentVariableNames()
{
return $this->variableNames;
}
}