62 lines
1.4 KiB
PHP
62 lines
1.4 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/functions.php';
|
|
|
|
readonly class config
|
|
{
|
|
private const CONFIG_BASE_PATH = '.config';
|
|
|
|
public function __construct(
|
|
private string $name
|
|
)
|
|
{
|
|
$this->init();
|
|
}
|
|
|
|
public function get(string $key, mixed $default = null): mixed
|
|
{
|
|
$config = include $this->getConfigFile();
|
|
return $config[$key] ?? $default;
|
|
}
|
|
|
|
public function set(string $key, mixed $value): void
|
|
{
|
|
$config = include $this->getConfigFile();
|
|
$config[$key] = $value;
|
|
$this->writeConfig($config);
|
|
}
|
|
|
|
private function getConfigFile(): string
|
|
{
|
|
return path(
|
|
$this->getConfigPath(),
|
|
'config.php'
|
|
);
|
|
}
|
|
|
|
private function getConfigPath(): string
|
|
{
|
|
return path(
|
|
getenv('HOME'),
|
|
self::CONFIG_BASE_PATH,
|
|
$this->name
|
|
);
|
|
}
|
|
|
|
private function init(): void
|
|
{
|
|
if (file_exists($this->getConfigFile())) {
|
|
return;
|
|
} else {
|
|
if (!is_dir($this->getConfigPath())) {
|
|
mkdir($this->getConfigPath(), 0755, true);
|
|
}
|
|
$this->writeConfig([]);
|
|
}
|
|
}
|
|
|
|
public function writeConfig(array $config): void
|
|
{
|
|
file_put_contents($this->getConfigFile(), '<?php return ' . var_export($config, true) . ';');
|
|
}
|
|
} |