Silex 框架中的 YAML 配置文件,具有动态替换功能


YAML config file in Silex Framework with dynamic replacement

我创建了一个配置提供程序,用于处理 silex 的 yaml 文件中的配置。一切都还好!即使import功能也很好用。我遇到的唯一问题是动态替换被%%参数包围的参数。

base_path: /my/path/to/base
paths:
    web_path: %base_path%/web
    upload_path: %web_path%/uploads

我真的不知道Symfony配置组件是否可以以这种方式处理数据。

也许这段代码会对你有所帮助

function _flattenArray($array, &$flatten, $index = null, $path = null)
{
    if ('is_array($array)) {
        foreach ($array as $k => $v) {
            _flattenArray(
                $v,
                $flatten,
                (isset($index) ? $index . '.' : '') . $k,
                (isset($path) ? $path : '') . '[' . $k . ']'
            );
        }
    } else {
        $flatten[$index] = [
            'path' => $path,
            'value' => $array,
        ];
    }
}
$config = ...; // load config
$flatten = [];
_flattenArray($config, $flatten);
$parameterBag = [];
foreach ($flatten as $k => $v) {
    $parameterBag[$k] = $v['value'];
}
$parameterBag = new 'Symfony'Component'DependencyInjection'ParameterBag'ParameterBag($parameterBag);
$parameterBag->resolve();
$parameterBag = $parameterBag->all();
$propertyAccessor = 'Symfony'Component'PropertyAccess'PropertyAccess::createPropertyAccessor();
foreach ($parameterBag as $k => $v) {
    if ($v === $flatten[$k]['value']) continue;
    $propertyAccessor->setValue($config, $flatten[$k]['path'], $v);
}
dump($config);