Skip to content

Commit

Permalink
update LibConfig.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Amitminer committed Jun 26, 2023
1 parent d3f8add commit e5dde38
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion lib/LibConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,63 @@
use Symfony\Component\Yaml\Yaml;

class LibConfig {

/**
* The path to the configuration file.
*
* @var string
*/
protected $file;

/**
* The configuration data.
*
* @var array
*/
protected $data;

/**
* LibConfig constructor.
*
* @param string $file The path to the configuration file.
*/
public function __construct($file)
{
$this->file = $file;
$this->data = $this->read();
}

/**
* Get a value from the configuration data.
*
* @param string $key The key to retrieve.
* @param mixed $default The default value to return if the key does not exist.
*
* @return mixed The value associated with the key, or the default value if the key does not exist.
*/
public function get($key, $default = null)
{
return $this->data[$key] ?? $default;
}

/**
* Set a value in the configuration data.
*
* @param string $key The key to set.
* @param mixed $value The value to set.
*
* @return void
*/
public function set($key, $value)
{
$this->data[$key] = $value;
$this->save();
}

/**
* Read the configuration file.
*
* @return array The parsed configuration data.
*/
protected function read()
{
if (file_exists($this->file)) {
Expand All @@ -36,6 +72,11 @@ protected function read()
return [];
}

/**
* Save the configuration data to the file.
*
* @return void
*/
public function save()
{
$content = Yaml::dump($this->data);
Expand Down

0 comments on commit e5dde38

Please sign in to comment.