Skip to content

[DI] ContainerBuilder::compile() can optionally resolve env vars in parameter bag #21460

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
Feb 9, 2017

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Jan 30, 2017

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

Here is a new feature allowing one to do $container->compile(true) and get the env vars resolved using the current env.

@chalasr
Copy link
Member

chalasr commented Jan 30, 2017

👍

@@ -555,15 +556,27 @@ public function prependExtensionConfig($name, array $config)
* * The parameter bag is frozen;
* * Extension loading is disabled.
*/
public function compile()
public function compile(/* $resolveEnvPlaceholders = false */)
Copy link
Contributor

@olvlvl olvlvl Jan 30, 2017

Choose a reason for hiding this comment

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

How about using a flag (e.g. self::RESOLVE_ENV_PLACEHOLDERS) instead of a boolean, so that additional features may be introduced without requiring additional parameters?

Copy link
Member Author

Choose a reason for hiding this comment

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

IMHO, we shouldn't plan for additional unplanned features :) This can be changed easily in the future if needed.

$_ENV['DUMMY_ENV_VAR'] = 'du%%y';

$container = new ContainerBuilder();
$container->setParameter('bar', '%% %env(DUMMY_ENV_VAR)%');
Copy link
Member

Choose a reason for hiding this comment

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

What will happen in the other two cases: 1) undefined env var; 2) undefined env var, but default value defined for it. Should we add those cases to this test too?

Copy link
Member Author

Choose a reason for hiding this comment

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

same as usual, this is exactly the same code/logic

Copy link
Member Author

Choose a reason for hiding this comment

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

tests added

@fabpot
Copy link
Member

fabpot commented Jan 30, 2017

Can you add some docs in the phpdocs?

@nicolas-grekas
Copy link
Member Author

phpdoc added

@@ -555,16 +556,37 @@ public function prependExtensionConfig($name, array $config)
* * Parameter values are resolved;
* * The parameter bag is frozen;
* * Extension loading is disabled.
*
* @param bool $resolveEnvPlaceholders Whether %env()% parameters should be resolved using the current
* env vars or be replaced by uniquely identifiable placeholders
Copy link
Member

Choose a reason for hiding this comment

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

That describes what it does, but I'd like to understand also when I should pass true here.

Copy link
Member Author

Choose a reason for hiding this comment

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

comment updated

@nicolas-grekas nicolas-grekas force-pushed the di-compile-env branch 3 times, most recently from a735b0e to 6ea3a24 Compare February 3, 2017 12:26
@fabpot
Copy link
Member

fabpot commented Feb 9, 2017

Thank you @nicolas-grekas.

@fabpot fabpot merged commit a3fd512 into symfony:master Feb 9, 2017
fabpot added a commit that referenced this pull request Feb 9, 2017
…e env vars in parameter bag (nicolas-grekas)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] ContainerBuilder::compile() can optionally resolve env vars in parameter bag

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

Here is a new feature allowing one to do `$container->compile(true)` and get the env vars resolved using the current env.

Commits
-------

a3fd512 [DI] ContainerBuilder::compile() can optionally resolve env vars in parameter bag
@nicolas-grekas nicolas-grekas deleted the di-compile-env branch February 14, 2017 16:21
@fabpot fabpot mentioned this pull request May 1, 2017
@zippy1981
Copy link

I tried to capture how to do this in a stackoverflow answer.

@pwm
Copy link

pwm commented Jun 15, 2017

@zippy1981 Thanks for that SO answer, however I can't seem to get it working. If I replace $container->compile(); with $container->compile(true); in the initializeContainer() method of Symfony\Component\HttpKernel\Kernel then everything works fine, however if I add $container->compile(true); to my own bundle then nothing happens. Where exactly should I add this line?

@stof
Copy link
Member

stof commented Jun 15, 2017

@pwm why would you want to use this argument in your bundle ?

@pwm
Copy link

pwm commented Jun 16, 2017

@stof I'm trying to get env vars work as monolog config values, pretty much exactly like the SO entry above, but I get an Incompatible use of dynamic environment variables... error. If I understand it correctly this PR is resolving this issue.

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