Reputation: 1415
I want to make use of the new Symfony feature of autowiring environment variables in a service, where previously i would put them as defaults in the service configuration.
As we build our application, some env vars are reserved to change our behavior only if we need to.
Before i would do in services.yaml
parameters:
env(PUBLISH_CHECK_CYCLE_SECONDS): '3600'
services:
_defaults:
bind:
$publishCheckCycleSeconds: '%env(int:PUBLISH_CHECK_CYCLE_SECONDS)%'
SomeService: ~ # for binds
where SomeService contained int $publishCheckCycleSeconds
as a constructor argument.
This is a lot of boilerplate which i want to replace with something like this:
class SomeService {
public function __construct(
// ...
#[Autowire(env: 'bool:CACHE_EXPANDED_REFERENCES')]
private readonly bool $cacheExpandedEntities = true,
#[Autowire(env: 'int:CACHE_EXPANDED_REFERENCES_SECONDS')]
private readonly int $expandedReferencesCacheTime = 60 * 60 * 24,
) {
}
// ...
}
however, in our CI i get errors about the missing envvars:
In EnvVarProcessor.php line 217:
Environment variable not found: "CACHE_EXPANDED_REFERENCES".
What is the best-practice for this approach? Can i set a default somehow? I am aware that the env-processor default
exists but when I last used it, it seems i can only reference other variables as a default instead of values. Otherwise i would try
#[Autowire(env: 'default:true:bool:CACHE_EXPANDED_REFERENCES')]
but this gives me a different error that the variable true
is not defined:
In EnvVarProcessor.php line 124:
Invalid env fallback in "default:true:bool:CACHE_EXPANDED_REFERENCES": para
meter "true" not found
and creating fallback-variables seems to be more boilerplate again that i want to avoid.
Upvotes: 2
Views: 568
Reputation: 2291
You should declare the CACHE_EXPANDED_REFERENCES
var in your .env
file. You can also set a default value there if you wish, but at least var should be declared like CACHE_EXPANDED_REFERENCES=
. If you then wish to set different values based on environment, you can override these in .env.test
, .env.prod
etc... files.
Upvotes: -1