Skip to content

Commit 5aac19c

Browse files
committed
Add examples folder
Fixed xml parser adn array parser bug version 1.0
1 parent 7086069 commit 5aac19c

13 files changed

Lines changed: 192 additions & 24 deletions

README.md

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,14 @@ PConfig 是一个使用了 PHP 编写的配置文件解析库,能够解析 PHP
44

55
PConfig is a PHP library for parsing config file ( e.g. php, json, xml, yaml, ini...) It has simple apis and  easy to use. You can custom your own provider and parser to process data.
66

7-
## 更新 Update
8-
9-
中文
10-
11-
- 2017-12-24 修改 `pconfig\Config#setConfigFile` api 为 `pconfig\Config#setPath`
12-
13-
English
14-
15-
- 2017-12-24 change `pconfig\Config#setConfigFile` api to `pconfig\Config#setPath`
16-
177
## 安装 Install
188

199
使用 PHP Composer 安装
2010

2111
Install by composer
2212

2313
```bash
24-
composer require oopsguy/pconfig
14+
composer require oopsguy/pconfig:1.0
2515
```
2616

2717
## 用法 Usage
@@ -45,7 +35,8 @@ $config->save();
4535
// json file
4636
$jsonConfig = DefaultConfigBuilder::build('config/config.json');
4737
$jsonConfig->set('homepage', 'https://github.com');
48-
$jsonConfig->setPath('config/temp_json.json'); //Save as temp_json.json file
38+
// Save as temp_json.json file
39+
$jsonConfig->setPath('config/temp_json.json');
4940
$jsonConfig->save();
5041

5142
$parser = new YamlParser();
@@ -69,12 +60,55 @@ $config = new Config(
6960
new JsonParser(), // Specify format parser
7061
new FileProvider(['file' => 'config/config.php']),
7162
[
72-
Config::CONFIG_KEY_CASE => Config::KEY_CASE_LOWER, // Change config keys into case lower
73-
Config::CONFIG_SEPARATOR => '.', // Setting config item separator
63+
// 配置项统一小写转换
64+
// Change config keys into case lower
65+
Config::CONFIG_KEY_CASE => Config::KEY_CASE_LOWER,
66+
// 配置项以 . 分割
67+
// Setting config item separator
68+
Config::CONFIG_SEPARATOR => '.',
7469
]
7570
);
7671
```
7772

73+
## 下标访问 ArrayAccess
74+
75+
配置实现了 SPL ArrayAccess 接口,支持下标操作方式
76+
77+
Implement SPL ArrayAccess interface.
78+
79+
```php
80+
<?php
81+
use pconfig\DefaultConfigBuilder;
82+
83+
require '../vendor/autoload.php';
84+
85+
// access by index
86+
$json = DefaultConfigBuilder::build('./config/arrayaccess.json');
87+
$json['status'] = true;
88+
$json['data'] = [
89+
'page' => 1,
90+
'pageSize' => 10,
91+
'pages' => 2,
92+
'total' => 13,
93+
'list' => [
94+
[
95+
'username' => 'oopsguy',
96+
'gender' => '男'
97+
]
98+
]
99+
];
100+
$json['msg'] = 'ok';
101+
$json['delData'] = 'XHSYSYSDkoksoada8dsaidsa9d8adsa';
102+
103+
// unset and isset
104+
var_dump(isset($json['delData']));
105+
unset($json['delData']);
106+
var_dump(isset($json['delData']));
107+
108+
// save config
109+
$json->save();
110+
```
111+
78112
config example
79113

80114
```php

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{
77
"name": "Oopsguy",
88
"email": "[email protected]",
9-
"homepage": "https://github.com/oopsguy/phpconf/graphs/contributors"
9+
"homepage": "https://github.com/oopsguy/pconfig/graphs/contributors"
1010
}
1111
],
1212
"support": {
13-
"issues": "https://github.com/oopsguy/phpconf/issues",
14-
"source": "https://github.com/oopsguy/phpconf"
13+
"issues": "https://github.com/oopsguy/pconfig/issues",
14+
"source": "https://github.com/oopsguy/pconfig"
1515
},
1616
"require": {
1717
"php": ">=5.6.0",

examples/arrayConfig.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
use pconfig\DefaultConfigBuilder;
3+
use pconfig\Config;
4+
use pconfig\parser\impl\YamlParser;
5+
use pconfig\provider\impl\FileProvider;
6+
7+
require '../vendor/autoload.php';
8+
9+
// php array
10+
$config = DefaultConfigBuilder::build("./config/arrayConfig.php");
11+
echo $config->get("app");
12+
$config->delete("version");
13+
$config->set('debug', false);
14+
$config->set("settings.key", "new value");
15+
echo $config['settings.key'] . PHP_EOL;
16+
$config->save();

examples/arrayaccess.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
use pconfig\DefaultConfigBuilder;
3+
4+
require '../vendor/autoload.php';
5+
6+
// access by index
7+
$json = DefaultConfigBuilder::build('./config/arrayaccess.json');
8+
$json['status'] = true;
9+
$json['data'] = [
10+
'page' => 1,
11+
'pageSize' => 10,
12+
'pages' => 2,
13+
'total' => 13,
14+
'list' => [
15+
[
16+
'username' => 'oopsguy',
17+
'gender' => ''
18+
]
19+
]
20+
];
21+
$json['msg'] = 'ok';
22+
$json['delData'] = 'XHSYSYSDkoksoada8dsaidsa9d8adsa';
23+
24+
// unset and isset
25+
var_dump(isset($json['delData']));
26+
unset($json['delData']);
27+
var_dump(isset($json['delData']));
28+
29+
// save config
30+
$json->save();
31+
32+

examples/config/arrayConfig.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
return array (
3+
0 => '',
4+
'debug' => false,
5+
'settings' =>
6+
array (
7+
'key' => 'new value',
8+
),
9+
);

examples/config/arrayaccess.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"status":true,"data":{"page":1,"pageSize":10,"pages":2,"total":13,"list":[{"username":"oopsguy","gender":""}]},"msg":"ok"}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<database>
2+
<type>nosql</type>
3+
<vendors>
4+
<vendor>redis</vendor>
5+
<vendor>mongo</vendor>
6+
<vendor>memcache</vendor>
7+
<vendor>levelDB</vendor>
8+
<vendor>etc</vendor>
9+
</vendors>
10+
</database>

