From b8dfca3aa5a2e40e4b4ffe5053b2339607d2c605 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Mon, 29 Aug 2011 19:19:29 +0100 Subject: [PATCH] Add support for renaming config settings --- source/lib/SihnonFramework/Config.class.php | 16 ++++++++++++++++ .../SihnonFramework/Config/IUpdateable.class.php | 9 +++++++++ .../Config/Plugin/Database.class.php | 7 +++++++ 3 files changed, 32 insertions(+) diff --git a/source/lib/SihnonFramework/Config.class.php b/source/lib/SihnonFramework/Config.class.php index 4e012c1..50a84e6 100644 --- a/source/lib/SihnonFramework/Config.class.php +++ b/source/lib/SihnonFramework/Config.class.php @@ -167,6 +167,22 @@ class SihnonFramework_Config { // Persist the change into the backend return $this->backend->remove($key); } + + public function rename($key, $new_key) { + if ( ! ($this->backend instanceof Sihnon_Config_IUpdateable)) { + throw new Sihnon_Exception_ReadOnlyConfigBackend(); + } + if (!isset($this->settings[$key])) { + throw new Sihnon_Exception_UnknownSetting($key); + } + + // Rename the setting for this run + $this->settings[$new_key] = $this->settings[$key]; + unset($this->settings[$key]); + + // Persist the change into the backend + return $this->backend->rename($key, $new_key); + } }; diff --git a/source/lib/SihnonFramework/Config/IUpdateable.class.php b/source/lib/SihnonFramework/Config/IUpdateable.class.php index 76c1b60..df23dcd 100644 --- a/source/lib/SihnonFramework/Config/IUpdateable.class.php +++ b/source/lib/SihnonFramework/Config/IUpdateable.class.php @@ -29,6 +29,15 @@ interface SihnonFramework_Config_IUpdateable { */ public function remove($key); + /** + * + * Rename a setting + * @param string $key + * @param string $new_key + * @return bool + */ + public function rename($key, $new_key); + } ?> \ No newline at end of file diff --git a/source/lib/SihnonFramework/Config/Plugin/Database.class.php b/source/lib/SihnonFramework/Config/Plugin/Database.class.php index dc98509..81b0b5a 100644 --- a/source/lib/SihnonFramework/Config/Plugin/Database.class.php +++ b/source/lib/SihnonFramework/Config/Plugin/Database.class.php @@ -49,6 +49,13 @@ class SihnonFramework_Config_Plugin_Database extends Sihnon_PluginBase implement )); } + public function rename($key, $new_key) { + return $this->database->update("UPDATE `{$this->table}` SET `name`=:new_name WHERE `name`=:name LIMIT 1", array( + array('name' => 'new_name', 'value' => $new_key, 'type' => PDO::PARAM_STR), + array('name' => 'name', 'value' => $key, 'type' => PDO::PARAM_STR), + )); + } + } ?> \ No newline at end of file