Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oopsguy/pconfig
A PHP library for parsing and persisting configuration files (json, yaml, ini, xml and php). 一个 PHP 配置文件工具库,可解析和持久化配置文件内容,简化文件操作。
https://github.com/oopsguy/pconfig
config configuration ini json lib parser php php-array php-library php-utility utils xml yaml
Last synced: 6 days ago
JSON representation
A PHP library for parsing and persisting configuration files (json, yaml, ini, xml and php). 一个 PHP 配置文件工具库,可解析和持久化配置文件内容,简化文件操作。
- Host: GitHub
- URL: https://github.com/oopsguy/pconfig
- Owner: oopsguy
- License: mit
- Created: 2017-03-31T16:10:57.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-01T12:12:41.000Z (over 5 years ago)
- Last Synced: 2024-04-19T15:08:41.902Z (7 months ago)
- Topics: config, configuration, ini, json, lib, parser, php, php-array, php-library, php-utility, utils, xml, yaml
- Language: PHP
- Homepage:
- Size: 52.7 KB
- Stars: 5
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# PConfig
PConfig is a PHP library for parsing configuration.
It is lightweight and easy to use.## Supported formats
- php
- json
- xml
- yaml
- ini## Installation
```bash
composer require oopsguy/pconfig
```## Usage
```php
'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
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
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
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.
```php
$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