Add write support to Config backends with add/set methods
This commit is contained in:
@@ -56,6 +56,28 @@ class SihnonFramework_Config {
|
|||||||
$this->settings = $this->backend->preload();
|
$this->settings = $this->backend->preload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static function pack($type, $value) {
|
||||||
|
switch ($type) {
|
||||||
|
case static::TYPE_STRING_LIST: {
|
||||||
|
return join("\n", $value);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
default: {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static function unpack($type, $value) {
|
||||||
|
switch ($type) {
|
||||||
|
case self::TYPE_STRING_LIST:
|
||||||
|
return array_map('trim', explode("\n", $value));
|
||||||
|
|
||||||
|
default:
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Identifies whether the named setting exists
|
* Identifies whether the named setting exists
|
||||||
*
|
*
|
||||||
@@ -76,13 +98,59 @@ class SihnonFramework_Config {
|
|||||||
throw new Sihnon_Exception_UnknownSetting($key);
|
throw new Sihnon_Exception_UnknownSetting($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ($this->settings[$key]['type']) {
|
return static::unpack($this->settings[$key]['type'], $this->settings[$key]['value']);
|
||||||
case self::TYPE_STRING_LIST:
|
}
|
||||||
return array_map('trim', explode("\n", $this->settings[$key]['value']));
|
|
||||||
|
|
||||||
default:
|
public function type($key) {
|
||||||
return $this->settings[$key]['value'];
|
if (!isset($this->settings[$key])) {
|
||||||
|
throw new Sihnon_Exception_UnknownSetting($key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->settings[$key]['type'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function enumerateAll() {
|
||||||
|
return array_map(function($r) {return $r['name'];}, $this->settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set($key, $value) {
|
||||||
|
if ( ! ($this->backend instanceof Sihnon_Config_IUpdateable)) {
|
||||||
|
throw new Sihnon_Exception_ReadOnlyConfigBackend();
|
||||||
|
}
|
||||||
|
if (!isset($this->settings[$key])) {
|
||||||
|
throw new Sihnon_Exception_UnknownSetting($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
$packed_value = static::pack($this->settings[$key]['type'], $value);
|
||||||
|
|
||||||
|
// Change the setting value for this run
|
||||||
|
$this->settings[$key]['value'] = $packed_value;
|
||||||
|
|
||||||
|
// Persist the change into the backend
|
||||||
|
return $this->backend->set($key, $packed_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function add($key, $type, $value) {
|
||||||
|
if ( ! ($this->backend instanceof Sihnon_Config_IUpdateable)) {
|
||||||
|
throw new Sihnon_Exception_ReadOnlyConfigBackend();
|
||||||
|
}
|
||||||
|
if (isset($this->settings[$key])) {
|
||||||
|
throw new Sihnon_Exception_SettingExists($key);
|
||||||
|
}
|
||||||
|
if ( ! Sihnon_Main::isClassConstantValue(get_called_class(), 'TYPE_', $type)) {
|
||||||
|
throw new Sihnon_Exception_UnknownSettingType($type);
|
||||||
|
}
|
||||||
|
|
||||||
|
$packed_value = static::pack($type, $value);
|
||||||
|
|
||||||
|
// Add the setting for this run
|
||||||
|
$this->settings[$key] = array(
|
||||||
|
'type' => $type,
|
||||||
|
'value' => $packed_value,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Persist the setting into the backend
|
||||||
|
return $this->backend->add($key, $type, $packed_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
26
source/lib/SihnonFramework/Config/IUpdateable.class.php
Normal file
26
source/lib/SihnonFramework/Config/IUpdateable.class.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
interface SihnonFramework_Config_IUpdateable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Change the value of a setting
|
||||||
|
* @param string $key
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function set($key, $value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Add a new setting
|
||||||
|
* @param string $key
|
||||||
|
* @param string $type
|
||||||
|
* @param mixed $value
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function add($key, $type, $value);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class SihnonFramework_Config_Plugin_Database extends Sihnon_PluginBase implements Sihnon_Config_IPlugin {
|
class SihnonFramework_Config_Plugin_Database extends Sihnon_PluginBase implements Sihnon_Config_IPlugin, Sihnon_Config_IUpdateable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Name of this plugin
|
* Name of this plugin
|
||||||
@@ -28,6 +28,21 @@ class SihnonFramework_Config_Plugin_Database extends Sihnon_PluginBase implement
|
|||||||
throw new Sihnon_Exception_NotImplemented();
|
throw new Sihnon_Exception_NotImplemented();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function set($key, $value) {
|
||||||
|
return $this->database->update("UPDATE `{$this->table}` SET `value`=:value WHERE `name`=:name", array(
|
||||||
|
array('name' => 'name', 'value' => $key, 'type' => PDO::PARAM_STR),
|
||||||
|
array('name' => 'value', 'value' => $value, 'type' => PDO::PARAM_STR),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function add($key, $type, $value) {
|
||||||
|
return $this->database->insert("INSERT INTO `{$this->table}` (`name`,`value`,`type`) VALUES(:name,:value,:type)", array(
|
||||||
|
array('name' => 'name', 'value' => $key, 'type' => PDO::PARAM_STR),
|
||||||
|
array('name' => 'value', 'value' => $value, 'type' => PDO::PARAM_STR),
|
||||||
|
array('name' => 'type', 'value' => $type, 'type' => PDO::PARAM_STR),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
@@ -24,6 +24,9 @@ class SihnonFramework_Exception_ResultCountMismatch extends SihnonFramework_E
|
|||||||
|
|
||||||
class SihnonFramework_Exception_ConfigException extends SihnonFramework_Exception {};
|
class SihnonFramework_Exception_ConfigException extends SihnonFramework_Exception {};
|
||||||
class SihnonFramework_Exception_UnknownSetting extends SihnonFramework_Exception_ConfigException {};
|
class SihnonFramework_Exception_UnknownSetting extends SihnonFramework_Exception_ConfigException {};
|
||||||
|
class SihnonFramework_Exception_ReadOnlyConfigBackend extends SihnonFramework_Exception_ConfigException {};
|
||||||
|
class SihnonFramework_Exception_SettingExists extends SihnonFramework_Exception_ConfigException {};
|
||||||
|
class SihnonFramework_Exception_UnknownSettingType extends SihnonFramework_Exception_ConfigException {};
|
||||||
|
|
||||||
class SihnonFramework_Exception_CacheException extends SihnonFramework_Exception {};
|
class SihnonFramework_Exception_CacheException extends SihnonFramework_Exception {};
|
||||||
class SihnonFramework_Exception_InvalidCacheDir extends SihnonFramework_Exception_CacheException {};
|
class SihnonFramework_Exception_InvalidCacheDir extends SihnonFramework_Exception_CacheException {};
|
||||||
|
|||||||
Reference in New Issue
Block a user