PConfig

PConfig is a PHP library for parsing configuration. It is lightweight and easy to use.

Supported formats

php json xml yaml ini

Installation

composer require oopsguy/pconfig

Usage

<?php use pconfigPConfig; use pconfigproviderimplFileProvider; use pconfigserializerimplJSONSerializer; $config = new PConfig('config-file.json'); $config = new PConfig([ 'file' => 'config-file.json' ]); $config = new PConfig([ 'data' => [ 'key' => 'value' // more... ] ]); $config->setSerializer(new JSONSerializer()); $config->setProvider(new FileProvider('config/a.json')); $config = new PConfig([ 'file' => 'config-file.json', 'serializer' => new JSONSerializer(), ]); $config = new PConfig([ 'serializer' => new JSONSerializer(), 'provider' => new FileProvider('config-file.json'), ]);

<?php use pconfigPConfig; use pconfigserializerimplYAMLSerializer; use pconfigproviderimplFileProvider; // PHP array // Automatically detect file extension and select a suitable serializer $config = new PConfig("config/config.php"); echo $config->get("app"); $config->delete("version"); $config->set('debug', false); $config->set("settings.key", "new value"); $config->save(); // handle JSON $jsonConfig = new PConfig('config/config.json'); $jsonConfig->set('homepage', 'https://github.com'); // Save as temp.json file $jsonConfig->setFile('config/temp.json'); $jsonConfig->save(); // Parsing YAML // Explicitly specify a YAML serializer $serializer = new YAMLSerializer(); $provider = new FileProvider('config/settings.yaml'); $extConfig = new PConfig([ 'provider' => $provider, 'serializer' => $serializer ]); $extConfig->set('type', 'yaml'); $extConfig->save();

The default key separator is a dot-notation ..

key1.key2.key3

You can use PConfig::CONFIG_KEY_EXTRACT_SEPARATOR to custom your own separator.

PConfig::CONFIG_KEY_EXTRACT_SEPARATOR => '-',

key1-key2-key3

<?php use pconfigPConfig; use pconfigproviderimplFileProvider; use pconfigserializerimplJSONSerializer; $config = new PConfig([ // Specify the serializer 'serializer' => new JSONSerializer(), 'provider' => new FileProvider('config/config.php'), 'config' => [ // Set the key separator PConfig::CONFIG_KEY_EXTRACT_SEPARATOR => '-', ] ]);

ArrayAccess

PConfig has implemented ArrayAccess interface, you can access configuration by array operations.

<?php use pconfigPConfig; // Access by index $json = new PConfig('./config/arrayaccess.json'); $json['status'] = true; $json['data'] = [ 'page' => 1, 'pageSize' => 10, 'pages' => 2, 'total' => 13, 'list' => [ [ 'username' => 'oopsguy', 'gender' => '男' ] ] ]; $json['msg'] = 'ok'; $json['delData'] = 'XHSYSYSDkoksoada8dsaidsa9d8adsa'; // unset and isset API var_dump(isset($json['delData'])); unset($json['delData']); var_dump(isset($json['delData'])); // Save to file $json->save();

Nested configuration.

$config->set('level1.level2.level3', "Level end"); $config->delete('level1.level2');

APIs

set($key, $value) get($key[, $defult]) delete($key) exists($key) getConfig($key) setConfig($key, $value) setProvider($provider) setSerializer($serializer) setFile($path) reload() clear() save()

Licence

MIT Licence

版权声明:

1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。
2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。