|
| __construct ($file, $type=Config::DETECT, $default=[], &$correct=null) |
|
| reload () |
|
| load ($file, $type=Config::DETECT, $default=[]) |
|
| check () |
|
| save ($async=false) |
|
| __get ($k) |
|
| __set ($k, $v) |
|
| __isset ($k) |
|
| __unset ($k) |
|
| setNested ($key, $value) |
|
| getNested ($key, $default=null) |
|
| get ($k, $default=false) |
|
| set ($k, $v=true) |
|
| setAll ($v) |
|
| exists ($k, $lowercase=false) |
|
| remove ($k) |
|
| getAll ($keys=false) |
|
| setDefaults (array $defaults) |
|
Config Class for simple config manipulation of multiple formats.
◆ __construct()
__construct |
( |
|
$file, |
|
|
|
$type = Config::DETECT , |
|
|
|
$default = [] , |
|
|
& |
$correct = null |
|
) |
| |
- Parameters
-
string | $file | Path of the file to be loaded |
int | $type | Config type to load, -1 by default (detect) |
array | $default | Array with the default values that will be written to the file if it did not exist |
null | &$correct | Sets correct to true if everything has been loaded correctly |
79 $this->
load($file, $type, $default);
80 $correct = $this->correct;
◆ __get()
- Parameters
-
- Returns
- bool|mixed
228 return $this->
get($k);
◆ __isset()
◆ __set()
◆ __unset()
◆ check()
- Returns
- bool
172 return $this->correct ===
true;
◆ exists()
exists |
( |
|
$k, |
|
|
|
$lowercase = false |
|
) |
| |
- Parameters
-
| $k | |
bool | $lowercase | If set, searches Config in single-case / lowercase. |
- Returns
- bool
349 if($lowercase ===
true){
351 $array = array_change_key_case($this->config, CASE_LOWER);
352 return isset($array[$k]);
354 return isset($this->config[$k]);
◆ fixYAMLIndexes()
static fixYAMLIndexes |
( |
|
$str | ) |
|
|
static |
- Parameters
-
- Returns
- mixed
99 return preg_replace(
"#^([ ]*)([a-zA-Z_]{1}[ ]*)\\:$#m",
"$1\"$2\":", $str);
◆ get()
get |
( |
|
$k, |
|
|
|
$default = false |
|
) |
| |
- Parameters
-
- Returns
- bool|mixed
319 return ($this->correct and isset($this->config[$k])) ? $this->config[$k] : $default;
◆ getAll()
- Parameters
-
- Returns
- array
371 return ($keys ===
true ? array_keys($this->config) : $this->config);
◆ getNested()
getNested |
( |
|
$key, |
|
|
|
$default = null |
|
) |
| |
- Parameters
-
- Returns
- mixed
288 if(isset($this->nestedCache[$key])){
289 return $this->nestedCache[$key];
292 $vars = explode(
".", $key);
293 $base = array_shift($vars);
294 if(isset($this->config[$base])){
295 $base = $this->config[$base];
300 while(count($vars) > 0){
301 $baseKey = array_shift($vars);
302 if(is_array($base) and isset($base[$baseKey])){
303 $base = $base[$baseKey];
309 return $this->nestedCache[$key] = $base;
◆ load()
- Parameters
-
| $file | |
int | $type | |
array | $default | |
- Returns
- bool
110 $this->correct =
true;
111 $this->type = (int) $type;
113 if(!is_array($default)){
116 if(!file_exists($file)){
117 $this->config = $default;
121 $extension = explode(
".", basename($this->file));
122 $extension = strtolower(trim(array_pop($extension)));
126 $this->correct =
false;
129 if($this->correct ===
true){
130 $content = file_get_contents($this->file);
134 $this->parseProperties($content);
137 $this->config = json_decode($content,
true);
140 $content = self::fixYAMLIndexes($content);
141 $this->config = yaml_parse($content);
144 $this->config = unserialize($content);
147 $this->parseList($content);
150 $this->correct =
false;
154 if(!is_array($this->config)){
155 $this->config = $default;
157 if($this->fillDefaults($default, $this->config) > 0){
◆ reload()
Removes all the changes in memory and loads the file again
88 $this->nestedCache = [];
89 $this->correct =
false;
90 $this->
load($this->file, $this->type);
◆ remove()
- Parameters
-
362 unset($this->config[$k]);
◆ save()
- Parameters
-
- Returns
- bool
181 if($this->correct ===
true){
187 $content = $this->writeProperties();
190 $content = json_encode($this->config, JSON_PRETTY_PRINT | JSON_BIGINT_AS_STRING | JSON_UNESCAPED_UNICODE);
193 $content = yaml_emit($this->config, YAML_UTF8_ENCODING);
196 $content = serialize($this->config);
199 $content = implode(
"\r\n", array_keys($this->config));
204 Server::getInstance()->getScheduler()->scheduleAsyncTask(
new FileWriteTask($this->file, $content));
206 file_put_contents($this->file, $content);
208 }
catch(\Throwable $e){
210 $logger->critical(
"Could not save Config " . $this->file .
": " . $e->getMessage());
◆ set()
- Parameters
-
string | $k | key to be set |
mixed | $v | value to set key |
327 $this->config[$k] = $v;
328 foreach($this->nestedCache as $nestedKey => $nvalue){
329 if(substr($nestedKey, 0, strlen($k) + 1) === ($k .
".")){
330 unset($this->nestedCache[$nestedKey]);
◆ setAll()
◆ setDefaults()
setDefaults |
( |
array |
$defaults | ) |
|
- Parameters
-
378 $this->fillDefaults($defaults, $this->config);
◆ setNested()
setNested |
( |
|
$key, |
|
|
|
$value |
|
) |
| |
- Parameters
-
260 $vars = explode(
".", $key);
261 $base = array_shift($vars);
263 if(!isset($this->config[$base])){
264 $this->config[$base] = [];
267 $base =& $this->config[$base];
269 while(count($vars) > 0){
270 $baseKey = array_shift($vars);
271 if(!isset($base[$baseKey])){
272 $base[$baseKey] = [];
274 $base =& $base[$baseKey];
278 $this->nestedCache[$key] = $value;
◆ $formats
◆ CNF
◆ cnf
◆ DETECT
◆ ENUM
◆ ENUMERATION
◆ JSON
◆ PROPERTIES
◆ SERIALIZED
◆ YAML
The documentation for this class was generated from the following file:
- src/pocketmine/utils/Config.php