1c6639e6aSAndreas Gohr<?php 2c6639e6aSAndreas Gohr 3c6639e6aSAndreas Gohrnamespace dokuwiki\plugin\config\core; 47a0ee538SAndreas Gohr 50a5b05ebSAndreas Gohruse dokuwiki\plugin\config\core\Setting\Setting; 67a0ee538SAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingNoClass; 77a0ee538SAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingNoDefault; 87a0ee538SAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingNoKnownClass; 90a5b05ebSAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingUndefined; 10c6639e6aSAndreas Gohr 11c6639e6aSAndreas Gohr/** 125675a07cSAndreas Gohr * Holds all the current settings and proxies the Loader and Writer 13077c27b2SAndreas Gohr * 14077c27b2SAndreas Gohr * @author Chris Smith <chris@jalakai.co.uk> 15077c27b2SAndreas Gohr * @author Ben Coburn <btcoburn@silicodon.net> 16077c27b2SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 17c6639e6aSAndreas Gohr */ 188c7c53b0SAndreas Gohrclass Configuration 198c7c53b0SAndreas Gohr{ 20bf9be0e3SAndreas Gohr public const KEYMARKER = '____'; 21c6639e6aSAndreas Gohr 22077c27b2SAndreas Gohr /** @var Setting[] metadata as array of Settings objects */ 23467c1427SAndreas Gohr protected $settings = []; 247a0ee538SAndreas Gohr /** @var Setting[] undefined and problematic settings */ 25467c1427SAndreas Gohr protected $undefined = []; 268ea5685fSAndreas Gohr 27077c27b2SAndreas Gohr /** @var array all metadata */ 28077c27b2SAndreas Gohr protected $metadata; 29077c27b2SAndreas Gohr /** @var array all default settings */ 30077c27b2SAndreas Gohr protected $default; 31077c27b2SAndreas Gohr /** @var array all local settings */ 32077c27b2SAndreas Gohr protected $local; 33077c27b2SAndreas Gohr /** @var array all protected settings */ 34077c27b2SAndreas Gohr protected $protected; 35077c27b2SAndreas Gohr 36077c27b2SAndreas Gohr /** @var bool have the settings been changed since loading from disk? */ 37077c27b2SAndreas Gohr protected $changed = false; 38077c27b2SAndreas Gohr 395675a07cSAndreas Gohr /** @var Loader */ 405675a07cSAndreas Gohr protected $loader; 41077c27b2SAndreas Gohr /** @var Writer */ 42077c27b2SAndreas Gohr protected $writer; 438ea5685fSAndreas Gohr 44c6639e6aSAndreas Gohr /** 45077c27b2SAndreas Gohr * ConfigSettings constructor. 46c6639e6aSAndreas Gohr */ 47*d868eb89SAndreas Gohr public function __construct() 48*d868eb89SAndreas Gohr { 495675a07cSAndreas Gohr $this->loader = new Loader(new ConfigParser()); 50077c27b2SAndreas Gohr $this->writer = new Writer(); 51077c27b2SAndreas Gohr 525675a07cSAndreas Gohr $this->metadata = $this->loader->loadMeta(); 535675a07cSAndreas Gohr $this->default = $this->loader->loadDefaults(); 545675a07cSAndreas Gohr $this->local = $this->loader->loadLocal(); 555675a07cSAndreas Gohr $this->protected = $this->loader->loadProtected(); 56077c27b2SAndreas Gohr 57077c27b2SAndreas Gohr $this->initSettings(); 58c6639e6aSAndreas Gohr } 59c6639e6aSAndreas Gohr 60c6639e6aSAndreas Gohr /** 61077c27b2SAndreas Gohr * Get all settings 62c6639e6aSAndreas Gohr * 63077c27b2SAndreas Gohr * @return Setting[] 64c6639e6aSAndreas Gohr */ 65*d868eb89SAndreas Gohr public function getSettings() 66*d868eb89SAndreas Gohr { 67077c27b2SAndreas Gohr return $this->settings; 68c6639e6aSAndreas Gohr } 69c6639e6aSAndreas Gohr 70077c27b2SAndreas Gohr /** 717a0ee538SAndreas Gohr * Get all unknown or problematic settings 72077c27b2SAndreas Gohr * 73077c27b2SAndreas Gohr * @return Setting[] 74077c27b2SAndreas Gohr */ 75*d868eb89SAndreas Gohr public function getUndefined() 76*d868eb89SAndreas Gohr { 77077c27b2SAndreas Gohr return $this->undefined; 78c6639e6aSAndreas Gohr } 79c6639e6aSAndreas Gohr 80077c27b2SAndreas Gohr /** 81077c27b2SAndreas Gohr * Have the settings been changed since loading from disk? 82077c27b2SAndreas Gohr * 83077c27b2SAndreas Gohr * @return bool 84077c27b2SAndreas Gohr */ 85*d868eb89SAndreas Gohr public function hasChanged() 86*d868eb89SAndreas Gohr { 87077c27b2SAndreas Gohr return $this->changed; 88077c27b2SAndreas Gohr } 89077c27b2SAndreas Gohr 90077c27b2SAndreas Gohr /** 91077c27b2SAndreas Gohr * Check if the config can be written 92077c27b2SAndreas Gohr * 93077c27b2SAndreas Gohr * @return bool 94077c27b2SAndreas Gohr */ 95*d868eb89SAndreas Gohr public function isLocked() 96*d868eb89SAndreas Gohr { 97077c27b2SAndreas Gohr return $this->writer->isLocked(); 98077c27b2SAndreas Gohr } 99077c27b2SAndreas Gohr 100077c27b2SAndreas Gohr /** 101077c27b2SAndreas Gohr * Update the settings using the data provided 102077c27b2SAndreas Gohr * 103077c27b2SAndreas Gohr * @param array $input as posted 104077c27b2SAndreas Gohr * @return bool true if all updates went through, false on errors 105077c27b2SAndreas Gohr */ 106*d868eb89SAndreas Gohr public function updateSettings($input) 107*d868eb89SAndreas Gohr { 108077c27b2SAndreas Gohr $ok = true; 109077c27b2SAndreas Gohr 110077c27b2SAndreas Gohr foreach ($this->settings as $key => $obj) { 111467c1427SAndreas Gohr $value = $input[$key] ?? null; 112077c27b2SAndreas Gohr if ($obj->update($value)) { 113077c27b2SAndreas Gohr $this->changed = true; 114077c27b2SAndreas Gohr } 1155c17d2d3SAndreas Gohr if ($obj->hasError()) $ok = false; 116077c27b2SAndreas Gohr } 117077c27b2SAndreas Gohr 118077c27b2SAndreas Gohr return $ok; 119077c27b2SAndreas Gohr } 120077c27b2SAndreas Gohr 121077c27b2SAndreas Gohr /** 122077c27b2SAndreas Gohr * Save the settings 123077c27b2SAndreas Gohr * 12453f3816eSAndreas Gohr * This save the current state as defined in this object, including the 12553f3816eSAndreas Gohr * undefined settings 12653f3816eSAndreas Gohr * 127077c27b2SAndreas Gohr * @throws \Exception 128077c27b2SAndreas Gohr */ 129*d868eb89SAndreas Gohr public function save() 130*d868eb89SAndreas Gohr { 1317a0ee538SAndreas Gohr // only save the undefined settings that have not been handled in settings 1327a0ee538SAndreas Gohr $undefined = array_diff_key($this->undefined, $this->settings); 1337a0ee538SAndreas Gohr $this->writer->save(array_merge($this->settings, $undefined)); 134077c27b2SAndreas Gohr } 135077c27b2SAndreas Gohr 136077c27b2SAndreas Gohr /** 137077c27b2SAndreas Gohr * Touch the settings 138077c27b2SAndreas Gohr * 139077c27b2SAndreas Gohr * @throws \Exception 140077c27b2SAndreas Gohr */ 141*d868eb89SAndreas Gohr public function touch() 142*d868eb89SAndreas Gohr { 143077c27b2SAndreas Gohr $this->writer->touch(); 144077c27b2SAndreas Gohr } 145077c27b2SAndreas Gohr 146077c27b2SAndreas Gohr /** 1475675a07cSAndreas Gohr * Load the extension language strings 1485675a07cSAndreas Gohr * 1495675a07cSAndreas Gohr * @return array 1505675a07cSAndreas Gohr */ 151*d868eb89SAndreas Gohr public function getLangs() 152*d868eb89SAndreas Gohr { 1535675a07cSAndreas Gohr return $this->loader->loadLangs(); 1545675a07cSAndreas Gohr } 1555675a07cSAndreas Gohr 1565675a07cSAndreas Gohr /** 157077c27b2SAndreas Gohr * Initalizes the $settings and $undefined properties 158077c27b2SAndreas Gohr */ 159*d868eb89SAndreas Gohr protected function initSettings() 160*d868eb89SAndreas Gohr { 161467c1427SAndreas Gohr $keys = [ 162467c1427SAndreas Gohr ...array_keys($this->metadata), 163467c1427SAndreas Gohr ...array_keys($this->default), 164467c1427SAndreas Gohr ...array_keys($this->local), 165467c1427SAndreas Gohr ...array_keys($this->protected) 166467c1427SAndreas Gohr ]; 167077c27b2SAndreas Gohr $keys = array_unique($keys); 168077c27b2SAndreas Gohr 169077c27b2SAndreas Gohr foreach ($keys as $key) { 170077c27b2SAndreas Gohr $obj = $this->instantiateClass($key); 171077c27b2SAndreas Gohr 172077c27b2SAndreas Gohr if ($obj->shouldHaveDefault() && !isset($this->default[$key])) { 1737a0ee538SAndreas Gohr $this->undefined[$key] = new SettingNoDefault($key); 174077c27b2SAndreas Gohr } 175077c27b2SAndreas Gohr 176467c1427SAndreas Gohr $d = $this->default[$key] ?? null; 177467c1427SAndreas Gohr $l = $this->local[$key] ?? null; 178467c1427SAndreas Gohr $p = $this->protected[$key] ?? null; 179077c27b2SAndreas Gohr 180077c27b2SAndreas Gohr $obj->initialize($d, $l, $p); 181077c27b2SAndreas Gohr } 182077c27b2SAndreas Gohr } 183077c27b2SAndreas Gohr 184077c27b2SAndreas Gohr /** 185077c27b2SAndreas Gohr * Instantiates the proper class for the given config key 186077c27b2SAndreas Gohr * 187077c27b2SAndreas Gohr * The class is added to the $settings or $undefined arrays and returned 188077c27b2SAndreas Gohr * 189077c27b2SAndreas Gohr * @param string $key 190077c27b2SAndreas Gohr * @return Setting 191077c27b2SAndreas Gohr */ 192*d868eb89SAndreas Gohr protected function instantiateClass($key) 193*d868eb89SAndreas Gohr { 194077c27b2SAndreas Gohr if (isset($this->metadata[$key])) { 195077c27b2SAndreas Gohr $param = $this->metadata[$key]; 196077c27b2SAndreas Gohr $class = $this->determineClassName(array_shift($param), $key); // first param is class 197077c27b2SAndreas Gohr $obj = new $class($key, $param); 198d6987bddSAndreas Gohr $this->settings[$key] = $obj; 199077c27b2SAndreas Gohr } else { 200077c27b2SAndreas Gohr $obj = new SettingUndefined($key); 201d6987bddSAndreas Gohr $this->undefined[$key] = $obj; 202077c27b2SAndreas Gohr } 203077c27b2SAndreas Gohr return $obj; 204077c27b2SAndreas Gohr } 205077c27b2SAndreas Gohr 206077c27b2SAndreas Gohr /** 207077c27b2SAndreas Gohr * Return the class to load 208077c27b2SAndreas Gohr * 209077c27b2SAndreas Gohr * @param string $class the class name as given in the meta file 210077c27b2SAndreas Gohr * @param string $key the settings key 211077c27b2SAndreas Gohr * @return string 212077c27b2SAndreas Gohr */ 213*d868eb89SAndreas Gohr protected function determineClassName($class, $key) 214*d868eb89SAndreas Gohr { 215077c27b2SAndreas Gohr // try namespaced class first 216b71f2463SAndreas Gohr if (is_string($class)) { 217077c27b2SAndreas Gohr $modern = str_replace('_', '', ucwords($class, '_')); 2180a5b05ebSAndreas Gohr $modern = '\\dokuwiki\\plugin\\config\\core\\Setting\\Setting' . $modern; 219077c27b2SAndreas Gohr if ($modern && class_exists($modern)) return $modern; 2200a5b05ebSAndreas Gohr // try class as given 2210a5b05ebSAndreas Gohr if (class_exists($class)) return $class; 222077c27b2SAndreas Gohr // class wasn't found add to errors 2237a0ee538SAndreas Gohr $this->undefined[$key] = new SettingNoKnownClass($key); 224077c27b2SAndreas Gohr } else { 225077c27b2SAndreas Gohr // no class given, add to errors 2267a0ee538SAndreas Gohr $this->undefined[$key] = new SettingNoClass($key); 227077c27b2SAndreas Gohr } 2280a5b05ebSAndreas Gohr return '\\dokuwiki\\plugin\\config\\core\\Setting\\Setting'; 229077c27b2SAndreas Gohr } 230077c27b2SAndreas Gohr} 231