simon_kohl
simon_kohl

Reputation: 93

Use "when@" in YAML configuration with value from environment variable

I am using symfony 6.1:

I have an environment variable do_something, and I want to include a section in the YAML configuration file only if do_something is set to true.

According to the symfony documentation I can use when@ to only include certain sections in the YAML configuration under specific conditions. The documentation only seems to show how to when@ with the app environment condition, e.g. when@prod.

Is it possible to use when@ with a variable from the .env?

I want my YAML configuration file to look something like this

something:
  anotherthing:
    site: &xyz
      type: 'abcd'
      api: 'abcd'
when@%env(bool:do_something)%:
  something:
    anotherthing:
      onlyifdosomething:
        <<: *xyz

Upvotes: 0

Views: 351

Answers (1)

yivi
yivi

Reputation: 47639

You can't do this exactly, not via configuration.

You could very probably accomplish this by manipulating the container on your application Kernel. But frankly, I wouldn't recommend it, and it looks like in any case is attacking the problem in the wrong direction.

If your service should behave differently with one configuration or another, you should pass the env var value to the service, and let the service deal with the different needed behaviors.

If you need completely different implementation of a service depending on circumstances, what you need are environments, and load different services_env.yaml files depending on the value of APP_ENV.

Upvotes: 1

Related Questions