1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-27 15:00:11 +02:00

feat(core): add Config API #432

This commit is contained in:
Awilum
2020-05-23 20:26:29 +03:00
parent 15ca7fd612
commit 63fb3d13fa

View File

@@ -0,0 +1,188 @@
<?php
declare(strict_types=1);
/**
* Flextype (http://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Component\Filesystem\Filesystem;
use Flextype\Component\Arr\Arr;
class Config
{
/**
* Flextype Dependency Container
*/
private $flextype;
/**
* Constructor
*
* @access public
*/
public function __construct($flextype)
{
$this->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;
}
}