@@ -257,6 +257,8 @@ private function lexValue()
257257 throw $ this ->createFormatException ('Whitespace are not supported before the value ' );
258258 }
259259
260+ $ loadedVars = array_flip (explode (', ' , isset ($ _SERVER ['SYMFONY_DOTENV_VARS ' ]) ? $ _SERVER ['SYMFONY_DOTENV_VARS ' ] : (isset ($ _ENV ['SYMFONY_DOTENV_VARS ' ]) ? $ _ENV ['SYMFONY_DOTENV_VARS ' ] : '' )));
261+ unset($ loadedVars ['' ]);
260262 $ v = '' ;
261263
262264 do {
@@ -295,8 +297,8 @@ private function lexValue()
295297 ++$ this ->cursor ;
296298 $ value = str_replace (['\\" ' , '\r ' , '\n ' ], ['" ' , "\r" , "\n" ], $ value );
297299 $ resolvedValue = $ value ;
298- $ resolvedValue = $ this ->resolveVariables ($ resolvedValue );
299- $ resolvedValue = $ this ->resolveCommands ($ resolvedValue );
300+ $ resolvedValue = $ this ->resolveVariables ($ resolvedValue, $ loadedVars );
301+ $ resolvedValue = $ this ->resolveCommands ($ resolvedValue, $ loadedVars );
300302 $ resolvedValue = str_replace ('\\\\' , '\\' , $ resolvedValue );
301303 $ v .= $ resolvedValue ;
302304 } else {
@@ -318,8 +320,8 @@ private function lexValue()
318320 }
319321 $ value = rtrim ($ value );
320322 $ resolvedValue = $ value ;
321- $ resolvedValue = $ this ->resolveVariables ($ resolvedValue );
322- $ resolvedValue = $ this ->resolveCommands ($ resolvedValue );
323+ $ resolvedValue = $ this ->resolveVariables ($ resolvedValue, $ loadedVars );
324+ $ resolvedValue = $ this ->resolveCommands ($ resolvedValue, $ loadedVars );
323325 $ resolvedValue = str_replace ('\\\\' , '\\' , $ resolvedValue );
324326
325327 if ($ resolvedValue === $ value && preg_match ('/\s+/ ' , $ value )) {
@@ -372,7 +374,7 @@ private function skipEmptyLines()
372374 }
373375 }
374376
375- private function resolveCommands ($ value )
377+ private function resolveCommands ($ value, $ loadedVars )
376378 {
377379 if (false === strpos ($ value , '$ ' )) {
378380 return $ value ;
@@ -388,7 +390,7 @@ private function resolveCommands($value)
388390 )
389391 /x ' ;
390392
391- return preg_replace_callback ($ regex , function ($ matches ) {
393+ return preg_replace_callback ($ regex , function ($ matches ) use ( $ loadedVars ) {
392394 if ('\\' === $ matches [1 ]) {
393395 return substr ($ matches [0 ], 1 );
394396 }
@@ -403,7 +405,15 @@ private function resolveCommands($value)
403405
404406 $ process = method_exists (Process::class, 'fromShellCommandline ' ) ? Process::fromShellCommandline ('echo ' .$ matches [0 ]) : new Process ('echo ' .$ matches [0 ]);
405407 $ process ->inheritEnvironmentVariables (true );
406- $ process ->setEnv ($ this ->values );
408+
409+ $ env = [];
410+ foreach ($ this ->values as $ name => $ value ) {
411+ if (isset ($ loadedVars [$ name ]) || (!isset ($ _ENV [$ name ]) && !(isset ($ _SERVER [$ name ]) && 0 !== strpos ($ name , 'HTTP_ ' )))) {
412+ $ env [$ name ] = $ value ;
413+ }
414+ }
415+ $ process ->setEnv ($ env );
416+
407417 try {
408418 $ process ->mustRun ();
409419 } catch (ProcessException $ e ) {
@@ -414,7 +424,7 @@ private function resolveCommands($value)
414424 }, $ value );
415425 }
416426
417- private function resolveVariables ($ value )
427+ private function resolveVariables ($ value, array $ loadedVars )
418428 {
419429 if (false === strpos ($ value , '$ ' )) {
420430 return $ value ;
@@ -430,7 +440,7 @@ private function resolveVariables($value)
430440 (?P<closing_brace>\})? # optional closing brace
431441 /x ' ;
432442
433- $ value = preg_replace_callback ($ regex , function ($ matches ) {
443+ $ value = preg_replace_callback ($ regex , function ($ matches ) use ( $ loadedVars ) {
434444 // odd number of backslashes means the $ character is escaped
435445 if (1 === \strlen ($ matches ['backslashes ' ]) % 2 ) {
436446 return substr ($ matches [0 ], 1 );
@@ -446,14 +456,16 @@ private function resolveVariables($value)
446456 }
447457
448458 $ name = $ matches ['name ' ];
449- if (isset ($ this ->values [$ name ])) {
459+ if (isset ($ loadedVars [ $ name ]) && isset ( $ this ->values [$ name ])) {
450460 $ value = $ this ->values [$ name ];
451- } elseif (isset ($ _SERVER [$ name ]) && 0 !== strpos ($ name , 'HTTP_ ' )) {
452- $ value = $ _SERVER [$ name ];
453461 } elseif (isset ($ _ENV [$ name ])) {
454462 $ value = $ _ENV [$ name ];
463+ } elseif (isset ($ _SERVER [$ name ]) && 0 !== strpos ($ name , 'HTTP_ ' )) {
464+ $ value = $ _SERVER [$ name ];
465+ } elseif (isset ($ this ->values [$ name ])) {
466+ $ value = $ this ->values [$ name ];
455467 } else {
456- $ value = ( string ) getenv ( $ name ) ;
468+ $ value = '' ;
457469 }
458470
459471 if (!$ matches ['opening_brace ' ] && isset ($ matches ['closing_brace ' ])) {
0 commit comments