Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Dotenv] add a flag to allow env vars override #26859

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2018
Merged

[Dotenv] add a flag to allow env vars override #26859

merged 1 commit into from
Sep 4, 2018

Conversation

fmata
Copy link
Contributor

@fmata fmata commented Apr 7, 2018

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #26846
License MIT
Doc PR symfony/symfony-docs#9568

I choose to use a new parameter in the constructor instead of populate() to not add boilerplate code to them who want allow overriding in their current setup. It's just a parameter to add in Dotenv creation instead of change or customize the loading of different .env files.

I targeted 4.1 despite the feature freeze because it's a small change but if you don't agree I can change to 4.2.

If you accept this PR I will do the doc PR then.
doc ready

@@ -35,6 +35,12 @@
private $end;
private $state;
private $values;
private $enableOverride;

public function __construct(bool $enableOverride = false)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a docblock specifying what the parameter is for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docblock added.

@@ -73,7 +79,7 @@ public function populate(array $values): void
foreach ($values as $name => $value) {
$notHttpName = 0 !== strpos($name, 'HTTP_');
// don't check existence with getenv() because of thread safety issues
if (!isset($loadedVars[$name]) && (isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName))) {
if (!isset($loadedVars[$name]) && ((!$this->enableOverride && isset($_ENV[$name])) || (isset($_SERVER[$name]) && $notHttpName))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be used for all isset checks?

if (!isset($loadedVars[$name]) && (!$this->enableOverride && (isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName)))) {

Additionally, I prefer an explicit === false check for readability, but I'm not sure what the convention is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For security reasons I think it's better to not allow overriding $_SERVER['HTTP_*'] values, a previous discussion took place before the creation of this component. Maybe a core team member can confirm this later.

About CS style, there are plenty of examples in the codebase (and in this file too) where shortcut is used. Personaly I think it's very strange to explicitly test a false value against a boolean return type :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The !$notHttpName already fulfills the stop condition to not overwrite the http server vars. So I think it's safe to include your check on the $_SERVER ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@fmata
Copy link
Contributor Author

fmata commented Apr 8, 2018

Status: Needs Review

Copy link
Contributor

@Taluu Taluu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure that this will make it to 4.1, as it's a feature freeze, unless it can be made as an exception ?

@@ -73,7 +79,7 @@ public function populate(array $values): void
foreach ($values as $name => $value) {
$notHttpName = 0 !== strpos($name, 'HTTP_');
// don't check existence with getenv() because of thread safety issues
if (!isset($loadedVars[$name]) && (isset($_ENV[$name]) || (isset($_SERVER[$name]) && $notHttpName))) {
if (!isset($loadedVars[$name]) && ((!$this->enableOverride && isset($_ENV[$name])) || (isset($_SERVER[$name]) && $notHttpName))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The !$notHttpName already fulfills the stop condition to not overwrite the http server vars. So I think it's safe to include your check on the $_SERVER ?

$dotenv = new DotEnv(true);
$dotenv->populate(array('TEST_ENV_VAR_OVERRIDEN' => 'new_value'));

$this->assertSame('new_value', getenv('TEST_ENV_VAR_OVERRIDEN'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add some asserts for the _ENV, _SERVER vars too ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asserts added.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you want to test they're getting properly overridden from $_SERVER and $_ENV?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Apr 9, 2018
@fabpot fabpot modified the milestones: 4.1, next May 7, 2018
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.1.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be 4.2.0 now

private $enableOverride;

/**
* @param bool $enableOverride true if you want to allow env vars overriding
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd propose to remove this comment and name the option $overrideExistingVars (default false) instead.

Copy link

@natebrunette natebrunette Jul 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a BC break Nevermind, I see the name change switches the functionality. That change would of course require a logic change as well, but I’m for it 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why so? That's adding an optional argument on the constructor, which doesn't break BC usually.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already edited 😀

@fmata
Copy link
Contributor Author

fmata commented Jul 9, 2018

@nicolas-grekas rebased & comments addressed.

@javiereguiluz
Copy link
Member

I was looking for how did other similar libraries solved this:

The most popular option seems to be to define two methods called load() and overload(). Maybe we can do the same here?

@nicolas-grekas
Copy link
Member

Lets' add overload() then?

@fabpot
Copy link
Member

fabpot commented Aug 28, 2018

I would also prefer to add another method.

@fmata
Copy link
Contributor Author

fmata commented Sep 2, 2018

I added Dotenv::overload() method but I had to add an optional parameter in Dotenv::populate() too because this method is public (I don't know why).

Tests failed are unrelated.

*/
public function populate(array $values): void
public function populate(array $values, bool $overrideExistingVars = false): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be reverted IMHO: it makes no sense to provide two public interfaces to do the same thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree but I think the same thing of populate() method, I don't know why it is public because the purpose of Dotenv is to use a file, no directly an array of vars it's so low level IMHO. So I added here to be consistent but let me know if you have no doubt about this revert, I can do it :)

@fabpot
Copy link
Member

fabpot commented Sep 4, 2018

Thank you @fmata.

@fabpot fabpot merged commit 228b220 into symfony:master Sep 4, 2018
fabpot added a commit that referenced this pull request Sep 4, 2018
This PR was merged into the 4.2-dev branch.

Discussion
----------

[Dotenv] add a flag to allow env vars override

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #26846
| License       | MIT
| Doc PR        | symfony/symfony-docs#9568

I choose to use a new parameter in the constructor instead of `populate()` to not add boilerplate code to them who want allow overriding in their current setup. It's just a parameter to add in `Dotenv` creation instead of change or customize the loading of different .env files.

I targeted 4.1 despite the feature freeze because it's a small change but if you don't agree I can change to 4.2.

~~If you accept this PR I will do the doc PR then.~~
doc ready

Commits
-------

228b220 [Dotenv] add Dotenv::overload() to allow env vars override
if (!is_readable($path) || is_dir($path)) {
throw new PathException($path);
}
$this->doLoad(false, $path, ...$paths);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of creating a PHP array (with the variadic argument) to spread it again (here) to create another PHP array (variadic argument in the private method), it might make sense to pass an array to the private method instead of making it variadic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, we don't need a variadic argument in the private method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof & @fabpot new PR #28359 created :)

javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Sep 4, 2018
…luz)

This PR was merged into the master branch.

Discussion
----------

Document the DotEnv::overload() method

I'm not sure how to update the doc according my PR symfony/symfony#26859 which is pending review but feel free to comment if I did a mistake. Thank you.

Commits
-------

38e4f08 Reword
f8a3c0d Update dotenv.rst
6325759 Update dotenv.rst
c8ad296 Update dotenv.rst
@fmata fmata deleted the issue-26846 branch September 4, 2018 16:01
fabpot added a commit that referenced this pull request Sep 5, 2018
…() (fmata)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[Dotenv] use array instead of variadic in Dotenv::doLoad()

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT

According to comments done after merge in #26859.

Commits
-------

f3af242 [Dotenv] use array instead of variadic in Dotenv::doLoad()
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants