Skip to content
This repository has been archived by the owner on Dec 4, 2019. It is now read-only.

Commit

Permalink
#704: Implement setting and saving config values.
Browse files Browse the repository at this point in the history
  • Loading branch information
franzliedke committed Aug 1, 2012
1 parent 7f7f0ec commit 290beb8
Showing 1 changed file with 71 additions and 19 deletions.
90 changes: 71 additions & 19 deletions models/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,38 @@ class Config

protected static $data = array();

protected static function data()
protected static $original = array();

protected static function load_data()
{
if (!static::$loaded)
if (static::$loaded)
{
static::$data = Cache::remember('fluxbb.config', function()
{
$data = DB::table('config')->get();
$cache = array();

foreach ($data as $row)
{
$cache[$row->conf_name] = $row->conf_value;
}
return;
}

static::$data = static::$original = Cache::remember('fluxbb.config', function()
{
$data = DB::table('config')->get();
$cache = array();

return $cache;
}, 24 * 60);
foreach ($data as $row)
{
$cache[$row->conf_name] = $row->conf_value;
}

static::$loaded = true;
}
return $cache;
}, 24 * 60);

return static::$data;
static::$loaded = true;
}

public static function get($key, $default = null)
{
$data = static::data();
static::load_data();

if (array_key_exists($key, $data))
if (array_key_exists($key, static::$data))
{
return $data[$key];
return static::$data[$key];
}

return $default;
Expand All @@ -79,4 +81,54 @@ public static function disabled($key)
return static::get($key, 0) == 0;
}

public static function set($key, $value)
{
static::load_data();

static::$data[$key] = $value;
}

public static function delete($key)
{
static::load_data();

unset(static::$data[$key]);
}

public static function save()
{
// New and changed keys
$changed = array_diff_assoc(static::$data, static::$original);

$insert_values = array();
foreach ($changed as $name => $value)
{
if (!array_key_exists($name, static::$original))
{
$insert_values[] = array(
'conf_name' => $name,
'conf_value' => $value,
);

unset($changed[$name]);
}
}

DB::table('config')->insert($insert_values);

foreach ($changed as $name => $value)
{
DB::table('config')->where_conf_name($name)->update(array('conf_value' => $value));
}

// Deleted keys
$deleted_keys = array_keys(array_diff_key(static::$original, static::$data));
DB::table('config')->where_in('conf_name', $deleted_keys)->delete();

// No need to cache old values anymore
static::$original = static::$data;
}

// TODO: Do we need another function to clear the cache here?
}

0 comments on commit 290beb8

Please sign in to comment.