Enable parsing/emitting .properties without creating a Config object

this is useful when the contents are just going to get passed straight into a model, making Config object redundant anyway.
This commit is contained in:
Dylan K. Taylor 2021-10-07 20:53:15 +01:00
parent 5115387feb
commit 2a3a57c519
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 17 additions and 10 deletions

View File

@ -170,7 +170,7 @@ class Config{
$config = null; $config = null;
switch($this->type){ switch($this->type){
case Config::PROPERTIES: case Config::PROPERTIES:
$config = $this->parseProperties($content); $config = self::parseProperties($content);
break; break;
case Config::JSON: case Config::JSON:
$config = json_decode($content, true); $config = json_decode($content, true);
@ -211,7 +211,7 @@ class Config{
$content = null; $content = null;
switch($this->type){ switch($this->type){
case Config::PROPERTIES: case Config::PROPERTIES:
$content = $this->writeProperties(); $content = self::writeProperties($this->config);
break; break;
case Config::JSON: case Config::JSON:
$content = json_encode($this->config, $this->jsonOptions); $content = json_encode($this->config, $this->jsonOptions);
@ -524,9 +524,13 @@ class Config{
return $result; return $result;
} }
private function writeProperties() : string{ /**
* @param string[]|int[]|float[]|bool[] $config
* @phpstan-param array<string, string|int|float|bool> $config
*/
public static function writeProperties(array $config) : string{
$content = "#Properties Config file\r\n#" . date("D M j H:i:s T Y") . "\r\n"; $content = "#Properties Config file\r\n#" . date("D M j H:i:s T Y") . "\r\n";
foreach($this->config as $k => $v){ foreach($config as $k => $v){
if(is_bool($v)){ if(is_bool($v)){
$v = $v ? "on" : "off"; $v = $v ? "on" : "off";
} }
@ -537,9 +541,10 @@ class Config{
} }
/** /**
* @return mixed[] * @return string[]|int[]|float[]|bool[]
* @phpstan-return array<string, string|int|float|bool>
*/ */
private function parseProperties(string $content) : array{ public static function parseProperties(string $content) : array{
$result = []; $result = [];
if(preg_match_all('/^\s*([a-zA-Z0-9\-_\.]+)[ \t]*=([^\r\n]*)/um', $content, $matches) > 0){ //false or 0 matches if(preg_match_all('/^\s*([a-zA-Z0-9\-_\.]+)[ \t]*=([^\r\n]*)/um', $content, $matches) > 0){ //false or 0 matches
foreach($matches[1] as $i => $k){ foreach($matches[1] as $i => $k){
@ -563,10 +568,7 @@ class Config{
}; };
break; break;
} }
if(isset($result[$k])){ $result[(string) $k] = $v;
\GlobalLogger::get()->debug("[Config] Repeated property " . $k . " on file " . $this->file);
}
$result[$k] = $v;
} }
} }

View File

@ -165,6 +165,11 @@ parameters:
count: 1 count: 1
path: ../../../src/timings/TimingsHandler.php path: ../../../src/timings/TimingsHandler.php
-
message: "#^Parameter \\#1 \\$config of static method pocketmine\\\\utils\\\\Config\\:\\:writeProperties\\(\\) expects array\\<string, bool\\|float\\|int\\|string\\>, array\\<string, mixed\\> given\\.$#"
count: 1
path: ../../../src/utils/Config.php
- -
message: "#^Parameter \\#2 \\$offset of function substr expects int, mixed given\\.$#" message: "#^Parameter \\#2 \\$offset of function substr expects int, mixed given\\.$#"
count: 1 count: 1