Symfony 2 - how to parse% parameter% in my own Yaml file downloader?

I have a Yaml bootloader that loads additional configuration items for a β€œprofile” (where one application can use different profiles, for example, for different local editions of the same site).

My bootloader is very simple:

# YamlProfileLoader.php use Symfony\Component\Config\Loader\FileLoader; use Symfony\Component\Yaml\Yaml; class YamlProfileLoader extends FileLoader { public function load($resource, $type = null) { $configValues = Yaml::parse($resource); return $configValues; } public function supports($resource, $type = null) { return is_string($resource) && 'yml' === pathinfo( $resource, PATHINFO_EXTENSION ); } } 

The loader is used in a more or less similar way (the bit is simplified because there is caching too):

 $loaderResolver = new LoaderResolver(array(new YamlProfileLoader($locator))); $delegatingLoader = new DelegatingLoader($loaderResolver); foreach ($yamlProfileFiles as $yamlProfileFile) { $profileName = basename($yamlProfileFile, '.yml'); $profiles[$profileName] = $delegatingLoader->load($yamlProfileFile); } 

So, the Yaml file that it parses:

 # profiles/germany.yml locale: de_DE hostname: %profiles.germany.host_name% 

Currently, the resulting array contains literally '%profiles.germany.host_name%' for the array key 'hostname' .

So, how can I parse the% parameters to get the actual parameter values?

I made my way through Symfony 2 code and documents (and this SO question) and cannot find where this is done in the structure itself. probably write your own parameter parser - get the parameters from the kernel, find the lines% foo% and look-up / replace ... but if the component is ready to use, I prefer to use this.

To give a little more background, why can't I just include it in the main config.yml file: I want to be able to load app/config/profiles/*.yml , where * is the profile name, and I use my own loader for this . If there is a way for wildcard import configuration files, then this may also work for me.

Note: 2.4 is currently in use, but is about ready to upgrade to 2.5 if that helps.

+7
php symfony
source share
2 answers

I made my way through the code and docs of Symfony 2 (and this SO question and cannot find where this is done in the structure itself.

The Symfony dependency injection component uses a compiler pass to resolve parameter references during the optimization phase.

Compiler receives registered compiler transfers from PassConfig . This class sets up several default compilers , which includes ResolveParameterPlaceHoldersPass .

At compile time, the ResolveParameterPlaceHoldersPass container uses the ParameterBag container to allow strings containing %parameters% . The compiler then passes that resolved value back to the container.

So, how can I parse the% parameters to get the actual parameter values?

You will need access to the container in your ProfileLoader (or wherever you see fit). Using the container, you can recursively iterate over the yaml configuration with the parsed syntax and pass the values ​​to the container parameter package, which must be resolved using the resolveValue () method.


It seems to me that perhaps a cleaner approach would be for you to implement this in your package configuration. Thus, your config will be checked for a specific structure, which can catch configuration errors at an early stage. For more details, see the batch configuration in the documents (this link is for v2.7, but I hope it will apply to your version as well).

I understand this is an old question, but I spent quite a bit of time figuring it out for my own projects, so I am posting the answer here for future reference.

+1
source share

I tried many options to allow the %% parameter to .yml parameters, but no luck. All I can think of is parsing the% parameter% and extracting it from the container, but not an innovation yet.

On the other hand, I don’t have enough information about your environment to see the big picture, but I just came up with a different idea. This can be very convenient if you declare your profiles in the parameters.yml file and load it as an array into your controller or service through the container.

application / Config / parameters.yml

 parameters: profiles: germany: locale: de_DE host_name: http://de.example.com uk: locale: en_EN host_name: http://uk.example.com turkey: locale: tr_TR host_name: http://tr.example.com 

You can have all your profiles as an array in your controller.

 <?php namespace Acme\DemoBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class DefaultController extends Controller { public function indexAction() { $profiles = $this->container->getParameter('profiles'); var_dump($profiles); return $this->render('AcmeDemoBundle:Default:index.html.twig'); } } 

With this approach

  • you do not need to encode custom YamlLoader
  • You do not need to worry about importing parameters to other yml files.
  • you can have your profiles as an array at any time when you have $ container in hand
  • You do not need to upload / cache profile files one at a time.
  • You don’t need to look for a solution to download wildcard files.

If I asked the question correctly, this approach may help you.

0
source share

All Articles