mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-19 04:15:04 +00:00
Config: improve config loading and parsing error handling
closes #4654 closes #3454
This commit is contained in:
@@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\utils;
|
||||
|
||||
use pocketmine\errorhandler\ErrorToExceptionHandler;
|
||||
use Webmozart\PathUtil\Path;
|
||||
use function array_change_key_case;
|
||||
use function array_fill_keys;
|
||||
@@ -33,6 +34,7 @@ use function date;
|
||||
use function explode;
|
||||
use function file_exists;
|
||||
use function file_get_contents;
|
||||
use function get_debug_type;
|
||||
use function implode;
|
||||
use function is_array;
|
||||
use function is_bool;
|
||||
@@ -167,20 +169,31 @@ class Config{
|
||||
if($content === false){
|
||||
throw new \RuntimeException("Unable to load config file");
|
||||
}
|
||||
$config = null;
|
||||
switch($this->type){
|
||||
case Config::PROPERTIES:
|
||||
$config = self::parseProperties($content);
|
||||
break;
|
||||
case Config::JSON:
|
||||
$config = json_decode($content, true);
|
||||
try{
|
||||
$config = json_decode($content, true, flags: JSON_THROW_ON_ERROR);
|
||||
}catch(\JsonException $e){
|
||||
throw ConfigLoadException::wrap($this->file, $e);
|
||||
}
|
||||
break;
|
||||
case Config::YAML:
|
||||
$content = self::fixYAMLIndexes($content);
|
||||
$config = yaml_parse($content);
|
||||
try{
|
||||
$config = ErrorToExceptionHandler::trap(fn() => yaml_parse($content));
|
||||
}catch(\ErrorException $e){
|
||||
throw ConfigLoadException::wrap($this->file, $e);
|
||||
}
|
||||
break;
|
||||
case Config::SERIALIZED:
|
||||
$config = unserialize($content);
|
||||
try{
|
||||
$config = ErrorToExceptionHandler::trap(fn() => unserialize($content));
|
||||
}catch(\ErrorException $e){
|
||||
throw ConfigLoadException::wrap($this->file, $e);
|
||||
}
|
||||
break;
|
||||
case Config::ENUM:
|
||||
$config = array_fill_keys(self::parseList($content), true);
|
||||
@@ -188,7 +201,10 @@ class Config{
|
||||
default:
|
||||
throw new \InvalidArgumentException("Invalid config type specified");
|
||||
}
|
||||
$this->config = is_array($config) ? $config : $default;
|
||||
if(!is_array($config)){
|
||||
throw new ConfigLoadException("Failed to load config $this->file: Expected array for base type, but got " . get_debug_type($config));
|
||||
}
|
||||
$this->config = $config;
|
||||
if($this->fillDefaults($default, $this->config) > 0){
|
||||
$this->save();
|
||||
}
|
||||
|
31
src/utils/ConfigLoadException.php
Normal file
31
src/utils/ConfigLoadException.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\utils;
|
||||
|
||||
final class ConfigLoadException extends \RuntimeException{
|
||||
|
||||
public static function wrap(string $fileName, \Exception $e) : self{
|
||||
return new self("Failed to parse config $fileName: " . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user