Skip to content

Commit 5746587

Browse files
committed
添加 Soli\Config 配置信息类
1 parent c37f659 commit 5746587

File tree

2 files changed

+320
-0
lines changed

2 files changed

+320
-0
lines changed

src/Soli/Config.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
/**
3+
* @author ueaner <[email protected]>
4+
*/
5+
namespace Soli;
6+
7+
/**
8+
* Config.
9+
*
10+
*<code>
11+
* $config = new \Soli\Config($arrayConfig);
12+
*</code>
13+
*/
14+
class Config implements \ArrayAccess, \Countable
15+
{
16+
/**
17+
* Config constructor.
18+
*/
19+
public function __construct(array $arrayConfig = null)
20+
{
21+
if (!empty($arrayConfig)) {
22+
foreach ($arrayConfig as $key => $value) {
23+
$this->offsetSet($key, $value);
24+
}
25+
}
26+
}
27+
28+
/**
29+
* var_dump(isset($config['database']));
30+
*/
31+
public function offsetExists($index)
32+
{
33+
return isset($this->{$index});
34+
}
35+
36+
/**
37+
* var_dump($config['database']);
38+
*/
39+
public function offsetGet($index)
40+
{
41+
return $this->{$index};
42+
}
43+
44+
/**
45+
* $config['database'] = [
46+
* 'host' => '127.0.0.1',
47+
* 'port' => '3306',
48+
* ]
49+
*/
50+
public function offsetSet($index, $value)
51+
{
52+
if (is_array($value)) {
53+
$this->{$index} = new self($value);
54+
} else {
55+
$this->{$index} = $value;
56+
}
57+
}
58+
59+
/**
60+
* unset($config['cacheDir']);
61+
*/
62+
public function offsetUnset($index)
63+
{
64+
$this->{$index} = null;
65+
}
66+
67+
/**
68+
* echo count($config);
69+
* echo $config->count();
70+
*/
71+
public function count()
72+
{
73+
return count(get_object_vars($this));
74+
}
75+
76+
/**
77+
* $config->set('database', [
78+
* 'host' => '127.0.0.1',
79+
* 'port' => '3306',
80+
* ]);
81+
* $config->set('database.host', '192.168.1.100');
82+
* $config->set('database.dbname', 'demo');
83+
*/
84+
public function set($index, $value)
85+
{
86+
$config = $this;
87+
$keys = explode('.', $index);
88+
89+
foreach ($keys as $key) {
90+
if (!isset($config->{$key})) {
91+
$config->{$key} = new self();
92+
}
93+
94+
$config = &$config->{$key};
95+
}
96+
97+
if (is_array($value)) {
98+
$config = new self($value);
99+
} else {
100+
$config = $value;
101+
}
102+
}
103+
104+
/**
105+
* print_r($config->get('database'));
106+
* print_r($config->get('database.host', '192.168.1.100'));
107+
*/
108+
public function get($index, $defaultValue = null)
109+
{
110+
if (isset($this->{$index})) {
111+
return $this->{$index};
112+
}
113+
114+
$config = $this;
115+
$keys = explode('.', $index);
116+
117+
while (!empty($keys)) {
118+
$key = array_shift($keys);
119+
120+
if (!isset($config->{$key})) {
121+
break;
122+
}
123+
124+
if (empty($keys)) {
125+
return $config->{$key};
126+
}
127+
128+
$config = $config->{$key};
129+
130+
if (empty($config)) {
131+
break;
132+
}
133+
}
134+
135+
return $defaultValue;
136+
}
137+
138+
/**
139+
* var_dump($config->toArray());
140+
*/
141+
public function toArray()
142+
{
143+
$arrayConfig = [];
144+
foreach (get_object_vars($this) as $key => $value) {
145+
if (is_object($value)) {
146+
if (method_exists($value, 'toArray')) {
147+
$arrayConfig[$key] = $value->toArray();
148+
} else {
149+
$arrayConfig[$key] = $value;
150+
}
151+
} else {
152+
$arrayConfig[$key] = $value;
153+
}
154+
}
155+
return $arrayConfig;
156+
}
157+
158+
/**
159+
* var_export($config);
160+
*/
161+
public static function __set_state($data)
162+
{
163+
return new self($data);
164+
}
165+
}