examples/config/simpleXml.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<root>
2+
<database>oracle</database>
3+
<database>mysql</database>
4+
<database>db2</database>
5+
<database>sqlserver</database>
6+
<config>
7+
<host>localhost111</host>
8+
<username>root</username>
9+
<password>hello world</password>
10+
<port>3306</port>
11+
</config>
12+
</root>

examples/customXml.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
use pconfig\DefaultConfigBuilder;
3+
use pconfig\Config;
4+
use pconfig\parser\impl\XmlParser;
5+
use pconfig\provider\impl\FileProvider;
6+
7+
require '../vendor/autoload.php';
8+
9+
// simple xml
10+
$xml = DefaultConfigBuilder::build('./config/simpleXml.xml');
11+
// print_r($xml->get());
12+
// $xml->set('database', [
13+
// 'oracle', 'mysql', 'db2', 'sqlserver'
14+
// ]);
15+
// $xml->set('config', [
16+
// 'host' => 'localhost111',
17+
// 'username' => 'root',
18+
// 'password'=> 'root',
19+
// 'port' => 3306,
20+
// 'type' => 'mysql'
21+
// ]);
22+
$xml['config.password'] = 'hello world';
23+
unset($xml['config.type']);
24+
$xml->save();
25+
26+
// Custom xml root node name
27+
$parser = new XmlParser([
28+
'root' => 'database'
29+
]);
30+
$provider = new FileProvider(['file' => './config/databasesRootNode.xml']);
31+
$extConfig = new Config($parser, $provider);
32+
$extConfig->set('type', 'nosql');
33+
$extConfig->set('vendors.vendor', [
34+
'redis', 'mongo', 'memcache', 'levelDB', 'etc'
35+
]);
36+
$extConfig->save();

src/Config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ function __construct(IParser $parser, AbstractProvider $provider, array $extraCo
8383

8484
$content = $this->provider->read();
8585
$this->config = $this->parser->parse($content);
86+
87+
if (!is_array($this->config)) {
88+
$this->config = [];
89+
}
8690

8791
//配置项名称大小写处理
8892
if (isset($this->extraConfig[self::CONFIG_KEY_CASE])) {

0 commit comments

Comments
 (0)