Skip to content

Commit e188005

Browse files
authored
Merge pull request #54 from blitz-php/devs
fix: concaténation de variable imbriqué dans le fichier .env
2 parents 2620d54 + 3423d6c commit e188005

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

src/Helpers/common.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444
*/
4545
function env(string $key, $default = null)
4646
{
47-
return Helpers::env($key, $default);
47+
if (is_string($value = Helpers::env($key, $default)) && trim($value) === '') {
48+
$value = $default;
49+
}
50+
51+
return $value;
4852
}
4953
}
5054

src/Loader/DotEnv.php

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,7 @@ public static function init(string $path, string $file = '.env')
4848
*/
4949
public function load(): bool
5050
{
51-
$vars = $this->parse();
52-
53-
if ($vars === null) {
54-
return false;
55-
}
56-
57-
foreach ($vars as $name => $value) {
58-
$this->setVariable($name, $value);
59-
}
60-
61-
return true; // notifie de la reussite de l'operation
51+
return $this->parse() !== null;
6252
}
6353

6454
/**
@@ -145,7 +135,7 @@ public function parse(): ?array
145135

146136
// Assurez-vous que le fichier est lisible
147137
if (! is_readable($this->path)) {
148-
throw new InvalidArgumentException("The .env file is not readable: {$this->path}");
138+
throw new InvalidArgumentException("Le fichier `.env` est n'est pas lisible: {$this->path}");
149139
}
150140

151141
$vars = [];
@@ -162,6 +152,7 @@ public function parse(): ?array
162152
if (str_contains($line, '=')) {
163153
[$name, $value] = $this->normaliseVariable($line);
164154
$vars[$name] = $value;
155+
$this->setVariable($name, $value);
165156
}
166157
}
167158

@@ -201,17 +192,14 @@ public function normaliseVariable(string $name, string $value = ''): array
201192
$value = trim($value);
202193

203194
// Assainir le nom
204-
$name = str_replace(['export', '\'', '"'], '', $name);
195+
$name = preg_replace('/^export[ \t]++(\S+)/', '$1', $name);
196+
$name = str_replace(['\'', '"'], '', $name);
205197

206198
// Assainir la valeur
207199
$value = $this->sanitizeValue($value);
208-
209200
$value = $this->resolveNestedVariables($value);
210201

211-
return [
212-
$name,
213-
$value,
214-
];
202+
return [$name, $value];
215203
}
216204

217205
/**
@@ -231,33 +219,34 @@ protected function sanitizeValue(string $value): string
231219
// Commence-t-il par une citation ?
232220
if (strpbrk($value[0], '"\'') !== false) {
233221
// la valeur commence par un guillemet
234-
$quote = $value[0];
222+
$quote = $value[0];
223+
235224
$regexPattern = sprintf(
236225
'/^
237-
%1$s # match a quote at the start of the value
238-
( # capturing sub-pattern used
239-
(?: # we do not need to capture this
240-
[^%1$s\\\\] # any character other than a quote or backslash
241-
|\\\\\\\\ # or two backslashes together
242-
|\\\\%1$s # or an escaped quote e.g \"
243-
)* # as many characters that match the previous rules
244-
) # end of the capturing sub-pattern
245-
%1$s # and the closing quote
246-
.*$ # and discard any string after the closing quote
247-
/mx',
248-
$quote
226+
%1$s # match a quote at the start of the value
227+
( # capturing sub-pattern used
228+
(?: # we do not need to capture this
229+
[^%1$s\\\\] # any character other than a quote or backslash
230+
|\\\\\\\\ # or two backslashes together
231+
|\\\\%1$s # or an escaped quote e.g \"
232+
)* # as many characters that match the previous rules
233+
) # end of the capturing sub-pattern
234+
%1$s # and the closing quote
235+
.*$ # and discard any string after the closing quote
236+
/mx',
237+
$quote,
249238
);
239+
250240
$value = preg_replace($regexPattern, '$1', $value);
251241
$value = str_replace("\\{$quote}", $quote, $value);
252242
$value = str_replace('\\\\', '\\', $value);
253243
} else {
254244
$parts = explode(' #', $value, 2);
255-
256245
$value = trim($parts[0]);
257246

258247
// Les valeurs sans guillemets ne peuvent pas contenir d'espaces
259248
if (preg_match('/\s+/', $value) > 0) {
260-
throw new InvalidArgumentException('.env values containing spaces must be surrounded by quotes.');
249+
throw new InvalidArgumentException('Les valeurs du fichier `.env` contenant des espaces doivent être entourées de guillemets.');
261250
}
262251
}
263252

@@ -276,20 +265,18 @@ protected function sanitizeValue(string $value): string
276265
protected function resolveNestedVariables(string $value): string
277266
{
278267
if (str_contains($value, '$')) {
279-
$loader = $this;
280-
281268
$value = preg_replace_callback(
282-
'/\${([a-zA-Z0-9_]+)}/',
283-
static function ($matchedPatterns) use ($loader) {
284-
$nestedVariable = $loader->getVariable($matchedPatterns[1]);
269+
'/\${([a-zA-Z0-9_\.]+)}/',
270+
function ($matchedPatterns) {
271+
$nestedVariable = $this->getVariable($matchedPatterns[1]);
285272

286-
if (null === $nestedVariable) {
273+
if ($nestedVariable === null) {
287274
return $matchedPatterns[0];
288275
}
289276

290277
return $nestedVariable;
291278
},
292-
$value
279+
$value,
293280
);
294281
}
295282

0 commit comments

Comments
 (0)