GenisysPro  for Minecraft PE/Windows 10 v1.1.x
Feature-rich server software for Minecraft PE and Windows 10 Edition
Config Class Reference

Public Member Functions

 __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)
 

Static Public Member Functions

static fixYAMLIndexes ($str)
 

Data Fields

const DETECT = -1
 
const PROPERTIES = 0
 
const CNF = Config::PROPERTIES
 
const JSON = 1
 
const YAML = 2
 
const SERIALIZED = 4
 
const ENUM = 5
 
const ENUMERATION = Config::ENUM
 

Static Public Attributes

static $formats
 
static cnf
 

Detailed Description

Config Class for simple config manipulation of multiple formats.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $file,
  $type = Config::DETECT,
  $default = [],
$correct = null 
)
Parameters
string$filePath of the file to be loaded
int$typeConfig type to load, -1 by default (detect)
array$defaultArray with the default values that will be written to the file if it did not exist
null&$correctSets correct to true if everything has been loaded correctly
78  {
79  $this->load($file, $type, $default);
80  $correct = $this->correct;
81  }

Member Function Documentation

◆ __get()

__get (   $k)
Parameters
$k
Returns
bool|mixed
227  {
228  return $this->get($k);
229  }

◆ __isset()

__isset (   $k)
Parameters
$k
Returns
bool
244  {
245  return $this->exists($k);
246  }

◆ __set()

__set (   $k,
  $v 
)
Parameters
$k
$v
235  {
236  $this->set($k, $v);
237  }

◆ __unset()

__unset (   $k)
Parameters
$k
251  {
252  $this->remove($k);
253  }

◆ check()

check ( )
Returns
bool
171  {
172  return $this->correct === true;
173  }

◆ exists()

exists (   $k,
  $lowercase = false 
)
Parameters
$k
bool$lowercaseIf set, searches Config in single-case / lowercase.
Returns
bool
348  {
349  if($lowercase === true){
350  $k = strtolower($k); //Convert requested key to lower
351  $array = array_change_key_case($this->config, CASE_LOWER); //Change all keys in array to lower
352  return isset($array[$k]); //Find $k in modified array
353  }else{
354  return isset($this->config[$k]);
355  }
356  }

◆ fixYAMLIndexes()

static fixYAMLIndexes (   $str)
static
Parameters
$str
Returns
mixed
98  {
99  return preg_replace("#^([ ]*)([a-zA-Z_]{1}[ ]*)\\:$#m", "$1\"$2\":", $str);
100  }

◆ get()

get (   $k,
  $default = false 
)
Parameters
$k
mixed$default
Returns
bool|mixed
318  {
319  return ($this->correct and isset($this->config[$k])) ? $this->config[$k] : $default;
320  }

◆ getAll()

getAll (   $keys = false)
Parameters
bool$keys
Returns
array
370  {
371  return ($keys === true ? array_keys($this->config) : $this->config);
372  }

◆ getNested()

getNested (   $key,
  $default = null 
)
Parameters
$key
mixed$default
Returns
mixed
287  {
288  if(isset($this->nestedCache[$key])){
289  return $this->nestedCache[$key];
290  }
291 
292  $vars = explode(".", $key);
293  $base = array_shift($vars);
294  if(isset($this->config[$base])){
295  $base = $this->config[$base];
296  }else{
297  return $default;
298  }
299 
300  while(count($vars) > 0){
301  $baseKey = array_shift($vars);
302  if(is_array($base) and isset($base[$baseKey])){
303  $base = $base[$baseKey];
304  }else{
305  return $default;
306  }
307  }
308 
309  return $this->nestedCache[$key] = $base;
310  }

◆ load()

load (   $file,
  $type = Config::DETECT,
  $default = [] 
)
Parameters
$file
int$type
array$default
Returns
bool
109  {
110  $this->correct = true;
111  $this->type = (int) $type;
112  $this->file = $file;
113  if(!is_array($default)){
114  $default = [];
115  }
116  if(!file_exists($file)){
117  $this->config = $default;
118  $this->save();
119  }else{
120  if($this->type === Config::DETECT){
121  $extension = explode(".", basename($this->file));
122  $extension = strtolower(trim(array_pop($extension)));
123  if(isset(Config::$formats[$extension])){
124  $this->type = Config::$formats[$extension];
125  }else{
126  $this->correct = false;
127  }
128  }
129  if($this->correct === true){
130  $content = file_get_contents($this->file);
131  switch($this->type){
132  case Config::PROPERTIES:
133  case Config::CNF:
134  $this->parseProperties($content);
135  break;
136  case Config::JSON:
137  $this->config = json_decode($content, true);
138  break;
139  case Config::YAML:
140  $content = self::fixYAMLIndexes($content);
141  $this->config = yaml_parse($content);
142  break;
143  case Config::SERIALIZED:
144  $this->config = unserialize($content);
145  break;
146  case Config::ENUM:
147  $this->parseList($content);
148  break;
149  default:
150  $this->correct = false;
151 
152  return false;
153  }
154  if(!is_array($this->config)){
155  $this->config = $default;
156  }
157  if($this->fillDefaults($default, $this->config) > 0){
158  $this->save();
159  }
160  }else{
161  return false;
162  }
163  }
164 
165  return true;
166  }

