Added Config->getNested(key) and Config->setNested(key, value)

This commit is contained in:
Shoghi Cervantes 2014-12-07 16:41:43 +01:00
parent 747f7685e7
commit 964bf98ca6
2 changed files with 44 additions and 17 deletions

View File

@ -1224,24 +1224,9 @@ class Server{
* @return mixed
*/
public function getProperty($variable, $defaultValue = null){
$vars = explode(".", $variable);
$base = array_shift($vars);
if($this->config->exists($base)){
$base = $this->config->get($base);
}else{
return $defaultValue;
}
$value = $this->config->getNested($variable);
while(count($vars) > 0){
$baseKey = array_shift($vars);
if(is_array($base) and isset($base[$baseKey])){
$base = $base[$baseKey];
}else{
return $defaultValue;
}
}
return $base;
return $value === null ? $defaultValue : $value;
}
/**

View File

@ -233,6 +233,48 @@ class Config{
$this->remove($k);
}
public function setNested($key, $value){
$vars = explode(".", $key);
$base = array_shift($vars);
if(!isset($this->config[$base])){
$this->config[$base] = [];
}
$base =& $this->config[$base];
while(count($vars) > 0){
$baseKey = array_shift($vars);
if(!isset($base[$baseKey])){
$base[$baseKey] = [];
}
$base =& $base[$baseKey];
}
$base = $value;
}
public function getNested($key){
$vars = explode(".", $key);
$base = array_shift($vars);
if(isset($this->config[$base])){
$base = $this->config[$base];
}else{
return null;
}
while(count($vars) > 0){
$baseKey = array_shift($vars);
if(is_array($base) and isset($base[$baseKey])){
$base = $base[$baseKey];
}else{
return null;
}
}
return $base;
}
/**
* @param $k
*