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

Skip to content

[DependencyInjection] resolve array env vars #27267

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
May 18, 2018

Conversation

jamesthomasonjr
Copy link

@jamesthomasonjr jamesthomasonjr commented May 14, 2018

Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #27239
License MIT
Doc PR n/a

Why

This bugfix solves a problem where environment variables resolved as an array would cause an error while compiling the container if they aren't the last parameter in the ParameterBag: the next parameter to be resolved would fail at the stripos() check. More information about the bug is available at #27239

Tests

  • This PR modifies existing ContainerBuilder tests to make use of the EnvVarProcessor to resolve json strings into arrays, instead of relying upon a TestingEnvPlaceholderParameterBag class.
    • I would liked to have kept EnvVarProcessor logic out of the ContainerBuilder tests, but it was the interaction between the ContainerBuilder and EnvVarProcessor that caused the bug
  • This PR adds a new ContainerBuilder test to verify that an environment variable resolved into an array doesn't cause an error when the next variable attempts to be resolved

Code

  • This PR adds an \is_string() sanity check before the stripos() method call so that only a string are passed into stripos()
  • This PR also adds a $completed flag so that completely resolved environment variables (currently only determined by $placeholder === $value) can break out of the loop early (handled via break 2;

@jamesthomasonjr jamesthomasonjr changed the title 3.4 resolve array env vars [3.4] [DependencyInjection] resolve array env vars May 14, 2018
@jamesthomasonjr jamesthomasonjr force-pushed the 3.4-resolve-array-env-vars branch from 7af7268 to 7af5d6c Compare May 14, 2018 19:07
@nicolas-grekas nicolas-grekas added this to the 3.4 milestone May 14, 2018
foreach ($envPlaceholders as $env => $placeholders) {
foreach ($placeholders as $placeholder) {
if (false !== stripos($value, $placeholder)) {
if (\is_string($value) && false !== stripos($value, $placeholder)) {
Copy link
Member

Choose a reason for hiding this comment

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

Is the added check useful? I feel like the added break is enough, isn't it?

Copy link
Author

@jamesthomasonjr jamesthomasonjr May 14, 2018

Choose a reason for hiding this comment

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

Yeah, the break should be enough. I included the is_string() as a sanity check for the stripos() call, but it shouldn't be necessary. I can remove it if that's preferred.

Copy link
Member

@nicolas-grekas nicolas-grekas May 15, 2018

Choose a reason for hiding this comment

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

yes, let's remove the check as it might make ppl spend time wondering when this can be false, when it cannot :)

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage A string value must be composed of strings and/or numbers, but found parameter "env(ARRAY)" of type array inside string value "ABC %env(ARRAY)%".
* @expectedExceptionMessage A string value must be composed of strings and/or numbers, but found parameter "env(json:ARRAY)" of type array inside string value "ABC env_json_ARRAY_
Copy link
Member

Choose a reason for hiding this comment

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

is env_json_ARRAY_ correct? Shouldn't it be "ABC %env(ARRAY)%"?

Copy link
Author

@jamesthomasonjr jamesthomasonjr May 15, 2018

Choose a reason for hiding this comment

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

My first thought was that env_json_ARRAY_%s was expected due to the below code of EnvPlaceholderParameterBag, but now I'm seeing it should only be called for environment variables that resolve to strings.

$uniqueName = md5($name.uniqid(mt_rand(), true));
$placeholder = sprintf('env_%s_%s', str_replace(':', '_', $env), $uniqueName);

Copy link
Member

Choose a reason for hiding this comment

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

That's a bug that was hidden by the previous test case then.
$value should be processed by resolveEnvPlaceholders before being put in the exception message (with the default $format = null)

@nicolas-grekas nicolas-grekas changed the title [3.4] [DependencyInjection] resolve array env vars [DependencyInjection] resolve array env vars May 15, 2018
Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

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

Nice first contribution

@fabpot fabpot force-pushed the 3.4-resolve-array-env-vars branch from d084f73 to 4c3b950 Compare May 18, 2018 01:58
@fabpot
Copy link
Member

fabpot commented May 18, 2018

Thank you @jamesthomasonjr.

@fabpot fabpot merged commit 4c3b950 into symfony:3.4 May 18, 2018
fabpot added a commit that referenced this pull request May 18, 2018
…njr)

This PR was squashed before being merged into the 3.4 branch (closes #27267).

Discussion
----------

[DependencyInjection] resolve array env vars

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

## Why
This bugfix solves a problem where environment variables resolved as an array would cause an error while compiling the container if they aren't the last parameter in the ParameterBag: the next parameter to be resolved would fail at the `stripos()` check. More information about the bug is available at #27239

## Tests
- This PR modifies existing ContainerBuilder tests to make use of the EnvVarProcessor to resolve json strings into arrays, instead of relying upon a TestingEnvPlaceholderParameterBag class.
  - I would liked to have kept EnvVarProcessor logic out of the ContainerBuilder tests, but it was the interaction between the ContainerBuilder and EnvVarProcessor that caused the bug
- This PR adds a new ContainerBuilder test to verify that an environment variable resolved into an array doesn't cause an error when the next variable attempts to be resolved

## Code
- ~This PR adds an `\is_string()` sanity check before the `stripos()` method call so that only a string are passed into `stripos()`~
- This PR also adds a `$completed` flag so that completely resolved environment variables (currently only determined by `$placeholder === $value`) can break out of the loop early (handled via `break 2;`

Commits
-------

4c3b950 [DependencyInjection] resolve array env vars
This was referenced May 21, 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.

5 participants