Skip to content

Commit 01c384c

Browse files
committed
Update README and testing.
1 parent 170bdab commit 01c384c

5 files changed

Lines changed: 137 additions & 27 deletions

File tree

README.md

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,52 @@ composer require oopsguy/pconfig
2121

2222
```php
2323
<?php
24+
use pconfig\PConfig;
25+
use \pconfig\provider\impl\FileProvider;
26+
use \pconfig\serializer\impl\JSONSerializer;
27+
28+
$config = new PConfig('config-file.json');
29+
30+
$config = new PConfig([
31+
'file' => 'config-file.json'
32+
]);
33+
34+
$config = new PConfig([
35+
'data' => [
36+
'key' => 'value'
37+
// more...
38+
]
39+
]);
40+
$config->setSerializer(new JSONSerializer());
41+
$config->setProvider(new FileProvider('config/a.json'));
2442

43+
$config = new PConfig([
44+
'file' => 'config-file.json',
45+
'serializer' => new JSONSerializer(),
46+
]);
47+
48+
$config = new PConfig([
49+
'serializer' => new JSONSerializer(),
50+
'provider' => new FileProvider('config-file.json'),
51+
]);
52+
```
53+
54+
```php
55+
<?php
2556
use pconfig\PConfig;
2657
use pconfig\serializer\impl\YAMLSerializer;
2758
use pconfig\provider\impl\FileProvider;
2859

29-
// Parsing PHP array
30-
// Auto detect file extension and choose a suitable serializer
60+
// PHP array
61+
// Automatically detect file extension and select a suitable serializer
3162
$config = new PConfig("config/config.php");
3263
echo $config->get("app");
3364
$config->delete("version");
3465
$config->set('debug', false);
3566
$config->set("settings.key", "new value");
3667
$config->save();
3768

38-
// Parsing JSON
69+
// handle JSON
3970
$jsonConfig = new PConfig('config/config.json');
4071
$jsonConfig->set('homepage', 'https://github.com');
4172
// Save as temp.json file
@@ -89,7 +120,7 @@ $config = new PConfig([
89120

90121
## ArrayAccess
91122

92-
`Config` has implemented `ArrayAccess` interface, you can access configuration by index.
123+
`PConfig` has implemented `ArrayAccess` interface, you can access configuration by array operations.
93124

94125
```php
95126
<?php
@@ -137,6 +168,8 @@ $config->delete('level1.level2');
137168
- `exists($key)`
138169
- `getConfig($key)`
139170
- `setConfig($key, $value)`
171+
- `setProvider($provider)`
172+
- `setSerializer($serializer)`
140173
- `setFile($path)`
141174
- `reload()`
142175
- `clear()`

examples/config/arrayConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22
return ['0' => '',
33
'debug' => false,
44
'settings' => [

src/PConfig.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Closure;
77
use Exception;
88
use pconfig\provider\impl\FileProvider;
9+
use pconfig\provider\IProvider;
910
use pconfig\serializer\ISerializer;
1011
use pconfig\utils\ArrayUtil;
1112
use pconfig\utils\ClassUtil;
@@ -78,24 +79,26 @@ public function __construct($config)
7879
$this->loadData();
7980
} elseif (is_array($config)) {
8081
$file = null;
82+
$this->provider = $config[self::CONFIG_KEY_PROVIDER];
83+
$this->serializer = $config[self::CONFIG_KEY_SERIALIZER];
84+
8185
if (isset($config[self::CONFIG_KEY_DATA])) {
8286
$this->data = $config[self::CONFIG_KEY_DATA];
8387
} elseif (isset($config[self::CONFIG_KEY_FILE])) {
8488
$file = $config[self::CONFIG_KEY_FILE];
85-
$this->provider = new FileProvider($file);
86-
}
87-
88-
$this->serializer = $config[self::CONFIG_KEY_SERIALIZER];
89-
90-
if (is_null($this->provider)) {
91-
$this->provider = $config[self::CONFIG_KEY_PROVIDER];
92-
}
89+
if (is_null($this->provider)) {
90+
$this->provider = new FileProvider($file);
91+
}
9392

94-
if (!empty($file)) {
9593
if (is_null($this->serializer)) {
9694
$this->serializer = $this->detectSerializer($file);
9795
}
9896
$this->loadData();
97+
} else {
98+
if (is_null($this->provider)) {
99+
throw new Exception("must provide 'data', 'file' or 'provider'");
100+
}
101+
$this->loadData();
99102
}
100103

101104
if (isset($config[self::CONFIG_KEY_EXTRACT_CONFIG])) {
@@ -104,6 +107,8 @@ public function __construct($config)
104107
$this->config = array_merge($this->config, $extraConfig);
105108
}
106109
}
110+
} else {
111+
throw new Exception('invalid parameter, require a string or an array');
107112
}
108113
}
109114

