[core] config->save() is implicit in set_XXX(), no need to call it from outside of there

This commit is contained in:
Shish 2024-06-19 14:34:22 +01:00 committed by Shish
parent e3e25c1228
commit 63c6f9d2ac
3 changed files with 26 additions and 34 deletions

View file

@ -11,13 +11,6 @@ namespace Shimmie2;
*/ */
interface Config interface Config
{ {
/**
* Save the list of name:value pairs to wherever they came from,
* so that the next time a page is loaded it will use the new
* configuration.
*/
public function save(string $name = null): void;
//@{ /*--------------------------------- SET ------------------------------------------------------*/ //@{ /*--------------------------------- SET ------------------------------------------------------*/
/** /**
* Set a configuration option to a new value, regardless of what the value is at the moment. * Set a configuration option to a new value, regardless of what the value is at the moment.
@ -144,6 +137,13 @@ abstract class BaseConfig implements Config
/** @var array<string, mixed> */ /** @var array<string, mixed> */
public array $values = []; public array $values = [];
/**
* Save the list of name:value pairs to wherever they came from,
* so that the next time a page is loaded it will use the new
* configuration.
*/
abstract protected function save(string $name): void;
public function set_int(string $name, ?int $value): void public function set_int(string $name, ?int $value): void
{ {
$this->values[$name] = is_null($value) ? null : $value; $this->values[$name] = is_null($value) ? null : $value;
@ -341,35 +341,29 @@ class DatabaseConfig extends BaseConfig
return $values; return $values;
} }
public function save(string $name = null): void protected function save(string $name): void
{ {
global $cache; global $cache;
if (is_null($name)) { $query = "DELETE FROM {$this->table_name} WHERE name = :name";
reset($this->values); // rewind the array to the first element $args = ["name" => $name];
foreach ($this->values as $name => $value) { $cols = ["name","value"];
$this->save($name); $params = [":name",":value"];
} if (!empty($this->sub_column) && !empty($this->sub_value)) {
} else { $query .= " AND $this->sub_column = :sub_value";
$query = "DELETE FROM {$this->table_name} WHERE name = :name"; $args["sub_value"] = $this->sub_value;
$args = ["name" => $name]; $cols[] = $this->sub_column;
$cols = ["name","value"]; $params[] = ":sub_value";
$params = [":name",":value"];
if (!empty($this->sub_column) && !empty($this->sub_value)) {
$query .= " AND $this->sub_column = :sub_value";
$args["sub_value"] = $this->sub_value;
$cols[] = $this->sub_column;
$params[] = ":sub_value";
}
$this->database->execute($query, $args);
$args["value"] = $this->values[$name];
$this->database->execute(
"INSERT INTO {$this->table_name} (".join(",", $cols).") VALUES (".join(",", $params).")",
$args
);
} }
$this->database->execute($query, $args);
$args["value"] = $this->values[$name];
$this->database->execute(
"INSERT INTO {$this->table_name} (".join(",", $cols).") VALUES (".join(",", $params).")",
$args
);
// rather than deleting and having some other request(s) do a thundering // rather than deleting and having some other request(s) do a thundering
// herd of race-conditioned updates, just save the updated version once here // herd of race-conditioned updates, just save the updated version once here
$cache->set($this->cache_name, $this->values); $cache->set($this->cache_name, $this->values);

View file

@ -73,7 +73,6 @@ class Featured extends Extension
global $config; global $config;
if ($event->image->id == $config->get_int("featured_id")) { if ($event->image->id == $config->get_int("featured_id")) {
$config->set_int("featured_id", 0); $config->set_int("featured_id", 0);
$config->save();
} }
} }

View file

@ -406,7 +406,6 @@ class Setup extends Extension
} }
} }
} }
$config->save();
log_warning("setup", "Configuration updated"); log_warning("setup", "Configuration updated");
} }