问题描述
这里是上下文:
- 我的应用程序的每个用户都属于一家公司。
- 每个公司的参数都在 company.yml配置文件中定义,它们都共享完全相同的结构。
- 然后使用这些参数来调整应用程序行为。
这听起来很琐碎,但我要寻找的是加载这些特定YAML文件的正确方法。
It may sound trivial, but all I'm looking for is the proper way to load these specific YAML files.
根据我到目前为止的了解,不可能使用Extension类,因为它不了解当前用户。
From what I understood so far, using an Extension class isn't possible, since it has no knowledge about current user.
使用自定义服务来管理这些配置而不是依靠Symfony的参数似乎更合适,但是我找不到如何实现验证(使用Configuration类)和缓存的方法。
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!
推荐答案
使用Yaml,处理器和配置
Using the Yaml, Processor and Configuration components of Symfony2 should fit your needs.
- http://symfony.com/doc/current/components/yaml/introduction.html
- http://symfony.com/doc/current/components/config/definition.html
定义 CompanyConfiguration类,就像在DependencyInjection情况下一样
创建一个新的 CompanyLoader服务
Define your "CompanyConfiguration" class as if you were in the DependencyInjection caseCreate 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
这篇关于Symfony:动态配置文件加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!