-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |