From 63fb3d13fa31ef03c8b2e8f0cc3e7aa9822624ac Mon Sep 17 00:00:00 2001 From: Awilum Date: Sat, 23 May 2020 20:26:29 +0300 Subject: [PATCH] feat(core): add Config API #432 --- src/flextype/core/Config/Config.php | 188 ++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 src/flextype/core/Config/Config.php diff --git a/src/flextype/core/Config/Config.php b/src/flextype/core/Config/Config.php new file mode 100644 index 00000000..71286602 --- /dev/null +++ b/src/flextype/core/Config/Config.php @@ -0,0 +1,188 @@ +flextype = $flextype; + } + + /** + * Get key value from the config + * + * @param string $config Config namespace. + * @param string $key The key of the config item to get. + * @param mixed $default Default value + * + * @return array + */ + public function get(string $config, $key, $default = null) : array + { + $config_file = $this->getFileLocation($config); + + if (Filesystem::has($config_file)) { + return Arr::get($this->flextype->serializer->decode(Filesystem::read($config_file), 'yaml'), $key, $default); + } + + return []; + } + + /** + * Create new config key + * + * @param string $config Config namespace. + * @param string $key The key of the config item to get. + * @param mixed $value Value + * + * @return bool + */ + public function create(string $config, $key, $value) : bool + { + $config_file = $this->getFileLocation($config); + + if (Filesystem::has($config_file)) { + + $config_file_data = $this->flextype->serializer->decode(Filesystem::read($config_file), 'yaml'); + + if (!Arr::keyExists($config_file_data, $key)) { + Arr::set($config_file_data, $key, $value); + return Filesystem::write($config_file, $this->flextype->serializer->encode($config_file_data, 'yaml')); + } + + return false; + } + + return false; + } + + /** + * Update config key + * + * @param string $config Config namespace. + * @param string $key The key of the config item to get. + * @param mixed $value Value + * + * @return bool + */ + public function update(string $config, $key, $value) : bool + { + $config_file = $this->getFileLocation($config); + + if (Filesystem::has($config_file)) { + + $config_file_data = $this->flextype->serializer->decode(Filesystem::read($config_file), 'yaml'); + + if (Arr::keyExists($config_file_data, $key)) { + Arr::set($config_file_data, $key, $value); + return Filesystem::write($config_file, $this->flextype->serializer->encode($config_file_data, 'yaml')); + } + + return false; + } + + return false; + } + + /** + * Delete config key + * + * @param string $config Config namespace. + * @param string $key The key of the config item to get. + * + * @return bool + */ + public function delete(string $config, $key) : bool + { + $config_file = $this->getFileLocation($config); + + if (Filesystem::has($config_file)) { + + $config_file_data = $this->flextype->serializer->decode(Filesystem::read($config_file), 'yaml'); + + if (Arr::keyExists($config_file_data, $key)) { + Arr::delete($config_file_data, $key); + return Filesystem::write($config_file, $this->flextype->serializer->encode($config_file_data, 'yaml')); + } + + return false; + } + + return false; + } + + /** + * Checks if an config key with this key name is in the config. + * + * @param string $config Config namespace. + * @param string $key The key of the config item to get. + * + * @return bool + */ + public function has(string $config, $key) : bool + { + $config_file = $this->getFileLocation($config); + + if (Filesystem::has($config_file)) { + + $config_file_data = $this->flextype->serializer->decode(Filesystem::read($config_file), 'yaml'); + + if (Arr::keyExists($config_file_data, $key)) { + return true; + } + + return false; + } + + return false; + } + + /** + * Get config file location + * + * @param string $id Unique identifier of the entry(entries). + * + * @return string entry file location + * + * @access private + */ + public function getFileLocation(string $config) : string + { + return PATH['project'] . '/config/' . $config . '/settings.yaml'; + } + + /** + * Get config directory location + * + * @param string $id Unique identifier of the entry(entries). + * + * @return string entry directory location + * + * @access private + */ + public function getDirLocation(string $config) : string + { + return PATH['project'] . '/config/' . $config; + } +}