juzaweb / config
支持PHP、INI、XML、JSON和YAML文件的超轻量级配置文件加载器
Requires
- php: >=5.5.9
Requires (Dev)
- phpunit/phpunit: ~4.8.36 || ~5.7 || ~6.5 || ~7.5
- scrutinizer/ocular: ~1.1
- squizlabs/php_codesniffer: ~2.2
- symfony/yaml: ~3.4
Suggests
- symfony/yaml: ~3.4
README
Config是一个支持PHP、INI、XML、JSON、YML、Properties和序列化文件和字符串的文件配置加载器。
要求
Config需要PHP 5.5.9+。
重要:如果您想使用YAML文件或字符串,请在您的
composer.json
中要求Symfony Yaml组件。
安装
安装Config的推荐方法是使用Composer。
$ composer require hassankhan/config
使用
Config设计得非常简单直观。您可以使用它来加载、获取和设置。
加载文件
可以通过工厂方法load()
或直接实例化创建Config
对象。
use Noodlehaus\Config; use Noodlehaus\Parser\Json; // Load a single file $conf = Config::load('config.json'); $conf = new Config('config.json'); // Load values from multiple files $conf = new Config(['config.json', 'config.xml']); // Load all supported files in a directory $conf = new Config(__DIR__ . '/config'); // Load values from optional files $conf = new Config(['config.dist.json', '?config.json']); // Load a file using specified parser $conf = new Config('configuration.config', new Json);
根据文件扩展名或指定的解析器解析并加载文件。如果指定了解析器,则它将用于所有文件。注意,当加载多个文件时,具有重复键的条目将采用最后一个加载文件的值。
当加载目录时,路径将被glob
,文件按名称字母顺序加载。
警告:不要包含未信任的配置在PHP格式中。它可能包含并执行恶意代码。
加载字符串
可以通过工厂方法load()
或通过设置参数$string
为true
直接实例化来创建字符串配置。
use Noodlehaus\Config; use Noodlehaus\Parser\Json; use Noodlehaus\Parser\Yaml; $settingsJson = <<<FOOBAR { "application": { "name": "configuration", "secret": "s3cr3t" }, "host": "localhost", "port": 80, "servers": [ "host1", "host2", "host3" ] } FOOBAR; $settingsYaml = <<<FOOBAR application: name: configuration secret: s3cr3t host: localhost port: 80 servers: - host1 - host2 - host3 FOOBAR; $conf = Config::load($settingsJson, new Json, true); $conf = new Config($settingsYaml, new Yaml, true);
警告:不要包含未信任的配置在PHP格式中。它可能包含并执行恶意代码。
获取值
可以通过三种方式获取值。一种是通过使用get()
方法
// Get value using key $debug = $conf->get('debug'); // Get value using nested key $secret = $conf->get('security.secret'); // Get a value with a fallback $ttl = $conf->get('app.timeout', 3000);
第二种方法,就像使用数组一样使用它
// Get value using a simple key $debug = $conf['debug']; // Get value using a nested key $secret = $conf['security.secret']; // Get nested value like you would from a nested array $secret = $conf['security']['secret'];
第三种方法,是使用all()
方法
// Get all values $data = $conf->all();
设置值
尽管Config支持通过set()
或通过数组语法设置值,但通过这种方式进行的任何更改都不会反映到源文件中。按设计,如果您需要更改配置文件,您必须手动进行。
$conf = Config::load('config.json'); // Sample value from our config file assert($conf['secret'] == '123'); // Update config value to something else $conf['secret'] = '456'; // Reload the file $conf = Config::load('config.json'); // Same value as before assert($conf['secret'] == '123'); // This will fail assert($conf['secret'] == '456');
保存配置
可以将配置保存回任何支持的格式(除PHP外)的文件。
$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'); $config->toFile('config.txt', new Serialize()); // you can also force the writer
使用默认值
有时在您的项目中,您可能想使用Config来存储应用程序设置,而不需要文件I/O。您可以通过扩展AbstractConfig
类并填充getDefaults()
方法来实现这一点
class MyConfig extends AbstractConfig { protected function getDefaults() { return [ 'host' => 'localhost', 'port' => 80, 'servers' => [ 'host1', 'host2', 'host3' ], 'application' => [ 'name' => 'configuration', 'secret' => 's3cr3t' ] ]; } }
合并实例
您可能想合并多个Config实例
$conf1 = Config::load('conf1.json'); $conf2 = Config::load('conf2.json'); $conf1->merge($conf2);
支持的配置文件示例
可以在这里找到简单、有效的配置文件示例:here。
测试
$ phpunit
贡献
请参阅CONTRIBUTING以获取详细信息。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件contact@hassankhan.me而不是使用问题跟踪器。
致谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。