Description
Symfony version(s) affected
7.3
Description
PR #59136 and #54666 added the ability to reset env vars. In theory it works but in practice it doesn't since all env var loaders are loaded way too early because many symfony services define them dynamically, for example, these env vars APP_RUNTIME_MODE, APP_RUNTIME_ENV, VAR_DUMPER_SERVER, maybe even more.
As such I suggest EnvVarLoaders to define the env keys that it will provide a value for, thereby, preventing loading them unnecessarily.
How to reproduce
Create a new symfony project, define an EnvVarLoader which loads an env var whose value can change based on certain conditions like tenant id, use the env in your messenger handler, it will always use the first value since all env var loaders are loaded very early in the process, even before a messenger message is handled.
Possible Solution
EnvVarLoaders should be loaded on demand, they should define the keys that they can provide values for.
Something like or maybe even an attribute
public static function envKeys(): array
{
return ['DATABASE_URL', 'FILESYSTEM_PATH'];
}
and EnvProcessor::getEnv
shouldn't go through all EnvLoaders, it should only go through env loaders which can provide the value for current key.
For BC reasons envKeys
cannot be added to EnvVarLoaderInterface
but can possibly be added via phpdoc.
Additional Context
I've created a bug ticket instead of feature, because previous PRs aren't enough to make it work as expected even though the possible solution would lead to a new feature.