1

Here is the context :

  • Each user of my application belongs to a company.
  • Parameters for each company are defined inside "company.yml" configuration files, all of them sharing the exact same structure.
  • These parameters are then used to tweak the application behavior.

It may sound trivial, but all I'm looking for is the proper way to load these specific YAML files.


From what I understood so far, using an Extension class isn't possible, since it has no knowledge about current user.

Using a custom service to manage these configurations rather than relying on Symfony's parameters seems more appropriate, but I can't find how to implement validation (using a Configuration class) and caching.

Any help would be greatly appreciated, thanks for your inputs!

2 Answers 2

3

Using the Yaml, Processor and Configuration components of Symfony2 should fit your needs.

Define your "CompanyConfiguration" class as if you were in the DependencyInjection case Create a new "CompanyLoader" service

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Config\Definition\Processor;

$companies = Yaml::parse('company.yml');
$processor = new Processor();
$configuration = new CompanyConfiguration();
$processor->processConfiguration($configuration, $companies);

Now you should be able to use your companies array to do what you want

Sign up to request clarification or add additional context in comments.

2 Comments

This Processor instance might be what I've been looking for, I'll definitely try this, thanks! :)
I just finished implementing this solution, and it works like a charm. Thanks again!
1

Have a look at http://symfony.com/doc/current/cookbook/configuration/configuration_organization.html as well as http://symfony.com/doc/current/cookbook/configuration/environments.html. If that's not the correct answer you'll have to be more specific on what your company.yml configuration contains.

3 Comments

Most if not all Symfony's documentation on the subject (including these links) is about global application configuration, while I need to work on request level. To give you an example: - Company A needs access to a statistic page. - Company B wants it too, but with more information. - Company C doesn't need it at all. Using a configuration file per company, I can manage these differences without hardcoding anything. I can't rely on a global and static configuration though, and need to load the corresponding file depending on current user's company.
so you don't have one yml per company but many? because if it's only one per company i'd speak about a "global" config - and you could have one prod-env per company which is then loading only the yml which you require - again using the links provided.
Got it. That's a different and interesting approach that I'll have to try on my spare time :) Since the service container seems available in AppKernel, I should be able to access the current user and then load the corresponding conf file. Thanks for your input!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.