config 解析 json xml ini yaml

最后更新于:2022-04-02 02:25:06

[TOC] > [github](https://github.com/hassankhan/config) ## 安装 `composer require hassankhan/config` ## 概述 可以解析 json , xml , ini , yaml ### 解析 ini config.ini ``` host = localhost port = 80 servers[] = host1 servers[] = host2 servers[] = host3 [application] name = configuration secret = s3cr3t ``` ``` $conf = \Noodlehaus\Config::load("config.ini"); $var = $conf->get("servers"); /** * array ( 'host1', 'host2', 'host3', ) */ ``` ### 解析 json config.json ``` { "application": { "name": "configuration", "secret": "s3cr3t" }, "host": "localhost", "port": 80, "servers": [ "host1", "host2", "host3" ] } ``` ``` $conf = \Noodlehaus\Config::load("config.json"); $var = $conf->get("servers"); /** * array ( 'host1', 'host2', 'host3', ) */ ``` ## 解析 yaml 需要 在安装依赖 `composer require symfony/yaml` ``` $conf = \Noodlehaus\Config::load("config.yaml"); $var = $conf->get("servers"); /** * array ( 'host1', 'host2', 'host3', ) */ ``` ### 批量导入 配置 ``` //指定文件 $conf = new Config(['config.json', 'config.xml']); // 制定目录下的所有文件 $conf = new Config(__DIR__ . '/config'); ``` ### 配置格式转换 ``` $config = Config::load('config.json'); $ini = $config->toString(new Ini()); // Encode to string if you want to save the file yourself $config->toFile('config.yaml'); ``` ### 合并配置文件 ``` $conf1 = Config::load('conf1.json'); $conf2 = Config::load('conf2.json'); $conf1->merge($conf2); ```
';