Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Dotenv][WebServerBundle] Override previously loaded variables#23799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
nicolas-grekas left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Almost good, just this missing:
--- a/src/Symfony/Bundle/WebServerBundle/WebServer.php+++ b/src/Symfony/Bundle/WebServerBundle/WebServer.php@@ -154,6 +154,11 @@ class WebServer $process->setWorkingDirectory($config->getDocumentRoot()); $process->setTimeout(null);+ if (in_array('APP_ENV', explode(',', getenv('SYMFONY_DOTENV_VARS')))) {+ $process->setEnv(array('APP_ENV' => false));+ $process->inheritEnvironmentVariables();+ }+ return $process; }
| */ | ||
| publicfunctionpopulate($values) | ||
| { | ||
| $loadedVars =getenv('SYMFONY_DOTENV_VARS') ?explode(',',getenv('SYMFONY_DOTENV_VARS')) :array(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
array_flip(explode(',', getenv('SYMFONY_DOTENV_VARS'))) (no need for the check, the resulting array is good enough)
| foreach ($valuesas$name =>$value) { | ||
| if (isset($_ENV[$name]) ||isset($_SERVER[$name]) ||false !==getenv($name)) { | ||
| if (!in_array($name,$loadedVars) && (isset($_ENV[$name]) ||isset($_SERVER[$name]) ||false !==getenv($name))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
isset($loadedVars[$name]) || ...
| $_ENV[$name] =$value; | ||
| $_SERVER[$name] =$value; | ||
| $loadedVars[] =$name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
$loadedVars[$name] = true;
| $loadedVars[] =$name; | ||
| } | ||
| $loadedVars =implode(',',$loadedVars); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
if (1 < count($loadedVars)) { ... } ("1" because of the empty string - or we should remove it before)
voronkovich commentedAug 6, 2017
@nicolas-grekas, Thanks for the code review! I've fixed all issues you pointed out. |
nicolas-grekas commentedAug 6, 2017
It should be merged in 3.4 to me, even maybe in 3.3 as bug fix? |
voronkovich commentedAug 6, 2017
@nicolas-grekas, It introduces a BC break: // This code will work differently for a current version and the new one:(newDotenv())->populate(['FOO' =>'foo','FOO' =>'bar' ]);// Current version echoes: "foo", new version echoes "bar"echogetenv('FOO'); IMHO, we don't need this feature in the Symfony 3.x because it doesn't use the Dotenv component. |
nicolas-grekas commentedAug 6, 2017
Not sure to get your example, you can't define an array with two identical keys. |
voronkovich commentedAug 6, 2017
@nicolas-grekas, Sorry, example should be: (newDotenv())->populate(['FOO' =>'foo' ]);(newDotenv())->populate(['FOO' =>'bar' ]);echogetenv('FOO'); |
nicolas-grekas commentedAug 6, 2017
Ok :) |
voronkovich commentedAug 6, 2017
@nicolas-grekas, Ok, I've changed the base branch to 3.3. |
| $loadedVars[$name] =true; | ||
| } | ||
| $loadedVars =implode(',',array_keys($loadedVars)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I think this block of 4 lines should be inside anif ($loadedVars) {...}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@nicolas-grekas, I think you're right. I've moved that block inside anif statement.
chalasr left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
3.3 LGTM
| * Sets values as environment variables (via putenv, $_ENV, and $_SERVER). | ||
| * | ||
| *Note that existingenvironment variables are never overridden. | ||
| *Existingenvironment variables are never overridden, unless they are listed in the SYMFONY_DOTENV_VARS env var. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I would not document this feature as this is really just a hack to make the internal PHP web server work. Not a feature we want to promote.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
@fabpot, Ok, I've removed that comment.
fabpot commentedAug 22, 2017
Thank you@voronkovich. |
…bles (voronkovich)This PR was squashed before being merged into the 3.3 branch (closes#23799).Discussion----------[Dotenv][WebServerBundle] Override previously loaded variables| Q | A| ------------- | ---| Branch? | 3.3| Bug fix? | yes| New feature? | no| BC breaks? | yes| Deprecations? | no| Tests pass? | yes| Fixed tickets |#23723| License | MITThis PR implements@nicolas-grekas's idea about how we could refresh loaded environment variables. See his comment#23723 (comment)Commits-------c5a1218 [Dotenv][WebServerBundle] Override previously loaded variables
tiger-seo commentedSep 1, 2017
guys, i am grateful for all your hard work, but this change is a breaking change and it is not mentioned :( |
tiger-seo commentedSep 1, 2017
to give you more info why it is a BC, please see our <?php/** @var \Composer\Autoload\ClassLoader $loader */useSymfony\Component\Dotenv\Dotenv;useSymfony\Component\Dotenv\Exception\PathException;$loader =require__DIR__ .'/autoload.php';$dotenv =newDotenv;try {// existing environment variables are never overridden$dotenv->load(__DIR__ .'/config/.env');}catch (PathException$e) {}// existing environment variables are never overridden$dotenv->load(__DIR__ .'/config/.env.dist');return$loader; |
chalasr commentedSep 1, 2017
@tiger-seo Please open a dedicated issue so that we can look at possible alternatives. Comments on closed PR/issues are lost. |
voronkovich commentedSep 1, 2017 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@tiger-seo, I think you could try to load the // existing environment variables are never overridden$dotenv->load(__DIR__ .'/config/.env.dist');try {// existing environment variables are never overridden$dotenv->load(__DIR__ .'/config/.env');}catch (PathException$e) {}return$loader; |
tiger-seo commentedSep 3, 2017
@voronkovich yes, this is exactly how we've fixed it, but we spent at least 5 man-hours of our team to find(debug) and fix the issue. Symfony promises seamless upgrade for minor releases, but not this time. |
voronkovich commentedSep 3, 2017
Sorry,@tiger-seo! We thought that the BC break is so small, so nobody would notice it :) |
Uh oh!
There was an error while loading.Please reload this page.
This PR implements@nicolas-grekas's idea about how we could refresh loaded environment variables. See his comment#23723 (comment)