2007-04-16 11:58:25 +00:00
|
|
|
<?php
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
|
|
|
* @package SCore
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-01-04 14:38:48 +00:00
|
|
|
* an abstract interface for altering a name:value pair list
|
|
|
|
*/
|
|
|
|
interface Config {
|
|
|
|
public function save($name=null);
|
|
|
|
|
|
|
|
public function set_int($name, $value);
|
|
|
|
public function set_string($name, $value);
|
|
|
|
public function set_bool($name, $value);
|
2009-07-11 11:43:18 +00:00
|
|
|
public function set_array($name, $value);
|
2009-01-04 14:38:48 +00:00
|
|
|
|
|
|
|
public function set_default_int($name, $value);
|
|
|
|
public function set_default_string($name, $value);
|
|
|
|
public function set_default_bool($name, $value);
|
2009-07-11 11:43:18 +00:00
|
|
|
public function set_default_array($name, $value);
|
2009-01-04 14:38:48 +00:00
|
|
|
|
|
|
|
public function get_int($name, $default=null);
|
|
|
|
public function get_string($name, $default=null);
|
|
|
|
public function get_bool($name, $default=null);
|
2009-07-11 11:43:18 +00:00
|
|
|
public function get_array($name, $default=array());
|
2009-01-04 14:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
2009-01-04 14:38:48 +00:00
|
|
|
* Common methods for manipulating the list, loading and saving is
|
|
|
|
* left to the concrete implementation
|
2009-07-19 07:38:13 +00:00
|
|
|
*
|
|
|
|
* @ignore
|
2009-01-04 14:38:48 +00:00
|
|
|
*/
|
|
|
|
abstract class BaseConfig implements Config {
|
|
|
|
var $values = array();
|
|
|
|
|
|
|
|
public function set_int($name, $value) {
|
|
|
|
$this->values[$name] = parse_shorthand_int($value);
|
|
|
|
$this->save($name);
|
|
|
|
}
|
|
|
|
public function set_string($name, $value) {
|
|
|
|
$this->values[$name] = $value;
|
|
|
|
$this->save($name);
|
|
|
|
}
|
|
|
|
public function set_bool($name, $value) {
|
|
|
|
$this->values[$name] = (($value == 'on' || $value === true) ? 'Y' : 'N');
|
|
|
|
$this->save($name);
|
|
|
|
}
|
2009-07-11 11:43:18 +00:00
|
|
|
public function set_array($name, $value) {
|
|
|
|
assert(is_array($value));
|
|
|
|
$this->values[$name] = implode(",", $value);
|
|
|
|
$this->save($name);
|
|
|
|
}
|
2009-01-04 14:38:48 +00:00
|
|
|
|
|
|
|
public function set_default_int($name, $value) {
|
|
|
|
if(is_null($this->get($name))) {
|
|
|
|
$this->values[$name] = parse_shorthand_int($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function set_default_string($name, $value) {
|
|
|
|
if(is_null($this->get($name))) {
|
|
|
|
$this->values[$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function set_default_bool($name, $value) {
|
|
|
|
if(is_null($this->get($name))) {
|
|
|
|
$this->values[$name] = (($value == 'on' || $value === true) ? 'Y' : 'N');
|
|
|
|
}
|
|
|
|
}
|
2009-07-11 11:43:18 +00:00
|
|
|
public function set_default_array($name, $value) {
|
|
|
|
assert(is_array($value));
|
|
|
|
if(is_null($this->get($name))) {
|
|
|
|
$this->values[$name] = implode(",", $value);
|
|
|
|
}
|
|
|
|
}
|
2009-01-04 14:38:48 +00:00
|
|
|
|
|
|
|
public function get_int($name, $default=null) {
|
|
|
|
return (int)($this->get($name, $default));
|
|
|
|
}
|
|
|
|
public function get_string($name, $default=null) {
|
|
|
|
return $this->get($name, $default);
|
|
|
|
}
|
|
|
|
public function get_bool($name, $default=null) {
|
|
|
|
return ($this->get($name, $default) == 'Y');
|
|
|
|
}
|
2009-07-11 11:43:18 +00:00
|
|
|
public function get_array($name, $default=array()) {
|
|
|
|
return explode(",", $this->get($name, ""));
|
|
|
|
}
|
2009-01-04 14:38:48 +00:00
|
|
|
|
|
|
|
private function get($name, $default=null) {
|
|
|
|
if(isset($this->values[$name])) {
|
|
|
|
return $this->values[$name];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
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";
|
|
|
|
* ?>
|
2009-07-19 07:38:13 +00:00
|
|
|
*
|
|
|
|
* @ignore
|
2009-01-04 15:57:54 +00:00
|
|
|
*/
|
|
|
|
class StaticConfig extends BaseConfig {
|
|
|
|
public function __construct($filename) {
|
|
|
|
if(file_exists($filename)) {
|
|
|
|
require_once $filename;
|
|
|
|
if(isset($config)) {
|
|
|
|
$this->values = $config;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Exception("Config file '$filename' doesn't contain any config");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw new Exception("Config file '$filename' missing");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function save($name=null) {
|
|
|
|
// static config is static
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-19 07:38:13 +00:00
|
|
|
/**
|
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:
|
|
|
|
*
|
|
|
|
* CREATE TABLE config(
|
|
|
|
* name VARCHAR(255) NOT NULL,
|
|
|
|
* value TEXT
|
|
|
|
* );
|
2009-07-19 07:38:13 +00:00
|
|
|
*
|
|
|
|
* @ignore
|
2007-12-06 11:01:18 +00:00
|
|
|
*/
|
2009-01-04 14:40:35 +00:00
|
|
|
class DatabaseConfig extends BaseConfig {
|
2007-07-28 13:53:31 +00:00
|
|
|
var $database = null;
|
2007-04-16 11:58:25 +00:00
|
|
|
|
2007-12-06 11:01:18 +00:00
|
|
|
/*
|
|
|
|
* Load the config table from a database
|
|
|
|
*/
|
2009-01-04 14:40:35 +00:00
|
|
|
public function DatabaseConfig($database) {
|
2007-07-28 13:53:31 +00:00
|
|
|
$this->database = $database;
|
2009-01-19 18:47:33 +00:00
|
|
|
|
2009-01-22 07:21:03 +00:00
|
|
|
$cached = $this->database->cache->get("config");
|
2009-01-19 18:47:33 +00:00
|
|
|
if($cached) {
|
|
|
|
$this->values = $cached;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->values = $this->database->db->GetAssoc("SELECT name, value FROM config");
|
2009-01-22 07:21:03 +00:00
|
|
|
$this->database->cache->set("config", $this->values);
|
2009-01-19 18:47:33 +00:00
|
|
|
}
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2007-12-06 11:01:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the current values as the new config table
|
|
|
|
*/
|
2007-04-16 11:58:25 +00:00
|
|
|
public function save($name=null) {
|
|
|
|
if(is_null($name)) {
|
|
|
|
foreach($this->values as $name => $value) {
|
2007-12-06 10:47:09 +00:00
|
|
|
$this->save($name);
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2007-07-28 13:53:31 +00:00
|
|
|
$this->database->Execute("DELETE FROM config WHERE name = ?", array($name));
|
|
|
|
$this->database->Execute("INSERT INTO config VALUES (?, ?)", array($name, $this->values[$name]));
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
2009-01-22 07:21:03 +00:00
|
|
|
$this->database->cache->delete("config");
|
2007-04-16 11:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|