From 336257680ccef5f44c3c84bcbfcb5bb73d66da9b Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Sat, 27 Aug 2016 13:20:03 +0000 Subject: [PATCH] Settings: remove settings logic from DB... ...and keep helper method for easy instantiation --- application/controllers/HostController.php | 10 +- library/Director/Db.php | 70 ++-------- library/Director/KickstartHelper.php | 2 +- library/Director/Settings.php | 127 ++++++++++++++++++ .../Director/Web/Form/DirectorObjectForm.php | 2 +- 5 files changed, 142 insertions(+), 69 deletions(-) create mode 100644 library/Director/Settings.php diff --git a/application/controllers/HostController.php b/application/controllers/HostController.php index 6f1036d0..cc377089 100644 --- a/application/controllers/HostController.php +++ b/application/controllers/HostController.php @@ -84,10 +84,7 @@ class HostController extends ObjectController $tables[0] = $table; } - if ($applied = $host->vars()->get($db->getSetting( - 'magic_apply_for', - '_director_apply_for' - ))) { + if ($applied = $host->vars()->get($db->settings()->magic_apply_for)) { $table = $this->loadTable('IcingaHostAppliedForService') ->setHost($host) ->setDictionary($applied) @@ -126,10 +123,7 @@ class HostController extends ObjectController $host = $this->object; $serviceName = $this->params->get('service'); - $applied = $host->vars()->get($db->getSetting( - 'magic_apply_for', - '_director_apply_for' - )); + $applied = $host->vars()->get($db->settings()->magic_apply_for); $props = $applied->{$serviceName}; diff --git a/library/Director/Db.php b/library/Director/Db.php index 7ec564ae..8e9ab19f 100644 --- a/library/Director/Db.php +++ b/library/Director/Db.php @@ -61,6 +61,15 @@ class Db extends DbConnection return (int) $db->fetchOne($query); } + public function settings() + { + if ($this->settings === null) { + $this->settings = new Settings($this); + } + + return $this->settings; + } + public function getMasterZoneName() { if ($this->masterZoneName === null) { @@ -72,7 +81,7 @@ class Db extends DbConnection protected function detectMasterZoneName() { - if ($zone = $this->getSetting('master_zone')) { + if ($zone = $this->settings()->master_zone) { return $zone; } @@ -93,7 +102,7 @@ class Db extends DbConnection public function getDefaultGlobalZoneName() { - return $this->getSetting('default_global_zone', 'director-global'); + return $this->settings()->default_global_zone; } public function hasDeploymentEndpoint() @@ -152,63 +161,6 @@ class Db extends DbConnection return IcingaEndpoint::load($this->getDeploymentEndpointName(), $this); } - public function getSetting($name, $default = null) - { - if ($this->settings === null) { - $this->fetchSettings(); - } - - if (array_key_exists($name, $this->settings)) { - return $this->settings[$name]; - } - - return $default; - } - - public function storeSetting($name, $value) - { - $db = $this->db(); - if ($this->getSetting($name) === $value) { - return $this; - } - - $updated = $db->update( - 'director_setting', - array('setting_value' => $value), - $db->quoteInto('setting_name = ?', $name) - ); - - if ($updated === 0) { - $db->insert( - 'director_setting', - array( - 'setting_name' => $name, - 'setting_value' => $value, - ) - ); - } - - if ($this->settings !== null) { - $this->settings[$name] = $value; - } - - return $this; - } - - public function fetchSettings($force = true) - { - if ($force || $this->settings === null) { - $db = $this->db(); - $query = $db->select()->from( - array('s' => 'director_setting'), - array('setting_name', 'setting_value') - ); - $this->settings = (array) $db->fetchPairs($query); - } - - return $this->settings; - } - public function getActivitylogNeighbors($id, $type = null, $name = null) { $db = $this->db(); diff --git a/library/Director/KickstartHelper.php b/library/Director/KickstartHelper.php index b3a56ee7..0961612d 100644 --- a/library/Director/KickstartHelper.php +++ b/library/Director/KickstartHelper.php @@ -268,7 +268,7 @@ class KickstartHelper $object->store(); } - $db->storeSetting('master_zone', $this->deploymentEndpoint->zone); + $db->settings()->master_zone = $this->deploymentEndpoint->zone; return $this; } diff --git a/library/Director/Settings.php b/library/Director/Settings.php new file mode 100644 index 00000000..bb2f2442 --- /dev/null +++ b/library/Director/Settings.php @@ -0,0 +1,127 @@ + 'director-global', + 'magic_apply_for' => '_director_apply_for', + 'config_format' => 'v2', + 'override_services_varname' => '_override_servicevars', + 'override_services_templatename' => 'host var overrides (Director)', + // 'experimental_features' => null, // 'allow' + // 'master_zone' => null, + ); + + public function __construct(Db $connection) + { + $this->connection = $connection; + $this->db = $connection->getDbAdapter(); + } + + public function get($key, $default = null) + { + if (null === ($value = $this->getSetting($key))) { + if ($default === null) { + return $this->getDefaultValue($key); + } else { + return $default; + } + } + } + + public function getDefaultValue($key) + { + if (array_key_exists($key, $this->defaults)) { + return $this->defaults[$key]; + } else { + return null; + } + } + + public function set($name, $value) + { + $db = $this->db; + + if ($this->getSetting($name) === $value) { + return $this; + } + + $updated = $db->update( + 'director_setting', + array('setting_value' => $value), + $db->quoteInto('setting_name = ?', $name) + ); + + if ($updated === 0) { + $db->insert( + 'director_setting', + array( + 'setting_name' => $name, + 'setting_value' => $value, + ) + ); + } + + if ($this->cache !== null) { + $this->cache[$name] = $value; + } + + return $this; + } + + public function clearCache() + { + $this->cache = null; + return $this; + } + + protected function getSetting($name, $default = null) + { + if ($this->cache === null) { + $this->refreshCache(); + } + + if (array_key_exists($name, $this->cache)) { + return $this->cache[$name]; + } + + return $default; + } + + protected function refreshCache() + { + $db = $this->db; + + $query = $db->select()->from( + array('s' => 'director_setting'), + array('setting_name', 'setting_value') + ); + + $this->cache = (array) $db->fetchPairs($query); + } + + public function __get($key) + { + return $this->get($key); + } + + public function __set($key, $value) + { + $this->set($key, $value); + } + + public function __destruct() + { + $this->clearCache(); + unset($this->db); + unset($this->connection); + } +} diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php index cd42b466..9c51bd69 100644 --- a/library/Director/Web/Form/DirectorObjectForm.php +++ b/library/Director/Web/Form/DirectorObjectForm.php @@ -1429,7 +1429,7 @@ abstract class DirectorObjectForm extends QuickForm // NO, it is NOT a good idea to use this. You'll break your monitoring // and nobody will help you. if ($this->allowsExperimental === null) { - $this->allowsExperimental = $this->db->getSetting( + $this->allowsExperimental = $this->db->settings()->get( 'experimental_features' ) === 'allow'; }