feat: autosave configurations

This commit is contained in:
Rekryt
2024-08-31 15:27:36 +03:00
parent 7ee8f233ef
commit e5b21f822c
2 changed files with 34 additions and 1 deletions

View File

@@ -30,7 +30,8 @@ class IPListService {
}
foreach (scandir($dir) as $file) {
if (str_ends_with($file, '.json')) {
$this->loadConfig(substr($file, 0, -5), json_decode(file_get_contents($dir . $file)));
$name = substr($file, 0, -5);
$this->loadConfig($name, json_decode(file_get_contents($dir . $file)));
}
}
}
@@ -44,6 +45,11 @@ class IPListService {
return self::$_instance ??= new self($logger);
}
/**
* @param string $name
* @param object $config
* @return void
*/
private function loadConfig(string $name, object $config): void {
$this->sites[$name] = SiteFactory::create($name, $config);
}

View File

@@ -62,6 +62,7 @@ final class Site {
$this->ip4 = SiteFactory::normalize(array_merge($this->ip4, $ip4));
$this->ip6 = SiteFactory::normalize(array_merge($this->ip6, $ip6));
$this->saveConfig();
App::getLogger()->debug('Reloaded for ' . $this->name);
EventLoop::delay($this->timeout, function () {
@@ -112,4 +113,30 @@ final class Site {
App::getLogger()->debug('External reloaded for ' . $this->name);
}
/**
* @return object
*/
public function getConfig(): object {
return (object) [
'domains' => SiteFactory::normalizeArray($this->domains),
'dns' => $this->dns,
'timeout' => $this->timeout,
'ip4' => SiteFactory::normalizeArray($this->ip4, true),
'ip6' => SiteFactory::normalizeArray($this->ip6, true),
'cidr4' => SiteFactory::normalizeArray($this->cidr4, true),
'cidr6' => SiteFactory::normalizeArray($this->cidr6, true),
'external' => $this->external,
];
}
/**
* @return void
*/
private function saveConfig(): void {
file_put_contents(
PATH_ROOT . '/config/' . $this->name . '.json',
json_encode($this->getConfig(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
);
}
}