tests/ConfigTest.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
3+
namespace Soli\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
use Soli\Config;
8+
9+
class ConfigTest extends TestCase
10+
{
11+
public function testInitializeWithData()
12+
{
13+
$data = ['foo' => 'bar'];
14+
$config = new Config($data);
15+
16+
$this->assertEquals($data, $config->toArray());
17+
}
18+
19+
public function testSet()
20+
{
21+
$config = new Config();
22+
23+
$config->set('database', [
24+
'master' => [
25+
'host' => '127.0.0.1',
26+
'port' => '3306',
27+
],
28+
]);
29+
$this->assertEquals('127.0.0.1', $config['database']['master']['host']);
30+
$this->assertEquals('3306', $config->database->master->port);
31+
32+
$config->set('database.master', [
33+
'host' => '192.168.1.100',
34+
'port' => '3100',
35+
]);
36+
$this->assertEquals('192.168.1.100', $config['database']['master']['host']);
37+
$this->assertEquals('3100', $config->database->master->port);
38+
39+
$config->set('database.master.host', '192.168.1.101');
40+
$config->set('database.master.dbname', 'demo');
41+
$this->assertEquals('192.168.1.101', $config->database->master->host);
42+
$this->assertEquals('demo', $config->database->master->dbname);
43+
44+
$config->set('database.slave', [
45+
'host' => '192.168.1.200',
46+
'port' => '3200',
47+
]);
48+
$this->assertEquals('192.168.1.200', $config['database']['slave']['host']);
49+
$this->assertEquals('3200', $config->database->slave->port);
50+
51+
$config->set('database', 'just a string');
52+
$this->assertEquals('just a string', $config->database);
53+
54+
$this->assertArrayHasKey('database', $config);
55+
}
56+
57+
public function testOffsetSet()
58+
{
59+
$config = new Config();
60+
$config['foo'] = 'bar';
61+
$this->assertArrayHasKey('foo', $config);
62+
$this->assertEquals('bar', $config['foo']);
63+
}
64+
65+
public function testGet()
66+
{
67+
$config = new Config();
68+
$config->set('foo.bar', 'baz');
69+
$this->assertEquals('baz', $config->get('foo.bar'));
70+
71+
$foo = $config->get('foo');
72+
$this->assertInstanceOf(Config::class, $foo);
73+
$this->assertEquals('baz', $foo->bar);
74+
75+
$config['foo.bar'] = 'baz2000';
76+
$this->assertEquals('baz2000', $config->get('foo.bar'));
77+
78+
$this->assertEquals('baz2000', $config->get('foo.bar2000', 'baz2000'));
79+
80+
$config->set('foooooo.bar', '');
81+
$this->assertEquals('baz2000', $config->get('foooooo.bar.baz', 'baz2000'));
82+
83+
$this->assertEquals(null, $config->get('unknown.path'));
84+
}
85+
86+
public function testOffsetGet()
87+
{
88+
$config = new Config();
89+
$config['foo'] = 'bar';
90+
$this->assertEquals('bar', $config['foo']);
91+
}
92+
93+
public function testGetWithDefault()
94+
{
95+
$config = new Config();
96+
$config['foo'] = 'bar';
97+
$this->assertEquals('default', $config->get('unknown.path', 'default'));
98+
}
99+
100+
public function testOffsetExists()
101+
{
102+
$config = new Config();
103+
$config['foo'] = 'bar';
104+
$this->assertTrue(isset($config['foo']));
105+
}
106+
107+
public function testOffsetUnset()
108+
{
109+
$data = [
110+
'abc' => '123',
111+
'foo' => 'bar',
112+
];
113+
$config = new Config($data);
114+
115+
unset($config['foo']);
116+
$this->assertNull($config['foo']);
117+
}
118+
119+
public function testNumeric()
120+
{
121+
$config = new Config(['abc']);
122+
$this->assertEquals('abc', $config->{0});
123+
$this->assertEquals('abc', $config[0]);
124+
$this->assertEquals('abc', $config['0']);
125+
}
126+
127+
public function testCount()
128+
{
129+
$data = [
130+
'abc' => '123',
131+
'foo' => 'bar',
132+
];
133+
$config = new Config($data);
134+
$this->assertEquals(2, $config->count());
135+
}
136+
137+
public function testToArray()
138+
{
139+
$config = Config::__set_state([
140+
'database' => Config::__set_state([
141+
'master' => [
142+
'host' => '127.0.0.1',
143+
'port' => '3306',
144+
],
145+
'charset' => 'utf8',
146+
])
147+
]);
148+
149+
$data = $config->toArray();
150+
151+
$this->assertArrayHasKey('database', $data);
152+
$this->assertEquals('3306', $data['database']['master']['port']);
153+
$this->assertEquals('utf8', $data['database']['charset']);
154+
}
155+
}

0 commit comments

Comments
 (0)