Skip to content

Commit

Permalink
添加 Soli\Config 配置信息类
Browse files Browse the repository at this point in the history
  • Loading branch information
ueaner committed Mar 20, 2018
1 parent c37f659 commit 5746587
Show file tree
Hide file tree
Showing 2 changed files with 320 additions and 0 deletions.
165 changes: 165 additions & 0 deletions src/Soli/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php
/**
* @author ueaner <ueaner@gmail.com>
*/
namespace Soli;

/**
* Config.
*
*<code>
* $config = new \Soli\Config($arrayConfig);
*</code>
*/
class Config implements \ArrayAccess, \Countable
{
/**
* Config constructor.
*/
public function __construct(array $arrayConfig = null)
{
if (!empty($arrayConfig)) {
foreach ($arrayConfig as $key => $value) {
$this->offsetSet($key, $value);
}
}
}

/**
* var_dump(isset($config['database']));
*/
public function offsetExists($index)
{
return isset($this->{$index});
}

/**
* var_dump($config['database']);
*/
public function offsetGet($index)
{
return $this->{$index};
}

/**
* $config['database'] = [
* 'host' => '127.0.0.1',
* 'port' => '3306',
* ]
*/
public function offsetSet($index, $value)
{
if (is_array($value)) {
$this->{$index} = new self($value);
} else {
$this->{$index} = $value;
}
}

/**
* unset($config['cacheDir']);
*/
public function offsetUnset($index)
{
$this->{$index} = null;
}

/**
* echo count($config);
* echo $config->count();
*/
public function count()
{
return count(get_object_vars($this));
}

/**
* $config->set('database', [
* 'host' => '127.0.0.1',
* 'port' => '3306',
* ]);
* $config->set('database.host', '192.168.1.100');
* $config->set('database.dbname', 'demo');
*/
public function set($index, $value)
{
$config = $this;
$keys = explode('.', $index);

foreach ($keys as $key) {
if (!isset($config->{$key})) {
$config->{$key} = new self();
}

$config = &$config->{$key};
}

if (is_array($value)) {
$config = new self($value);
} else {
$config = $value;
}
}

/**
* print_r($config->get('database'));
* print_r($config->get('database.host', '192.168.1.100'));
*/
public function get($index, $defaultValue = null)
{
if (isset($this->{$index})) {
return $this->{$index};
}

$config = $this;
$keys = explode('.', $index);

while (!empty($keys)) {
$key = array_shift($keys);

if (!isset($config->{$key})) {
break;
}

if (empty($keys)) {
return $config->{$key};
}

$config = $config->{$key};

if (empty($config)) {
break;
}
}

return $defaultValue;
}

/**
* var_dump($config->toArray());
*/
public function toArray()
{
$arrayConfig = [];
foreach (get_object_vars($this) as $key => $value) {
if (is_object($value)) {
if (method_exists($value, 'toArray')) {
$arrayConfig[$key] = $value->toArray();
} else {
$arrayConfig[$key] = $value;
}
} else {
$arrayConfig[$key] = $value;
}
}
return $arrayConfig;
}

/**
* var_export($config);
*/
public static function __set_state($data)
{
return new self($data);
}
}
155 changes: 155 additions & 0 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Soli\Tests;

use PHPUnit\Framework\TestCase;

use Soli\Config;

class ConfigTest extends TestCase
{
public function testInitializeWithData()
{
$data = ['foo' => 'bar'];
$config = new Config($data);

$this->assertEquals($data, $config->toArray());
}

public function testSet()
{
$config = new Config();

$config->set('database', [
'master' => [
'host' => '127.0.0.1',
'port' => '3306',
],
]);
$this->assertEquals('127.0.0.1', $config['database']['master']['host']);
$this->assertEquals('3306', $config->database->master->port);

$config->set('database.master', [
'host' => '192.168.1.100',
'port' => '3100',
]);
$this->assertEquals('192.168.1.100', $config['database']['master']['host']);
$this->assertEquals('3100', $config->database->master->port);

$config->set('database.master.host', '192.168.1.101');
$config->set('database.master.dbname', 'demo');
$this->assertEquals('192.168.1.101', $config->database->master->host);
$this->assertEquals('demo', $config->database->master->dbname);

$config->set('database.slave', [
'host' => '192.168.1.200',
'port' => '3200',
]);
$this->assertEquals('192.168.1.200', $config['database']['slave']['host']);
$this->assertEquals('3200', $config->database->slave->port);

$config->set('database', 'just a string');
$this->assertEquals('just a string', $config->database);

$this->assertArrayHasKey('database', $config);
}

public function testOffsetSet()
{
$config = new Config();
$config['foo'] = 'bar';
$this->assertArrayHasKey('foo', $config);
$this->assertEquals('bar', $config['foo']);
}

public function testGet()
{
$config = new Config();
$config->set('foo.bar', 'baz');
$this->assertEquals('baz', $config->get('foo.bar'));

$foo = $config->get('foo');
$this->assertInstanceOf(Config::class, $foo);
$this->assertEquals('baz', $foo->bar);

$config['foo.bar'] = 'baz2000';
$this->assertEquals('baz2000', $config->get('foo.bar'));

$this->assertEquals('baz2000', $config->get('foo.bar2000', 'baz2000'));

$config->set('foooooo.bar', '');
$this->assertEquals('baz2000', $config->get('foooooo.bar.baz', 'baz2000'));

$this->assertEquals(null, $config->get('unknown.path'));
}

public function testOffsetGet()
{
$config = new Config();
$config['foo'] = 'bar';
$this->assertEquals('bar', $config['foo']);
}

public function testGetWithDefault()
{
$config = new Config();
$config['foo'] = 'bar';
$this->assertEquals('default', $config->get('unknown.path', 'default'));
}

public function testOffsetExists()
{
$config = new Config();
$config['foo'] = 'bar';
$this->assertTrue(isset($config['foo']));
}

public function testOffsetUnset()
{
$data = [
'abc' => '123',
'foo' => 'bar',
];
$config = new Config($data);

unset($config['foo']);
$this->assertNull($config['foo']);
}

public function testNumeric()
{
$config = new Config(['abc']);
$this->assertEquals('abc', $config->{0});
$this->assertEquals('abc', $config[0]);
$this->assertEquals('abc', $config['0']);
}

public function testCount()
{
$data = [
'abc' => '123',
'foo' => 'bar',
];
$config = new Config($data);
$this->assertEquals(2, $config->count());
}

public function testToArray()
{
$config = Config::__set_state([
'database' => Config::__set_state([
'master' => [
'host' => '127.0.0.1',
'port' => '3306',
],
'charset' => 'utf8',
])
]);

$data = $config->toArray();

$this->assertArrayHasKey('database', $data);
$this->assertEquals('3306', $data['database']['master']['port']);
$this->assertEquals('utf8', $data['database']['charset']);
}
}

0 comments on commit 5746587

Please sign in to comment.