我有一個Yaml加載程序,它爲「配置文件」(其中一個應用程序可以使用不同配置文件,例如用於同一站點的不同本地版本)加載其他配置項。Symfony 2 - 如何在我自己的Yaml文件加載器中解析%參數%?
我的加載程序是非常簡單的:
# 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
);
}
}
裝載機是用來或多或少像這樣(簡化了一下,因爲有太多緩存):
$loaderResolver = new LoaderResolver(array(new YamlProfileLoader($locator)));
$delegatingLoader = new DelegatingLoader($loaderResolver);
foreach ($yamlProfileFiles as $yamlProfileFile) {
$profileName = basename($yamlProfileFile, '.yml');
$profiles[$profileName] = $delegatingLoader->load($yamlProfileFile);
}
原來是YAML文件它的解析:
# profiles/germany.yml
locale: de_DE
hostname: %profiles.germany.host_name%
目前,由此產生的數組字面上包含'%profiles.germany.host_name%'
爲'hostname'
數組密鑰。
那麼,我如何解析%參數來獲取實際參數值?
我一直在通過Symfony 2代碼和文檔(和this SO question)進行拖拽,並且無法找到在框架本身內部完成的任務,我可能編寫自己的參數解析器 - 從內核獲取參數,搜索爲%foo%字符串和查找/替換...但如果有一個組件準備好使用,我更喜歡使用這個。
爲了給出更多的背景,爲什麼我不能只包含它到主要config.yml中:我想能夠加載app/config/profiles/*.yml
,其中*
是配置文件名稱,我使用自己的Loader來完成此操作。如果有通配符導入配置文件的方法,那麼也可能適用於我。
注意:目前使用2.4,但如果有幫助,可以升級到2.5。