@@ -166,6 +171,22 @@ public function setFile($file)
166171
$this->provider->setFile($file);
167172
}
168173

174+
/**
175+
* @param IProvider $provider
176+
*/
177+
public function setProvider($provider)
178+
{
179+
$this->provider = $provider;
180+
}
181+
182+
/**
183+
* @param ISerializer $serializer
184+
*/
185+
public function setSerializer($serializer)
186+
{
187+
$this->serializer = $serializer;
188+
}
189+
169190
public function offsetExists($offset)
170191
{
171192
return $this->exists($offset);

test/PConfigTest.php

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Exception;
66
use pconfig\PConfig;
7+
use pconfig\provider\impl\FileProvider;
8+
use pconfig\serializer\impl\JSONSerializer;
79
use PHPUnit\Framework\TestCase;
810

911
/**
@@ -34,38 +36,92 @@ public function testYAML()
3436
$this->_testConfig('yaml');
3537
}
3638

37-
private function _testConfig($type)
39+
public function testNew()
3840
{
39-
$config = null;
4041
try {
41-
$config = new PConfig($this->basePath . 'config.' . $type);
42+
$config = new PConfig($this->basePath . 'config.json');
43+
$this->_testPartial($config);
4244
} catch (Exception $e) {
4345
$this->fail($e->getMessage());
4446
}
45-
$this->assertNotNull($config);
4647

47-
$this->assertArrayHasKey('language', $config);
48-
$this->assertArrayHasKey("php", $config->get('language'));
49-
$this->assertArrayHasKey("php", $config['language']);
50-
$this->assertArrayHasKey("type", $config['language']['php']);
51-
$this->assertCount(3, $config['language']['php']['type']);
52-
$this->assertContains('object', $config['language']['php']['type']);
48+
try {
49+
$config = new PConfig([
50+
PConfig::CONFIG_KEY_FILE => $this->basePath . 'config.json'
51+
]);
52+
$this->_testPartial($config);
53+
} catch (Exception $e) {
54+
$this->fail($e->getMessage());
55+
}
5356

5457
try {
5558
$config = new PConfig([
56-
'file' => $this->basePath . 'config.' . $type,
59+
PConfig::CONFIG_KEY_DATA => [
60+
'language' => [
61+
'php' => [
62+
'type' => ['array', 'string', 'object']
63+
]
64+
]
65+
]
5766
]);
67+
$this->_testPartial($config);
5868
} catch (Exception $e) {
5969
$this->fail($e->getMessage());
6070
}
61-
$this->assertNotNull($config);
6271

72+
try {
73+
$config = new PConfig([
74+
PConfig::CONFIG_KEY_FILE => $this->basePath . 'config.json',
75+
PConfig::CONFIG_KEY_SERIALIZER => new JSONSerializer(),
76+
]);
77+
$this->_testPartial($config);
78+
} catch (Exception $e) {
79+
$this->fail($e->getMessage());
80+
}
81+
82+
try {
83+
$config = new PConfig([
84+
PConfig::CONFIG_KEY_PROVIDER => new FileProvider($this->basePath . 'config.json'),
85+
PConfig::CONFIG_KEY_SERIALIZER => new JSONSerializer(),
86+
]);
87+
$this->_testPartial($config);
88+
} catch (Exception $e) {
89+
$this->fail($e->getMessage());
90+
}
91+
}
92+
93+
/**
94+
* @param PConfig $config
95+
*/
96+
private function _testPartial($config)
97+
{
98+
$this->assertNotNull($config);
6399
$this->assertArrayHasKey('language', $config);
64100
$this->assertArrayHasKey("php", $config->get('language'));
65101
$this->assertArrayHasKey("php", $config['language']);
66102
$this->assertArrayHasKey("type", $config['language']['php']);
67103
$this->assertCount(3, $config['language']['php']['type']);
68104
$this->assertContains('object', $config['language']['php']['type']);
105+
}
106+
107+
private function _testConfig($type)
108+
{
109+
$config = null;
110+
try {
111+
$config = new PConfig($this->basePath . 'config.' . $type);
112+
} catch (Exception $e) {
113+
$this->fail($e->getMessage());
114+
}
115+
$this->_testPartial($config);
116+
117+
try {
118+
$config = new PConfig([
119+
'file' => $this->basePath . 'config.' . $type,
120+
]);
121+
} catch (Exception $e) {
122+
$this->fail($e->getMessage());
123+
}
124+
$this->_testPartial($config);
69125

70126
$this->assertTrue($config->set('hello.world', true));
71127

test/provider/data/data.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1561753033
1+
1561754394

0 commit comments

Comments
 (0)