From d311c5b1c8b70112ec8a930b6616920c2c89df4c Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Thu, 18 Jun 2020 10:34:44 -0600 Subject: [PATCH] changing Config to silently ignore read errors by default The old behavior is still available by setting public property $strict to true --- src/Config/Config.php | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Config/Config.php b/src/Config/Config.php index 2928a7c..a7c50b9 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -2,12 +2,13 @@ /* Flatrr | https://gitlab.com/byjoby/flatrr | MIT License */ namespace Flatrr\Config; -use Symfony\Component\Yaml\Yaml; use Flatrr\SelfReferencingFlatArray; -use Flatrr\FlatArray; +use Symfony\Component\Yaml\Yaml; class Config extends SelfReferencingFlatArray implements ConfigInterface { + public $strict = false; + public function readDir($dir, string $name = null, bool $overwrite = null) { $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)) { - 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)) { return $out; @@ -42,12 +47,12 @@ class Config extends SelfReferencingFlatArray implements ConfigInterface 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); } - public function yaml($raw = false) : string + public function yaml($raw = false): string { 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) { 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)); - $fn = 'read_'.$format; + $fn = 'read_' . $format; 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); 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); }