From ae612b913ebdf6fad65dfe270c20300720265a64 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Tue, 27 Jun 2017 16:44:05 +0100 Subject: [PATCH] Fixed config type detection, fixed configs being saved empty Fixes LegendOfMCPE/EssentialsPE#354 When the config file didn't exist, no type detection was performed. This resulted in the case statement for Config->save() falling through and not writing anything to the file. --- src/pocketmine/utils/Config.php | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/pocketmine/utils/Config.php b/src/pocketmine/utils/Config.php index 0430e6c61..153554583 100644 --- a/src/pocketmine/utils/Config.php +++ b/src/pocketmine/utils/Config.php @@ -110,26 +110,25 @@ class Config{ * * @return bool */ - public function load($file, $type = Config::DETECT, $default = []){ + public function load(string $file, int $type = Config::DETECT, array $default = []){ $this->correct = true; - $this->type = (int) $type; $this->file = $file; - if(!is_array($default)){ - $default = []; + + $this->type = $type; + if($this->type === Config::DETECT){ + $extension = explode(".", basename($this->file)); + $extension = strtolower(trim(array_pop($extension))); + if(isset(Config::$formats[$extension])){ + $this->type = Config::$formats[$extension]; + }else{ + $this->correct = false; + } } + if(!file_exists($file)){ $this->config = $default; $this->save(); }else{ - if($this->type === Config::DETECT){ - $extension = explode(".", basename($this->file)); - $extension = strtolower(trim(array_pop($extension))); - if(isset(Config::$formats[$extension])){ - $this->type = Config::$formats[$extension]; - }else{ - $this->correct = false; - } - } if($this->correct === true){ $content = file_get_contents($this->file); switch($this->type){ @@ -202,6 +201,8 @@ class Config{ case Config::ENUM: $content = implode("\r\n", array_keys($this->config)); break; + default: + throw new \InvalidStateException("Config type is unknown, has not been set or not detected"); } if($async){