Settings: remove settings logic from DB...

...and keep helper method for easy instantiation
This commit is contained in:
Thomas Gelf 2016-08-27 13:20:03 +00:00
parent 58441b88ad
commit 336257680c
5 changed files with 142 additions and 69 deletions

View File

@ -84,10 +84,7 @@ class HostController extends ObjectController
$tables[0] = $table; $tables[0] = $table;
} }
if ($applied = $host->vars()->get($db->getSetting( if ($applied = $host->vars()->get($db->settings()->magic_apply_for)) {
'magic_apply_for',
'_director_apply_for'
))) {
$table = $this->loadTable('IcingaHostAppliedForService') $table = $this->loadTable('IcingaHostAppliedForService')
->setHost($host) ->setHost($host)
->setDictionary($applied) ->setDictionary($applied)
@ -126,10 +123,7 @@ class HostController extends ObjectController
$host = $this->object; $host = $this->object;
$serviceName = $this->params->get('service'); $serviceName = $this->params->get('service');
$applied = $host->vars()->get($db->getSetting( $applied = $host->vars()->get($db->settings()->magic_apply_for);
'magic_apply_for',
'_director_apply_for'
));
$props = $applied->{$serviceName}; $props = $applied->{$serviceName};

View File

@ -61,6 +61,15 @@ class Db extends DbConnection
return (int) $db->fetchOne($query); return (int) $db->fetchOne($query);
} }
public function settings()
{
if ($this->settings === null) {
$this->settings = new Settings($this);
}
return $this->settings;
}
public function getMasterZoneName() public function getMasterZoneName()
{ {
if ($this->masterZoneName === null) { if ($this->masterZoneName === null) {
@ -72,7 +81,7 @@ class Db extends DbConnection
protected function detectMasterZoneName() protected function detectMasterZoneName()
{ {
if ($zone = $this->getSetting('master_zone')) { if ($zone = $this->settings()->master_zone) {
return $zone; return $zone;
} }
@ -93,7 +102,7 @@ class Db extends DbConnection
public function getDefaultGlobalZoneName() public function getDefaultGlobalZoneName()
{ {
return $this->getSetting('default_global_zone', 'director-global'); return $this->settings()->default_global_zone;
} }
public function hasDeploymentEndpoint() public function hasDeploymentEndpoint()
@ -152,63 +161,6 @@ class Db extends DbConnection
return IcingaEndpoint::load($this->getDeploymentEndpointName(), $this); 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) public function getActivitylogNeighbors($id, $type = null, $name = null)
{ {
$db = $this->db(); $db = $this->db();

View File

@ -268,7 +268,7 @@ class KickstartHelper
$object->store(); $object->store();
} }
$db->storeSetting('master_zone', $this->deploymentEndpoint->zone); $db->settings()->master_zone = $this->deploymentEndpoint->zone;
return $this; return $this;
} }

View File

@ -0,0 +1,127 @@
<?php
namespace Icinga\Module\Director;
class Settings
{
protected $connection;
protected $db;
protected $cache;
protected $defaults = array(
'default_global_zone' => '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);
}
}

View File

@ -1429,7 +1429,7 @@ abstract class DirectorObjectForm extends QuickForm
// NO, it is NOT a good idea to use this. You'll break your monitoring // NO, it is NOT a good idea to use this. You'll break your monitoring
// and nobody will help you. // and nobody will help you.
if ($this->allowsExperimental === null) { if ($this->allowsExperimental === null) {
$this->allowsExperimental = $this->db->getSetting( $this->allowsExperimental = $this->db->settings()->get(
'experimental_features' 'experimental_features'
) === 'allow'; ) === 'allow';
} }