wandu / config
此包已被弃用且不再维护。未建议替代包。
基于点数组的简单配置。
v4.0.0-beta2
2017-08-08 16:29 UTC
Requires
- php: >=7.0
Suggests
- m1/env: for the Env Loader.
- symfony/yaml: for the Yaml Loader.
This package is not auto-updated.
Last update: 2022-04-02 06:16:15 UTC
README
基于点数组的简单配置。
安装
composer require wandu/config
使用方法
$config = new \Wandu\Config\Config([ 'host' => 'wandu.github.io', 'log' => [ 'handler' => 'monolog', 'path' => 'log/wandu.log', ], ]); static::assertSame('wandu.github.io', $config->get('host')); static::assertSame('wandu.github.io', $config['host']); static::assertSame([ 'handler' => 'monolog', 'path' => 'log/wandu.log', ], $config->get('log')); static::assertSame([ 'handler' => 'monolog', 'path' => 'log/wandu.log', ], $config['log']); static::assertSame('log/wandu.log', $config->get('log.path')); static::assertSame('log/wandu.log', $config['log']['path']); static::assertSame('log/wandu.log', $config['log.path']); // you can use dot syntax in array!
使用默认值
$config = new \Wandu\Config\Config([ 'debug' => false, 'cache_dir' => null, ]); static::assertSame(false, $config['debug']); static::assertSame(null, $config['cache_dir']); static::assertSame(null, $config['unknown']); static::assertSame(false, $config->get('debug', true)); static::assertSame(null, $config->get('cache_dir', "/")); // check by array_key_exists static::assertSame("unknown text..", $config->get('unknown', "unknown text.."));
支持加载器
- PHP (示例,test_php.php)
- JSON (示例,test_json.json)
- 环境变量(需要
m1/env
)(示例,test_env.env) - YAML(需要
symfony/yaml
)(示例,test_yml.yml)
$config = new \Wandu\Config\Config(); $config->pushLoader(new \Wandu\Config\Loader\PhpLoader()); $config->pushLoader(new \Wandu\Config\Loader\JsonLoader()); $config->pushLoader(new \Wandu\Config\Loader\EnvLoader()); $config->pushLoader(new \Wandu\Config\Loader\YmlLoader()); $config->pushLoader(new \Wandu\Config\Loader\PathLoader([ new \Wandu\Config\Loader\YmlLoader(), ])); $config->load(__DIR__ . '/test_php.php'); $config->load(__DIR__ . '/test_json.json'); $config->load(__DIR__ . '/test_env.env'); $config->load(__DIR__ . '/test_yml.yml'); $config->load(__DIR__ . '/test_path'); static::assertSame([ 'foo' => 'foo string', 'vendor1' => [ 'service1' => [ 'name' => 'vendor1 service1 name..', 'path' => 'vendor1 service1 path..', ], 'service2' => [ 'name' => 'vendor1 service2 name..', 'path' => 'vendor1 service2 path..', ], ], 'vendor2' => [ 'service1' => [ 'name' => 'vendor2 service1 name..', 'path' => 'vendor2 service1 path..', ], 'service2' => [ 'name' => 'vendor2 service2 name..', 'path' => 'vendor2 service2 path..', ], ], 'json1' => 'json 1 string', 'json2' => [ 'json2-1', 'json2-2', ], 'env1' => 'what the', 'env2' => false, 'yml1' => [ 'yml11' => true, ], 'yml2' => [ 'paths' => ['vendor/*', 'tests/*'] ], 'yml3' => [ 'yml3_1', 'yml3_2', ], 'app' => [ 'debug' => true, 'env' => 'test', ], ], $config->toArray());