mirror of
https://github.com/Icinga/icingaweb2-module-director.git
synced 2025-07-28 08:14:04 +02:00
Settings: remove settings logic from DB...
...and keep helper method for easy instantiation
This commit is contained in:
parent
58441b88ad
commit
336257680c
@ -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};
|
||||
|
||||
|
@ -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();
|
||||
|
@ -268,7 +268,7 @@ class KickstartHelper
|
||||
$object->store();
|
||||
}
|
||||
|
||||
$db->storeSetting('master_zone', $this->deploymentEndpoint->zone);
|
||||
$db->settings()->master_zone = $this->deploymentEndpoint->zone;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
127
library/Director/Settings.php
Normal file
127
library/Director/Settings.php
Normal 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);
|
||||
}
|
||||
}
|
@ -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';
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user