-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Parse environment variable into array #40906
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
Comments
I think this is caused by the special logic in the Configuration class that wraps a string into an array with 1 element when getting a string in |
Thank you. Here is the code you reference So this issue is specifically for |
Well, we could have similar issues in other places doing some logic in |
i think #40654 (comment) is the ideal solution |
To my understanding, this fails because "json" declares returning "string|int|bool|...|array". |
json declares type array|null. The issue is |
The way I see it, we are using too many normalisations. The |
I agree we're trying to be too smart with these shortcuts. |
If we dont convert a string to an array, we will get the following exception:
|
it inherits symfony/src/Symfony/Component/Config/Definition/ArrayNode.php Lines 409 to 412 in 7636fd6
for prototyped arrays we can allow array-typed placeholders 👍 In this regard i agree with @nicolas-grekas more or less that we can loosen it a bit. Stricly spoken the env doesnt tell us the expected child prototype (eg.
By making ifString() opt-out from envs? eg. #40654 (comment) (maybe via a ifString(true) migration path?)
What's the proposal ... case by case? Deprecate beforeNorm() as a whole? The above? |
another issue is |
I don't think we should deprecated |
the "some places" is exactly what started bugging me in #29270 (comment) 😅 but perhaps exposing $isEnv works after all. |
Hey, thanks for your report! |
Relevant for me. But my case is about allowPlaceholders(): false |
Relevant to me too; And the documentation at https://symfony.com/doc/current/configuration/env_var_processors.html#built-in-environment-variable-processors is still wrong, as far as I understand ... |
Hey, thanks for your report! |
Friendly reminder that this issue exists. If I don't hear anything I'll close this. |
I haven't seen any new developments on this, I would still like to see it. |
still need this |
same here, i need a solution to this. Thanks |
same too me |
same too |
Hey, thanks for your report! |
I would like to see a solution to convert environment variables into arrays without custom code, just writing configurations. |
this works form me (symfony 4.4) :
hope this will help ;) |
Same error with symfony/mailer recipients field
|
Same with The lock memcache store looks following if I'm passing csv:
and exception Custom solution 1 via Extension:Solved it via custom extension. Created parameter and then reused it in the yaml file. Correct me if it's bad practice, but it's only way I have found how to solve it for me. public function prepend(ContainerBuilder $container): void
{
$envName = 'MEMCACHE_SERVERS';
$serversAsCsv = getenv($envName);
if (empty($serversAsCsv)) {
throw new LogicException(sprintf('Check that %s env is set and not empty', $envName));
}
$hostsList = str_getcsv($serversAsCsv, ',', '"', PHP_VERSION_ID >= 70400 ? '' : '\\');
$container->setParameter('cache.memcached.servers', $hostsList);
} Custom solution 2 via
|
I tried with a Extension and the prepend method, but it seems that at this point the .env file is not loaded yet. So for the mailer config i just did not use the ...
class MailerCatchallListener implements EventSubscriberInterface
{
/**
* @var null|Address[]
*/
private ?array $recipients = null;
public function __construct(?string $recipients)
{
if($recipients) {
$recipientsArray = explode(',', $recipients);
$this->recipients = Address::createArray($recipientsArray);
}
}
public function onMessage(MessageEvent $event): void
{
if ($this->recipients) {
$event->getEnvelope()->setRecipients($this->recipients);
}
}
public static function getSubscribedEvents(): array
{
return [
// framework.mailer.catchall_recipients will override
MessageEvent::class => ['onMessage', -254],
];
}
}
Would still be handy though to be able to use the FrameworkBundle config option. |
@ivoba, I suppose, the mine suggestions not worked well cause I forgot to mention that we force to use putenv in bootstrap (bad practice, but as it is). It was long ago when started to use that, but as I remember this is key moment why my code works. Correct me someone, please, if I'm wrong. $dontEnv = (new Dotenv());
// Nicolas Grekas: "Injecting variables via dependency injection, that's the recommended way. i.e. code shouldn't care where the value comes from."
// https://github.com/symfony/symfony/issues/35388#issuecomment-576027130
$dontEnv->usePutenv(true); // <---- THIS ONE. By default it is turn off
$dontEnv->bootEnv(dirname(__DIR__).'/.env'); |
I'm facing this issue too on a Symfony 6.4 app. Is there still no way to pass an array parsed from en environment variable to properties Using the JSON trick (e.g. Instead, I altered my if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? false) {
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO);
}
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? false) {
Request::setTrustedHosts(explode(',', $trustedHosts));
} |
Up, still not able to use env var to populate trusted_proxy configuration. This behaviour is not documented anywhere, and the other "trusted_" configuration works fine with env vars. I'm not expert in the core of the symfony framework, but the fact that the custom logic to validate the input is done before the environment resolution breaks the ability to use env for this specific configuration, and break the consistency in configurability. So, this bug is still relevant :) |
Fixed by #58161 actually. |
Oh fine, that closes a 3.5years old bug :), thank you @nicolas-grekas 👍 |
@nicolas-grekas is there another ticket for a generic solution or is it a wont-fix? i was hoping to be able to parse all kind of variables into arrays. i tried to configure the also other projects like a2lix/TranslationFormBundle#349 would depend on a more generic solution. related symfony issues/PRs: |
No generic solution is possible unfortunately: the way the config defines or checks the passed values requires special handling for env vars. |
Symfony version(s) affected: 5.2.6
Description
I cannot parse an environment variable into an array.
How to reproduce
I get:
See reproducer repo: https://github.com/Nyholm/sf-issue-40906
The text was updated successfully, but these errors were encountered: