Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:masterfromfmata:issue-26846
Sep 4, 2018
Merged

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

fabpot merged 1 commit intosymfony:masterfromfmata:issue-26846
Sep 4, 2018

Conversation

@fmata
Copy link
Contributor

@fmatafmata commentedApr 7, 2018
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#26846
LicenseMIT
Doc PRsymfony/symfony-docs#9568

I choose to use a new parameter in the constructor instead ofpopulate() to not add boilerplate code to them who want allow overriding in their current setup. It's just a parameter to add inDotenv 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

oleg-andreyev reacted with heart emoji
private$values;
private$enableOverride;

publicfunction__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
ContributorAuthor

Choose a reason for hiding this comment

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

docblock added.

$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 allisset 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
ContributorAuthor

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
ContributorAuthor

Choose a reason for hiding this comment

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

Updated

@fmata
Copy link
ContributorAuthor

Status: Needs Review

Copy link
Contributor

@TaluuTaluu 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 ?

$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 =newDotEnv(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
ContributorAuthor

Choose a reason for hiding this comment

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

Asserts added.

Taluu reacted with thumbs up emoji

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-grekasnicolas-grekas added this to the4.1 milestoneApr 9, 2018
@fabpotfabpot modified the milestones:4.1,nextMay 7, 2018
CHANGELOG
=========

4.1.0

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

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

@natebrunettenatebrunetteJul 7, 2018
edited
Loading

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 👍

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
ContributorAuthor

@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 calledload() andoverload(). Maybe we can do the same here?

ogizanagi, jvasseur, and sstok reacted with thumbs up emoji

@nicolas-grekas
Copy link
Member

Lets' addoverload() then?

jvasseur and javiereguiluz reacted with thumbs up emoji

@fabpot
Copy link
Member

I would also prefer to add another method.

@fmata
Copy link
ContributorAuthor

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

Tests failed are unrelated.

* @param bool $overrideExistingVars true when existing environment variables must be overridden
*/
publicfunctionpopulate(array$values):void
publicfunctionpopulate(array$values,bool$overrideExistingVars =false):void

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
ContributorAuthor

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 ofpopulate() method, I don't know why it is public because the purpose ofDotenv 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

Thank you@fmata.

@fabpotfabpot merged commit228b220 intosymfony:masterSep 4, 2018
fabpot added a commit that referenced this pull requestSep 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#9568I 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 readyCommits-------228b220 [Dotenv] add Dotenv::overload() to allow env vars override
if (!is_readable($path) ||is_dir($path)) {
thrownewPathException($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
ContributorAuthor

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 requestSep 4, 2018
…luz)This PR was merged into the master branch.Discussion----------Document the DotEnv::overload() methodI'm not sure how to update the doc according my PRsymfony/symfony#26859 which is pending review but feel free to comment if I did a mistake. Thank you.Commits-------38e4f08 Rewordf8a3c0d Update dotenv.rst6325759 Update dotenv.rstc8ad296 Update dotenv.rst
@fmatafmata deleted the issue-26846 branchSeptember 4, 2018 16:01
fabpot added a commit that referenced this pull requestSep 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       | MITAccording to comments done after merge in#26859.Commits-------f3af242 [Dotenv] use array instead of variadic in Dotenv::doLoad()
@nicolas-grekasnicolas-grekas modified the milestones:next,4.2Nov 1, 2018
This was referencedNov 3, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@fabpotfabpotfabpot approved these changes

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@stofstofstof left review comments

+3 more reviewers

@TaluuTaluuTaluu left review comments

@natebrunettenatebrunettenatebrunette requested changes

@TobionTobionTobion approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.2

Development

Successfully merging this pull request may close these issues.

9 participants

@fmata@javiereguiluz@nicolas-grekas@fabpot@Taluu@stof@Tobion@natebrunette@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp