Move config file creation to home config
This commit is contained in:
parent
0fcbd87083
commit
b7e5ee3b0e
@ -2,7 +2,7 @@
|
||||
|
||||
require_once __DIR__ . '/functions.php';
|
||||
|
||||
readonly class homeConfig extends configReader
|
||||
readonly class homeConfig extends config
|
||||
{
|
||||
private const CONFIG_BASE_PATH = '.config';
|
||||
|
||||
@ -15,21 +15,34 @@ readonly class homeConfig extends configReader
|
||||
self::CONFIG_BASE_PATH,
|
||||
$name
|
||||
));
|
||||
$this->createConfig();
|
||||
}
|
||||
|
||||
private function createConfig(): void
|
||||
{
|
||||
if (file_exists($this->getConfigFile())) {
|
||||
return;
|
||||
} else {
|
||||
if (!is_dir($this->getConfigPath())) {
|
||||
mkdir($this->getConfigPath(), 0755, true);
|
||||
}
|
||||
$this->writeConfig([]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
readonly class configReader
|
||||
readonly class config
|
||||
{
|
||||
public function __construct(
|
||||
private string $path,
|
||||
private string $fileName = 'config.php',
|
||||
)
|
||||
{
|
||||
$this->init();
|
||||
}
|
||||
) {}
|
||||
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
if (!file_exists($this->getConfigFile())) {
|
||||
return $default;
|
||||
}
|
||||
$config = include $this->getConfigFile();
|
||||
return $config[$key] ?? $default;
|
||||
}
|
||||
@ -41,32 +54,17 @@ readonly class configReader
|
||||
$this->writeConfig($config);
|
||||
}
|
||||
|
||||
private function getConfigFile(): string
|
||||
protected function getConfigFile(): string
|
||||
{
|
||||
return path(
|
||||
$this->getConfigPath(),
|
||||
$this->fileName
|
||||
);
|
||||
return path($this->getConfigPath(), $this->fileName);
|
||||
}
|
||||
|
||||
private function getConfigPath(): string
|
||||
protected 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
|
||||
protected function writeConfig(array $config): void
|
||||
{
|
||||
file_put_contents($this->getConfigFile(), '<?php return ' . var_export($config, true) . ';');
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user