This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/core/config.php

295 lines
8.3 KiB
PHP
Raw Normal View History

<?php
2009-07-19 07:38:13 +00:00
/**
* Interface Config
*
* An abstract interface for altering a name:value pair list.
2009-01-04 14:38:48 +00:00
*/
interface Config {
2010-01-12 15:01:34 +00:00
/**
* 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.
2010-01-12 15:01:34 +00:00
*/
2019-05-28 16:31:20 +00:00
public function save(string $name=null): void;
2009-01-04 14:38:48 +00:00
//@{ /*--------------------------------- SET ------------------------------------------------------*/
/**
* Set a configuration option to a new value, regardless of what the value is at the moment.
2010-01-12 15:01:34 +00:00
*/
2019-05-28 16:31:20 +00:00
public function set_int(string $name, ?int $value): void;
/**
* Set a configuration option to a new value, regardless of what the value is at the moment.
*/
2019-05-28 16:31:20 +00:00
public function set_string(string $name, ?string $value): void;
/**
* Set a configuration option to a new value, regardless of what the value is at the moment.
* @param null|bool|string $value
*/
2019-05-28 16:31:20 +00:00
public function set_bool(string $name, $value): void;
/**
* Set a configuration option to a new value, regardless of what the value is at the moment.
*/
2019-05-28 16:31:20 +00:00
public function set_array(string $name, array $value): void;
//@} /*--------------------------------------------------------------------------------------------*/
2009-01-04 14:38:48 +00:00
//@{ /*-------------------------------- SET DEFAULT -----------------------------------------------*/
/**
* Set a configuration option to a new value, if there is no value currently.
*
* Extensions should generally call these from their InitExtEvent handlers.
* This has the advantage that the values will show up in the "advanced" setup
* page where they can be modified, while calling get_* with a "default"
* parameter won't show up.
2010-01-12 15:01:34 +00:00
*/
2019-05-28 16:31:20 +00:00
public function set_default_int(string $name, int $value): void;
/**
* Set a configuration option to a new value, if there is no value currently.
*
* Extensions should generally call these from their InitExtEvent handlers.
* This has the advantage that the values will show up in the "advanced" setup
* page where they can be modified, while calling get_* with a "default"
* parameter won't show up.
*/
2019-05-28 16:31:20 +00:00
public function set_default_string(string $name, string $value): void;
/**
* Set a configuration option to a new value, if there is no value currently.
*
* Extensions should generally call these from their InitExtEvent handlers.
* This has the advantage that the values will show up in the "advanced" setup
* page where they can be modified, while calling get_* with a "default"
* parameter won't show up.
*/
2019-05-28 16:31:20 +00:00
public function set_default_bool(string $name, bool $value): void;
/**
* Set a configuration option to a new value, if there is no value currently.
*
* Extensions should generally call these from their InitExtEvent handlers.
* This has the advantage that the values will show up in the "advanced" setup
* page where they can be modified, while calling get_* with a "default"
* parameter won't show up.
*/
2019-05-28 16:31:20 +00:00
public function set_default_array(string $name, array $value): void;
//@} /*--------------------------------------------------------------------------------------------*/
2009-01-04 14:38:48 +00:00
//@{ /*--------------------------------- GET ------------------------------------------------------*/
/**
* Pick a value out of the table by name, cast to the appropriate data type.
2010-01-12 15:01:34 +00:00
*/
2019-05-28 16:31:20 +00:00
public function get_int(string $name, ?int $default=null): ?int;
/**
* Pick a value out of the table by name, cast to the appropriate data type.
*/
2019-05-28 16:31:20 +00:00
public function get_string(string $name, ?string $default=null): ?string;
/**
* Pick a value out of the table by name, cast to the appropriate data type.
*/
2019-05-28 16:31:20 +00:00
public function get_bool(string $name, ?bool $default=null): ?bool;
/**
* Pick a value out of the table by name, cast to the appropriate data type.
*/
2019-05-28 16:31:20 +00:00
public function get_array(string $name, ?array $default=array()): ?array;
//@} /*--------------------------------------------------------------------------------------------*/
2009-01-04 14:38:48 +00:00
}
2009-07-19 07:38:13 +00:00
/**
* Class BaseConfig
*
2009-01-04 14:38:48 +00:00
* Common methods for manipulating the list, loading and saving is
* left to the concrete implementation
*/
abstract class BaseConfig implements Config {
2016-06-19 22:05:57 +00:00
public $values = array();
2009-01-04 14:38:48 +00:00
2017-09-19 17:55:43 +00:00
public function set_int(string $name, $value) {
2009-01-04 14:38:48 +00:00
$this->values[$name] = parse_shorthand_int($value);
$this->save($name);
}
2017-09-19 17:55:43 +00:00
public function set_string(string $name, $value) {
2009-01-04 14:38:48 +00:00
$this->values[$name] = $value;
$this->save($name);
}
2017-09-19 17:55:43 +00:00
public function set_bool(string $name, $value) {
$this->values[$name] = bool_escape($value) ? 'Y' : 'N';
2009-01-04 14:38:48 +00:00
$this->save($name);
}
2017-09-19 17:55:43 +00:00
public function set_array(string $name, array $value) {
$this->values[$name] = implode(",", $value);
$this->save($name);
}
2009-01-04 14:38:48 +00:00
2017-09-19 17:55:43 +00:00
public function set_default_int(string $name, int $value) {
2009-01-04 14:38:48 +00:00
if(is_null($this->get($name))) {
2017-09-19 17:55:43 +00:00
$this->values[$name] = $value;
2009-01-04 14:38:48 +00:00
}
}
2017-09-19 17:55:43 +00:00
public function set_default_string(string $name, string $value) {
2009-01-04 14:38:48 +00:00
if(is_null($this->get($name))) {
$this->values[$name] = $value;
}
}
2017-09-19 17:55:43 +00:00
public function set_default_bool(string $name, bool $value) {
2009-01-04 14:38:48 +00:00
if(is_null($this->get($name))) {
2017-09-19 17:55:43 +00:00
$this->values[$name] = $value ? 'Y' : 'N';
2009-01-04 14:38:48 +00:00
}
}
2017-09-19 17:55:43 +00:00
public function set_default_array(string $name, array $value) {
if(is_null($this->get($name))) {
$this->values[$name] = implode(",", $value);
}
}
2009-01-04 14:38:48 +00:00
2017-09-19 17:55:43 +00:00
public function get_int(string $name, $default=null) {
2009-01-04 14:38:48 +00:00
return (int)($this->get($name, $default));
}
2017-09-19 17:55:43 +00:00
public function get_string(string $name, $default=null) {
2009-01-04 14:38:48 +00:00
return $this->get($name, $default);
}
2017-09-19 17:55:43 +00:00
public function get_bool(string $name, $default=null) {
return bool_escape($this->get($name, $default));
2009-01-04 14:38:48 +00:00
}
2017-09-19 17:55:43 +00:00
public function get_array(string $name, array $default=array()): array {
return explode(",", $this->get($name, ""));
}
2009-01-04 14:38:48 +00:00
2017-09-19 17:55:43 +00:00
private function get(string $name, $default=null) {
2009-01-04 14:38:48 +00:00
if(isset($this->values[$name])) {
return $this->values[$name];
}
else {
return $default;
}
}
}
2012-08-15 17:31:28 +00:00
/**
* Class HardcodeConfig
*
* For testing, mostly.
2012-08-15 17:31:28 +00:00
*/
class HardcodeConfig extends BaseConfig {
2017-09-19 17:55:43 +00:00
public function __construct(array $dict) {
2012-08-15 17:31:28 +00:00
$this->values = $dict;
}
2017-09-19 17:55:43 +00:00
public function save(string $name=null) {
2012-08-15 17:31:28 +00:00
// static config is static
}
}
2009-07-19 07:38:13 +00:00
/**
* Class StaticConfig
*
2009-01-04 15:57:54 +00:00
* Loads the config list from a PHP file; the file should be in the format:
*
* <?php
* $config['foo'] = "bar";
* $config['baz'] = "qux";
* ?>
*/
class StaticConfig extends BaseConfig {
2017-09-19 17:55:43 +00:00
public function __construct(string $filename) {
2009-01-04 15:57:54 +00:00
if(file_exists($filename)) {
2015-08-04 12:53:58 +00:00
$config = array();
2009-01-04 15:57:54 +00:00
require_once $filename;
2015-08-04 12:53:58 +00:00
if(!empty($config)) {
2009-01-04 15:57:54 +00:00
$this->values = $config;
}
else {
throw new Exception("Config file '$filename' doesn't contain any config");
}
}
else {
throw new Exception("Config file '$filename' missing");
}
}
2017-09-19 17:55:43 +00:00
public function save(string $name=null) {
2009-01-04 15:57:54 +00:00
// static config is static
}
}
2009-07-19 07:38:13 +00:00
/**
* Class DatabaseConfig
*
2009-01-04 15:57:54 +00:00
* Loads the config list from a table in a given database, the table should
* be called config and have the schema:
*
* \code
2009-01-04 15:57:54 +00:00
* CREATE TABLE config(
* name VARCHAR(255) NOT NULL,
* value TEXT
* );
* \endcode
*/
2009-01-04 14:40:35 +00:00
class DatabaseConfig extends BaseConfig {
2016-06-19 22:05:57 +00:00
/** @var Database */
private $database = null;
2014-03-22 09:00:59 +00:00
public function __construct(Database $database) {
$this->database = $database;
2009-01-22 07:21:03 +00:00
$cached = $this->database->cache->get("config");
if($cached) {
$this->values = $cached;
}
else {
2011-01-01 15:28:30 +00:00
$this->values = array();
foreach($this->database->get_all("SELECT name, value FROM config") as $row) {
$this->values[$row["name"]] = $row["value"];
}
2009-01-22 07:21:03 +00:00
$this->database->cache->set("config", $this->values);
}
}
2017-09-19 17:55:43 +00:00
public function save(string $name=null) {
if(is_null($name)) {
reset($this->values); // rewind the array to the first element
foreach($this->values as $name => $value) {
2017-09-19 17:55:43 +00:00
$this->save($name);
}
}
else {
2011-01-01 15:28:30 +00:00
$this->database->Execute("DELETE FROM config WHERE name = :name", array("name"=>$name));
$this->database->Execute("INSERT INTO config VALUES (:name, :value)", array("name"=>$name, "value"=>$this->values[$name]));
}
// rather than deleting and having some other request(s) do a thundering
// herd of race-conditioned updates, just save the updated version once here
$this->database->cache->set("config", $this->values);
}
}
/**
* Class MockConfig
*/
class MockConfig extends HardcodeConfig {
2017-09-19 17:55:43 +00:00
public function __construct(array $config=array()) {
$config["db_version"] = "999";
$config["anon_id"] = "0";
parent::__construct($config);
}
}