changing Config to silently ignore read errors by default

The old behavior is still available by setting public property $strict to true
This commit is contained in:
Joby 2020-06-18 10:34:44 -06:00
parent 1f1980ba23
commit d311c5b1c8

View file

@ -2,12 +2,13 @@
/* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */ /* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */
namespace Flatrr\Config; namespace Flatrr\Config;
use Symfony\Component\Yaml\Yaml;
use Flatrr\SelfReferencingFlatArray; use Flatrr\SelfReferencingFlatArray;
use Flatrr\FlatArray; use Symfony\Component\Yaml\Yaml;
class Config extends SelfReferencingFlatArray implements ConfigInterface class Config extends SelfReferencingFlatArray implements ConfigInterface
{ {
public $strict = false;
public function readDir($dir, string $name = null, bool $overwrite = null) public function readDir($dir, string $name = null, bool $overwrite = null)
{ {
$dir = realpath($dir); $dir = realpath($dir);
@ -25,11 +26,15 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface
} }
} }
protected function parse(string $input, string $format) : array protected function parse(string $input, string $format): array
{ {
$fn = 'parse_'.$format; $fn = 'parse_' . $format;
if (!method_exists($this, $fn)) { if (!method_exists($this, $fn)) {
throw new \Exception("Don't know how to parse the format \"$format\""); if ($this->strict) {
throw new \Exception("Don't know how to parse the format \"$format\"");
} else {
return null;
}
} }
if ($out = $this->$fn($input)) { if ($out = $this->$fn($input)) {
return $out; return $out;
@ -42,12 +47,12 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface
return Yaml::parse($input); return Yaml::parse($input);
} }
public function json($raw = false) : string public function json($raw = false): string
{ {
return json_encode($this->get(null, $raw), JSON_PRETTY_PRINT); return json_encode($this->get(null, $raw), JSON_PRETTY_PRINT);
} }
public function yaml($raw = false) : string public function yaml($raw = false): string
{ {
return Yaml::dump($this->get(null, $raw), 10); return Yaml::dump($this->get(null, $raw), 10);
} }
@ -75,16 +80,28 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface
public function readFile($filename, string $name = null, bool $overwrite = false) public function readFile($filename, string $name = null, bool $overwrite = false)
{ {
if (!is_file($filename) || !is_readable($filename)) { if (!is_file($filename) || !is_readable($filename)) {
throw new \Exception("Couldn't read config file \"$filename\""); if ($this->strict) {
throw new \Exception("Couldn't read config file \"$filename\"");
} else {
return null;
}
} }
$format = strtolower(preg_replace('/.+\./', '', $filename)); $format = strtolower(preg_replace('/.+\./', '', $filename));
$fn = 'read_'.$format; $fn = 'read_' . $format;
if (!method_exists($this, $fn)) { if (!method_exists($this, $fn)) {
throw new \Exception("Don't know how to read the format \"$format\""); if ($this->strict) {
throw new \Exception("Don't know how to read the format \"$format\"");
} else {
return null;
}
} }
$data = $this->$fn($filename); $data = $this->$fn($filename);
if (!$data) { if (!$data) {
throw new \Exception("Error reading \"".$filename."\""); if ($this->strict) {
throw new \Exception("Error reading \"" . $filename . "\"");
} else {
return null;
}
} }
$this->merge($data, $name, $overwrite); $this->merge($data, $name, $overwrite);
} }