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

Skip to content

[DI] Remove hasty verification of unused env variables #22726

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

Closed
wants to merge 3 commits into from
Closed

[DI] Remove hasty verification of unused env variables #22726

wants to merge 3 commits into from

Conversation

fefas
Copy link

@fefas fefas commented May 17, 2017

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

Hello,

After handling with problems reported on the issue #22561 and thinking about it, I decided to open this PR removing the verification that forces the use of every environment variable that was written at least once in some configuration file.

The code I want to remove was introduced in the PR #19681 that adds a nice and new feature, but in my opinion the verification of unused environment variables is hasty.

If there is some good argument to keep that verification, it should be done in a class that aims with the logic of the Dependency Injection component, not in a dumper class.

However, my vision is that the DI component MUST NOT force the use of any configuration set by the developer, because it is not matter of DI. If the developer wrote something unnecessary or wrong, it should be caught by environment tests after or during the build step.

I also added @deprecated to the method ContainerBuilder:: getEnvCounters, because I noted that it is only used by the method PhpDumper::dump.

Thank you.

Thanks @edudobay for helping me with some of the many english mistakes I did.

@@ -1080,6 +1080,8 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
/**
* Get statistics about env usage.
*
* @deprecated unused and must be removed in some next release
Copy link
Contributor

@Taluu Taluu May 17, 2017

Choose a reason for hiding this comment

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

In Symfony 4.0, deprecated since 3.4

@@ -1080,6 +1080,8 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
/**
* Get statistics about env usage.
*
* @deprecated unused and must be removed in some next release
*
* @return int[] The number of time each env vars has been resolved
*/
public function getEnvCounters()
Copy link
Contributor

Choose a reason for hiding this comment

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

you should add a @trigger_error() call here

@mattjanssen
Copy link
Contributor

mattjanssen commented May 17, 2017

Instead of throwing an EnvParameterException maybe this could log the concern somehow at a very low level? I'm not sure if a logger is available at this point though.

Another option would be to not trigger the error if that field gets overridden by an extending yaml (import resource).

@nicolas-grekas
Copy link
Member

πŸ‘Ž see the thread you linked: this was done on purpose. Env vars are no supposed to be allowed in every config options/parameters. So this error tells you when you did such mistake.

@nicolas-grekas nicolas-grekas added this to the 3.2 milestone May 18, 2017
@mattjanssen
Copy link
Contributor

Here is a use case of environment variables in config that is not a mistake, but produces an error.

symfony_prod.yml

monolog:
    handlers:
        main:
            type: loggly
            token: "%env(LOGGLY_TOKEN)%"

symfony_prod_ecs.yml

imports:
    - { resource: symfony_prod.yml }

monolog:
    handlers:
        main:
            type: stream

Error:

[Symfony\Component\DependencyInjection\Exception\EnvParameterException]
Incompatible use of dynamic environment variables "LOGGLY_TOKEN" found in parameters.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented May 20, 2017

If you change your config to this, does it fix the issue? Removing a generic check to prevent a specific false positive (I agree it can be seen as such) is too broad (I didn't think about a more specific fix). I'm still -1 with the current patch.

symfony_prod_ecs.yml

imports:
    - { resource: symfony_prod.yml }

monolog:
    handlers:
        main:
            type: stream
            token: ~

@mattjanssen
Copy link
Contributor

Sorry, I should have pointed out that even token: ~ didn't fix the issue for me. I'll re-verify this on Monday, too.

@mattjanssen
Copy link
Contributor

Confirmed. Even if you use ~ (or any value) to override an %env()% value you will get an exception.

This means that we cannot use environment variables in a config.yaml if you ever need to override, swap or un-use that environment variable in any extending config(s).

@nicolas-grekas
Copy link
Member

OK, thanks. This PR is too broad, we should seek for a more specific fix.

@nicolas-grekas
Copy link
Member

Closing as explained. Issue #23520 should be used to track the progress on this topic instead.

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