Skip to content

[DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the values of referenced env vars. #20618

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
Dec 17, 2016

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Nov 24, 2016

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

Being able to resolve environment variables at compile time as a replacement for SYMFONY__ special env vars, unlocking their deprecation (see #20100).

@@ -384,9 +384,18 @@ protected function loadCacheDriver($cacheName, $objectManagerName, array $cacheD

if (!isset($cacheDriver['namespace'])) {
// generate a unique namespace for the given application
$env = $container->getParameter('kernel.root_dir').$container->getParameter('kernel.environment');
$env = '';
foreach (array('kernel.name', 'kernel.environment', 'kernel.debug', 'cache.prefix.seed') as $key) {
Copy link
Member

Choose a reason for hiding this comment

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

the kernel name, environment and debug don't need to resolve env parameters, as these are special parameters set by the Kernel itself (redefining them is not allowed, as the value set by the kernel will win anyway)

Copy link
Member Author

Choose a reason for hiding this comment

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

Doing so makes the code simpler. Do you think this can have any adverse side effect?

*
* @return mixed The resolved value
*/
public function resolveValue($value, $resolveEnvValues = false, array $resolving = array())
Copy link
Member

Choose a reason for hiding this comment

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

array $resolving = array() should not be exposed in the public API IMO. Use a private method for the recursive call

Copy link
Member Author

Choose a reason for hiding this comment

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

This makes the resolution stateless. ParameterBag::resolveValue has the same strategy on this topic. I'd prefer keeping it this way.

Copy link
Member

@stof stof Nov 24, 2016

Choose a reason for hiding this comment

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

Well, I'm not talking about adding a private property. I'm talking about using a private method doResolveValue being exactly your current resolveValue method (except for the renaming of course) and then having this for resolveValue:

public function resolveValue($value, $resolveEnvValues = false)
{
    return $this->doResolveValue($value, $resolveEnvValues, array());
}

this way, we don't leak the internal argument to the public API (the fact that ParameterBag is leaking it is a mistake we made).

@nicolas-grekas nicolas-grekas force-pushed the container-resolve-value branch 5 times, most recently from c5e3ef9 to 9f55ad4 Compare November 24, 2016 13:28
* Replaces parameter placeholders (%name%) and unescapes percent signs.
*
* @param mixed $value A value
* @param mixed $resolveEnvValues Whether %env(VAR)% parameters should be replaced by the value of the corresponding environment variable or not
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 boolean, not mixed

@nicolas-grekas nicolas-grekas force-pushed the container-resolve-value branch 2 times, most recently from cc1bd7d to 0a7ec76 Compare November 24, 2016 20:32
@nicolas-grekas nicolas-grekas force-pushed the container-resolve-value branch 4 times, most recently from 58d827e to 6b7bf5c Compare November 29, 2016 21:46
@nicolas-grekas nicolas-grekas changed the title [DI] Add ContainerBuilder::resolveValue($value, $resolveEnvValues = false) [DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the value of referenced env vars. Nov 29, 2016
@nicolas-grekas nicolas-grekas changed the title [DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the value of referenced env vars. [DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the values of referenced env vars. Nov 29, 2016
@nicolas-grekas nicolas-grekas force-pushed the container-resolve-value branch from 6b7bf5c to 74f0f00 Compare November 30, 2016 17:12
@nicolas-grekas nicolas-grekas added this to the 3.x milestone Dec 6, 2016
@nicolas-grekas nicolas-grekas force-pushed the container-resolve-value branch from 74f0f00 to 713b081 Compare December 9, 2016 10:57
@nicolas-grekas nicolas-grekas modified the milestones: 3.3, 3.x Dec 9, 2016
@fabpot
Copy link
Member

fabpot commented Dec 17, 2016

Thank you @nicolas-grekas.

@fabpot fabpot merged commit 713b081 into symfony:master Dec 17, 2016
fabpot added a commit that referenced this pull request Dec 17, 2016
…ble to inline the values of referenced env vars. (nicolas-grekas)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the values of referenced env vars.

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

Being able to resolve environment variables at compile time as a replacement for `SYMFONY__` special env vars, unlocking their deprecation (see #20100).

Commits
-------

713b081 [DI] Make ContainerBuilder::resolveEnvPlaceholders() able to inline the values of referenced env vars.
@nicolas-grekas nicolas-grekas deleted the container-resolve-value branch December 17, 2016 10:59
@@ -1241,6 +1241,10 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
if (isset($config['prefix_seed'])) {
$container->setParameter('cache.prefix.seed', $config['prefix_seed']);
}
if ($container->hasParameter('cache.prefix.seed')) {
// Inline any env vars referenced in the parameter
$container->setParameter('cache.prefix.seed', $container->resolveEnvPlaceholders($container->getParameter('cache.prefix.seed'), true));
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be done directly in the if above ?

Copy link
Member Author

Choose a reason for hiding this comment

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

technically, it's possible to define the param before the framework extension, which is handled with this style

@fabpot fabpot mentioned this pull request May 1, 2017
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.

4 participants