73 lines
1.6 KiB
PHP
73 lines
1.6 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/functions.php';
|
|
|
|
readonly class homeConfig extends configReader
|
|
{
|
|
private const CONFIG_BASE_PATH = '.config';
|
|
|
|
public function __construct(
|
|
string $name
|
|
)
|
|
{
|
|
parent::__construct(path(
|
|
getenv('HOME'),
|
|
self::CONFIG_BASE_PATH,
|
|
$name
|
|
));
|
|
}
|
|
}
|
|
|
|
readonly class configReader
|
|
{
|
|
public function __construct(
|
|
private string $path,
|
|
private string $fileName = 'config.php',
|
|
)
|
|
{
|
|
$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(),
|
|
$this->fileName
|
|
);
|
|
}
|
|
|
|
private function getConfigPath(): string
|
|
{
|
|
return $this->path;
|
|
}
|
|
|
|
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) . ';');
|
|
}
|
|
} |