◆ reload()

reload ( )

Removes all the changes in memory and loads the file again

86  {
87  $this->config = [];
88  $this->nestedCache = [];
89  $this->correct = false;
90  $this->load($this->file, $this->type);
91  }

◆ remove()

remove (   $k)
Parameters
$k
361  {
362  unset($this->config[$k]);
363  }

◆ save()

save (   $async = false)
Parameters
bool$async
Returns
bool
180  {
181  if($this->correct === true){
182  try{
183  $content = null;
184  switch($this->type){
185  case Config::PROPERTIES:
186  case Config::CNF:
187  $content = $this->writeProperties();
188  break;
189  case Config::JSON:
190  $content = json_encode($this->config, JSON_PRETTY_PRINT | JSON_BIGINT_AS_STRING | JSON_UNESCAPED_UNICODE);
191  break;
192  case Config::YAML:
193  $content = yaml_emit($this->config, YAML_UTF8_ENCODING);
194  break;
195  case Config::SERIALIZED:
196  $content = serialize($this->config);
197  break;
198  case Config::ENUM:
199  $content = implode("\r\n", array_keys($this->config));
200  break;
201  }
202 
203  if($async){
204  Server::getInstance()->getScheduler()->scheduleAsyncTask(new FileWriteTask($this->file, $content));
205  }else{
206  file_put_contents($this->file, $content);
207  }
208  }catch(\Throwable $e){
209  $logger = Server::getInstance()->getLogger();
210  $logger->critical("Could not save Config " . $this->file . ": " . $e->getMessage());
211  if(\pocketmine\DEBUG > 1 and $logger instanceof MainLogger){
212  $logger->logException($e);
213  }
214  }
215 
216  return true;
217  }else{
218  return false;
219  }
220  }

◆ set()

set (   $k,
  $v = true 
)
Parameters
string$kkey to be set
mixed$vvalue to set key
326  {
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]);
331  }
332  }
333  }

◆ setAll()

setAll (   $v)
Parameters
array$v
338  {
339  $this->config = $v;
340  }

◆ setDefaults()

setDefaults ( array  $defaults)
Parameters
array$defaults
377  {
378  $this->fillDefaults($defaults, $this->config);
379  }

◆ setNested()

setNested (   $key,
  $value 
)
Parameters
$key
$value
259  {
260  $vars = explode(".", $key);
261  $base = array_shift($vars);
262 
263  if(!isset($this->config[$base])){
264  $this->config[$base] = [];
265  }
266 
267  $base =& $this->config[$base];
268 
269  while(count($vars) > 0){
270  $baseKey = array_shift($vars);
271  if(!isset($base[$baseKey])){
272  $base[$baseKey] = [];
273  }
274  $base =& $base[$baseKey];
275  }
276 
277  $base = $value;
278  $this->nestedCache[$key] = $value;
279  }

Field Documentation

◆ $formats

$formats
static
Initial value:
= [
"properties" => Config::PROPERTIES

◆ CNF

const CNF = Config::PROPERTIES

◆ cnf

cnf
static
Initial value:
"conf" => Config::CNF,
"config" => Config::CNF,
"json" => Config::JSON,
"js" => Config::JSON,
"yml" => Config::YAML,
"yaml" => Config::YAML,
"serialize" => Config::SERIALIZED,
"txt" => Config::ENUM,
"list" => Config::ENUM,
"enum" => Config::ENUM,
]

◆ DETECT

const DETECT = -1

◆ ENUM

const ENUM = 5

◆ ENUMERATION

const ENUMERATION = Config::ENUM

◆ JSON

const JSON = 1

◆ PROPERTIES

const PROPERTIES = 0

◆ SERIALIZED

const SERIALIZED = 4

◆ YAML

const YAML = 2

The documentation for this class was generated from the following file: