1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
<?php
namespace dokuwiki\plugin\config\core;
use dokuwiki\plugin\config\core\Setting\Setting;
use dokuwiki\plugin\config\core\Setting\SettingNoClass;
use dokuwiki\plugin\config\core\Setting\SettingNoDefault;
use dokuwiki\plugin\config\core\Setting\SettingNoKnownClass;
use dokuwiki\plugin\config\core\Setting\SettingUndefined;
/**
* Holds all the current settings and proxies the Loader and Writer
*
* @author Chris Smith <chris@jalakai.co.uk>
* @author Ben Coburn <btcoburn@silicodon.net>
* @author Andreas Gohr <andi@splitbrain.org>
*/
class Configuration
{
public const KEYMARKER = '____';
/** @var Setting[] metadata as array of Settings objects */
protected $settings = [];
/** @var Setting[] undefined and problematic settings */
protected $undefined = [];
/** @var array all metadata */
protected $metadata;
/** @var array all default settings */
protected $default;
/** @var array all local settings */
protected $local;
/** @var array all protected settings */
protected $protected;
/** @var bool have the settings been changed since loading from disk? */
protected $changed = false;
/** @var Loader */
protected $loader;
/** @var Writer */
protected $writer;
/**
* ConfigSettings constructor.
*/
public function __construct()
{
$this->loader = new Loader(new ConfigParser());
$this->writer = new Writer();
$this->metadata = $this->loader->loadMeta();
$this->default = $this->loader->loadDefaults();
$this->local = $this->loader->loadLocal();
$this->protected = $this->loader->loadProtected();
$this->initSettings();
}
/**
* Get all settings
*
* @return Setting[]
*/
public function getSettings()
{
return $this->settings;
}
/**
* Get all unknown or problematic settings
*
* @return Setting[]
*/
public function getUndefined()
{
return $this->undefined;
}
/**
* Have the settings been changed since loading from disk?
*
* @return bool
*/
public function hasChanged()
{
return $this->changed;
}
/**
* Check if the config can be written
*
* @return bool
*/
public function isLocked()
{
return $this->writer->isLocked();
}
/**
* Update the settings using the data provided
*
* @param array $input as posted
* @return bool true if all updates went through, false on errors
*/
public function updateSettings($input)
{
$ok = true;
foreach ($this->settings as $key => $obj) {
$value = $input[$key] ?? null;
if ($obj->update($value)) {
$this->changed = true;
}
if ($obj->hasError()) $ok = false;
}
return $ok;
}
/**
* Save the settings
*
* This save the current state as defined in this object, including the
* undefined settings
*
* @throws \Exception
*/
public function save()
{
// only save the undefined settings that have not been handled in settings
$undefined = array_diff_key($this->undefined, $this->settings);
$this->writer->save(array_merge($this->settings, $undefined));
}
/**
* Touch the settings
*
* @throws \Exception
*/
public function touch()
{
$this->writer->touch();
}
/**
* Load the extension language strings
*
* @return array
*/
public function getLangs()
{
return $this->loader->loadLangs();
}
/**
* Initalizes the $settings and $undefined properties
*/
protected function initSettings()
{
$keys = [
...array_keys($this->metadata),
...array_keys($this->default),
...array_keys($this->local),
...array_keys($this->protected)
];
$keys = array_unique($keys);
foreach ($keys as $key) {
$obj = $this->instantiateClass($key);
if ($obj->shouldHaveDefault() && !isset($this->default[$key])) {
$this->undefined[$key] = new SettingNoDefault($key);
}
$d = $this->default[$key] ?? null;
$l = $this->local[$key] ?? null;
$p = $this->protected[$key] ?? null;
$obj->initialize($d, $l, $p);
}
}
/**
* Instantiates the proper class for the given config key
*
* The class is added to the $settings or $undefined arrays and returned
*
* @param string $key
* @return Setting
*/
protected function instantiateClass($key)
{
if (isset($this->metadata[$key])) {
$param = $this->metadata[$key];
$class = $this->determineClassName(array_shift($param), $key); // first param is class
$obj = new $class($key, $param);
$this->settings[$key] = $obj;
} else {
$obj = new SettingUndefined($key);
$this->undefined[$key] = $obj;
}
return $obj;
}
/**
* Return the class to load
*
* @param string $class the class name as given in the meta file
* @param string $key the settings key
* @return string
*/
protected function determineClassName($class, $key)
{
// try namespaced class first
if (is_string($class)) {
$modern = str_replace('_', '', ucwords($class, '_'));
$modern = '\\dokuwiki\\plugin\\config\\core\\Setting\\Setting' . $modern;
if ($modern && class_exists($modern)) return $modern;
// try class as given
if (class_exists($class)) return $class;
// class wasn't found add to errors
$this->undefined[$key] = new SettingNoKnownClass($key);
} else {
// no class given, add to errors
$this->undefined[$key] = new SettingNoClass($key);
}
return '\\dokuwiki\\plugin\\config\\core\\Setting\\Setting';
}
}
|