From 43c0d3cacc578b911f24fa083de046f17dfacac3 Mon Sep 17 00:00:00 2001 From: Andrii Vasyliev Date: Wed, 12 Apr 2017 15:29:07 +0200 Subject: [PATCH] refactored out readers and ReaderFactory --- src/Builder.php | 87 +++------------------------------- src/ReaderFactory.php | 55 +++++++++++++++++++++ src/readers/AbstractReader.php | 54 +++++++++++++++++++++ src/readers/EnvReader.php | 35 ++++++++++++++ src/readers/JsonReader.php | 27 +++++++++++ src/readers/PhpReader.php | 29 ++++++++++++ src/readers/YamlReader.php | 30 ++++++++++++ 7 files changed, 237 insertions(+), 80 deletions(-) create mode 100644 src/ReaderFactory.php create mode 100644 src/readers/AbstractReader.php create mode 100644 src/readers/EnvReader.php create mode 100644 src/readers/JsonReader.php create mode 100644 src/readers/PhpReader.php create mode 100644 src/readers/YamlReader.php diff --git a/src/Builder.php b/src/Builder.php index 999f32d..4f9cfa0 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -271,87 +271,9 @@ public function loadConfig($name) */ public function loadFile($path) { - $skippable = strncmp($path, '?', 1) === 0 ? '?' : ''; - if ($skippable) { - $path = substr($path, 1); - } - - if (file_exists($path)) { - $res = $this->readFile($path); - - return is_array($res) ? $res : []; - } - - if (empty($skippable)) { - throw new FailedReadException("failed read file: $path"); - } - - return []; - } - - public function readFile($path) - { - $ext = pathinfo($path)['extension']; - if ($ext === 'env') { - return $this->readEnvFile($path); - } elseif ($ext === 'php') { - return $this->readPhpFile($path); - } elseif ($ext === 'json') { - return $this->readJsonFile($path); - } elseif ($ext === 'yml' || $ext === 'yaml') { - return $this->readYamlFile($path); - } - - throw new UnsupportedFileTypeException("unsupported extension: $ext"); - } - - public function readEnvFile($path) - { - if (!class_exists('Dotenv\Dotenv')) { - throw new UnsupportedFileTypeException("for .env support require `vlucas/phpdotenv` in your composer.json"); - } - $info = pathinfo($path); - $dotenv = new \Dotenv\Dotenv($info['dirname'], $info['basename']); - $oldenvs = $_ENV; - $dotenv->load(); - $newenvs = $_ENV; - - return array_diff_assoc($newenvs, $oldenvs); - } - - public function readPhpFile($__path) - { - if (!is_readable($__path)) { - throw new FailedReadException("failed read file: $__path"); - } - /// Expose variables to be used in configs - extract($this->vars); - - return require $__path; - } - - public function readJsonFile($path) - { - return json_decode($this->getFileContents($path), true); - } - - public function readYamlFile($path) - { - if (!class_exists('Symfony\Component\Yaml\Yaml')) { - throw new UnsupportedFileTypeException("for YAML support require `symfony/yaml` in your composer.json"); - } - - return \Symfony\Component\Yaml\Yaml::parse($this->getFileContents($path)); - } - - public function getFileContents($path) - { - $res = file_get_contents($path); - if ($res === FALSE) { - throw new FailedReadException("failed read file: $path"); - } + $reader = ReaderFactory::get($path); - return $res; + return $reader->read($path, $this); } public function setIo(IOInterface $io) @@ -367,4 +289,9 @@ protected function writeError($text) echo $text . "\n"; } } + + public function getVars() + { + return $this->vars; + } } diff --git a/src/ReaderFactory.php b/src/ReaderFactory.php new file mode 100644 index 0000000..7e53c18 --- /dev/null +++ b/src/ReaderFactory.php @@ -0,0 +1,55 @@ + + */ +class ReaderFactory +{ + private static $loaders; + + private static $knownReaders = [ + 'env' => EnvReader::class, + 'php' => PhpReader::class, + 'json' => JsonReader::class, + 'yaml' => YamlReader::class, + 'yml' => YamlReader::class, + ]; + + public static function get($path) + { + $ext = pathinfo($path, PATHINFO_EXTENSION); + if (empty(static::$loaders[$ext])) { + static::$loaders[$ext] = static::create($ext); + } + + return static::$loaders[$ext]; + } + + public static function create($ext) + { + if (empty(static::$knownReaders[$ext])) { + throw new UnsupportedFileTypeException("unsupported extension: $ext"); + } + $class = static::$knownReaders[$ext]; + + return new $class(); + } +} diff --git a/src/readers/AbstractReader.php b/src/readers/AbstractReader.php new file mode 100644 index 0000000..90545ec --- /dev/null +++ b/src/readers/AbstractReader.php @@ -0,0 +1,54 @@ + + */ +abstract class AbstractReader +{ + public function read($path, Builder $builder) + { + $skippable = strncmp($path, '?', 1) === 0 ? '?' : ''; + if ($skippable) { + $path = substr($path, 1); + } + + if (is_readable($path)) { + $res = $this->readRaw($path, $builder); + + return is_array($res) ? $res : []; + } + + if (empty($skippable)) { + throw new FailedReadException("failed read file: $path"); + } + + return []; + } + + public function getFileContents($path) + { + $res = file_get_contents($path); + if ($res === FALSE) { + throw new FailedReadException("failed read file: $path"); + } + + return $res; + } + + abstract public function readRaw($path, Builder $builder); +} diff --git a/src/readers/EnvReader.php b/src/readers/EnvReader.php new file mode 100644 index 0000000..acba2b1 --- /dev/null +++ b/src/readers/EnvReader.php @@ -0,0 +1,35 @@ + + */ +class EnvReader extends AbstractReader +{ + public function readRaw($path, Builder $builder) + { + if (!class_exists('Dotenv\Dotenv')) { + throw new UnsupportedFileTypeException("for .env support require `vlucas/phpdotenv` in your composer.json"); + } + $info = pathinfo($path); + $dotenv = new \Dotenv\Dotenv($info['dirname'], $info['basename']); + $oldenvs = $_ENV; + $dotenv->load(); + $newenvs = $_ENV; + + return array_diff_assoc($newenvs, $oldenvs); + } +} diff --git a/src/readers/JsonReader.php b/src/readers/JsonReader.php new file mode 100644 index 0000000..1f684ad --- /dev/null +++ b/src/readers/JsonReader.php @@ -0,0 +1,27 @@ + + */ +class JsonReader extends AbstractReader +{ + public function readRaw($path, $builder) + { + return json_decode($this->getFileContents($path), true); + } +} diff --git a/src/readers/PhpReader.php b/src/readers/PhpReader.php new file mode 100644 index 0000000..c14d73a --- /dev/null +++ b/src/readers/PhpReader.php @@ -0,0 +1,29 @@ + + */ +class PhpReader extends AbstractReader +{ + public function readRaw($__path, Builder $builder) + { + /// Expose variables to be used in configs + extract($builder->getVars()); + + return require $__path; + } +} diff --git a/src/readers/YamlReader.php b/src/readers/YamlReader.php new file mode 100644 index 0000000..42d66ea --- /dev/null +++ b/src/readers/YamlReader.php @@ -0,0 +1,30 @@ + + */ +class YamlReader extends AbstractReader +{ + public function readRaw($path, Builder $builder) + { + if (!class_exists('Symfony\Component\Yaml\Yaml')) { + throw new UnsupportedFileTypeException("for YAML support require `symfony/yaml` in your composer.json"); + } + + return \Symfony\Component\Yaml\Yaml::parse($this->getFileContents($path)); + } +}