mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 08:17:34 +00:00
Added Config->getNested(key) and Config->setNested(key, value)
This commit is contained in:
parent
747f7685e7
commit
964bf98ca6
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user