Non-scalar ENVs to use as a symfony parameter

I am trying to use ENVs to set my parameters in Symfony2. Scalar values ​​are simple enough, but I have parameters that are arrays that I need to set somehow using ENV.

This parameter:

parameters: redis.servers: - { host: 127.0.0.1, port: 6379 } - { host: other, port: 6379 } # and so on 

The kicker here is that the server array can change dynamically, so I can't just assume there 2.

What I was hoping to do (but it just gives me a json string):

 SYMFONY__REDIS__SERVERS=[{"host":"127.0.0.1","port":"6379"}] 

Is it possible? Are any labor costs possible? There are several packages that we use that accept array / object parameters, so I cannot update there to process the parameter. This should be the application tier, if anything.

Thanks.

+5
source share
1 answer

I was able to solve this problem by updating AppKernel to override the getEnvParameters () method of the parent kernel. This method only works with parameters that the kernel has already found in ENV (technically from $ _SERVER). I like this because it will not work on the entire parameter stack, not the entire $ _SERVER array.

 protected function getEnvParameters() { $parameters = parent::getEnvParameters(); foreach ($parameters as &$parameter) { if (is_string($parameter)) { $decoded = json_decode($parameter, true); // we only care about arrays (or objects that get turned into arrays) if (!json_last_error() && is_array($decoded)) { $parameter = $decoded; } } } return $parameters; } 
+1
source

Source: https://habr.com/ru/post/1211195/


All Articles