I have few parameters related to AWS configuration, I need to get access to from my service. Usually, I'm injecting ParameterBagInterface to the service if I need few parameters and using it to get parameters I need, in such way I do not need to check if a parameter really exists and handle it on my own, as ParameterBag throwing ParameterNotFoundException if a parameter is not exist.
But to make ParameterBag working as I need, parameters in service.yml should be added like:
aws.default_one: '%env(AWS_DEFAULT_BUCKET)%'
aws.default_two: '%env(AWS_DEFAULT_BUCKET)%'
and now I can get it from bag like this $this->parameterBag->get('aws.default_one')
But yml syntax allowing to add nested parameters, so I can add parameters to config like this:
aws:
default_one: '%env(AWS_DEFAULT_BUCKET)%'
default_two: '%env(AWS_DEFAULT_BUCKET)%'
it looks much cleaner and more readable especially when you have a lot of parameters, but in such a way I can't just write $this->parameterBag->get('aws.default_one') (it will throw the error The parameter "aws.default_one" must be defined ) and the only way to get it from parameters bag I found is $this->parameterBag->get('aws')['default_one'] but then it makes no sense to use parameter bag at all as you still need to make validation by your own to check if default_one key exists and handle it. And in such case instead of passing whole parameter bag, I can just pass %aws% to the service and work with it as with php array.
So questions are:
- what is the best practice to inject a list of parameters to the service, inject parameter bag or just pass each parameter or array of nested parameters to the service?
- maybe there is some way to get nested parameter using dot notation instead of doing like
$this->parameterBag->get('aws')['default_one']as it makes life easier with just using parameter bag which throwing exception?