@@ -37,6 +37,7 @@ final class Dotenv
3737 private int $ end ;
3838 private array $ values = [];
3939 private array $ overriddenValues = [];
40+ private array $ loadedRawVars = [];
4041 private array $ prodEnvs = ['prod ' ];
4142 private bool $ usePutenv = false ;
4243
@@ -646,10 +647,16 @@ private function doLoad(bool $overrideExistingVars, array $paths): void
646647
647648 $ values = $ this ->parseRaw ($ data , $ path );
648649
650+ $ loadedVars = array_flip (explode (', ' , $ _SERVER ['SYMFONY_DOTENV_VARS ' ] ?? $ _ENV ['SYMFONY_DOTENV_VARS ' ] ?? '' ));
651+ unset($ loadedVars ['' ]);
652+
649653 foreach ($ values as $ name => $ _ ) {
650654 if (!isset ($ this ->overriddenValues [$ name ]) && isset ($ _ENV [$ name ])) {
651655 $ this ->overriddenValues [$ name ] = $ _ENV [$ name ];
652656 }
657+ if (isset ($ loadedVars [$ name ]) || $ overrideExistingVars || !isset ($ _ENV [$ name ])) {
658+ $ this ->loadedRawVars [$ name ] = true ;
659+ }
653660 }
654661
655662 $ this ->populate ($ values , $ overrideExistingVars );
@@ -705,6 +712,10 @@ private function resolveLoadedVars(): void
705712 $ loadedVars = array_flip (explode (', ' , $ _SERVER ['SYMFONY_DOTENV_VARS ' ] ?? $ _ENV ['SYMFONY_DOTENV_VARS ' ] ?? '' ));
706713 unset($ loadedVars ['' ]);
707714
715+ $ rawVars = $ this ->loadedRawVars ;
716+ $ this ->loadedRawVars = [];
717+ unset($ rawVars ['SYMFONY_DOTENV_VARS ' ]);
718+
708719 $ this ->values = [];
709720 $ this ->path = '' ;
710721 $ this ->data = '' ;
@@ -716,10 +727,7 @@ private function resolveLoadedVars(): void
716727 // (e.g. MY_VAR="${MY_VAR:-default}") so their own raw value is hidden
717728 // during resolution, allowing the default to trigger correctly.
718729 $ selfReferencingVars = [];
719- foreach ($ loadedVars as $ name => $ _ ) {
720- if ('SYMFONY_DOTENV_VARS ' === $ name ) {
721- continue ;
722- }
730+ foreach ($ rawVars as $ name => $ _ ) {
723731 $ value = $ _ENV [$ name ] ?? '' ;
724732 if (str_contains ($ value , '$ ' ) && preg_match ('/\$\{? ' .preg_quote ($ name , '/ ' ).'(?![A-Za-z0-9_])/ ' , $ value )) {
725733 $ selfReferencingVars [$ name ] = true ;
@@ -728,10 +736,7 @@ private function resolveLoadedVars(): void
728736
729737 for ($ pass = 0 ; $ pass < 5 ; ++$ pass ) {
730738 $ resolved = [];
731- foreach ($ loadedVars as $ name => $ _ ) {
732- if ('SYMFONY_DOTENV_VARS ' === $ name ) {
733- continue ;
734- }
739+ foreach ($ rawVars as $ name => $ _ ) {
735740 if (!str_contains ($ value = $ _ENV [$ name ] ?? '' , '$ ' )) {
736741 continue ;
737742 }
@@ -785,10 +790,7 @@ private function resolveLoadedVars(): void
785790
786791 // Restore literal $ signs and unescape backslashes
787792 $ restored = [];
788- foreach ($ loadedVars as $ name => $ _ ) {
789- if ('SYMFONY_DOTENV_VARS ' === $ name ) {
790- continue ;
791- }
793+ foreach ($ rawVars as $ name => $ _ ) {
792794 $ value = $ _ENV [$ name ] ?? '' ;
793795 if ($ value !== $ newValue = str_replace (["\x00" , '\\\\' ], ['$ ' , '\\' ], $ value )) {
794796 $ restored [$ name ] = $ newValue ;
0 commit comments