xref: /dokuwiki/lib/plugins/config/core/Configuration.php (revision 8c7c53b0321a3cd3116b8d3b2ad27863a38dece7)
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 */
18*8c7c53b0SAndreas Gohrclass Configuration
19*8c7c53b0SAndreas Gohr{
20c6639e6aSAndreas Gohr
21bf9be0e3SAndreas Gohr    public const KEYMARKER = '____';
22c6639e6aSAndreas Gohr
23077c27b2SAndreas Gohr    /** @var Setting[] metadata as array of Settings objects */
24467c1427SAndreas Gohr    protected $settings = [];
257a0ee538SAndreas Gohr    /** @var Setting[] undefined and problematic settings */
26467c1427SAndreas Gohr    protected $undefined = [];
278ea5685fSAndreas Gohr
28077c27b2SAndreas Gohr    /** @var array all metadata */
29077c27b2SAndreas Gohr    protected $metadata;
30077c27b2SAndreas Gohr    /** @var array all default settings */
31077c27b2SAndreas Gohr    protected $default;
32077c27b2SAndreas Gohr    /** @var array all local settings */
33077c27b2SAndreas Gohr    protected $local;
34077c27b2SAndreas Gohr    /** @var array all protected settings */
35077c27b2SAndreas Gohr    protected $protected;
36077c27b2SAndreas Gohr
37077c27b2SAndreas Gohr    /** @var bool have the settings been changed since loading from disk? */
38077c27b2SAndreas Gohr    protected $changed = false;
39077c27b2SAndreas Gohr
405675a07cSAndreas Gohr    /** @var Loader */
415675a07cSAndreas Gohr    protected $loader;
42077c27b2SAndreas Gohr    /** @var Writer */
43077c27b2SAndreas Gohr    protected $writer;
448ea5685fSAndreas Gohr
45c6639e6aSAndreas Gohr    /**
46077c27b2SAndreas Gohr     * ConfigSettings constructor.
47c6639e6aSAndreas Gohr     */
485a38a129SAndreas Gohr    public function __construct() {
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     */
65077c27b2SAndreas Gohr    public function getSettings() {
66077c27b2SAndreas Gohr        return $this->settings;
67c6639e6aSAndreas Gohr    }
68c6639e6aSAndreas Gohr
69077c27b2SAndreas Gohr    /**
707a0ee538SAndreas Gohr     * Get all unknown or problematic settings
71077c27b2SAndreas Gohr     *
72077c27b2SAndreas Gohr     * @return Setting[]
73077c27b2SAndreas Gohr     */
74077c27b2SAndreas Gohr    public function getUndefined() {
75077c27b2SAndreas Gohr        return $this->undefined;
76c6639e6aSAndreas Gohr    }
77c6639e6aSAndreas Gohr
78077c27b2SAndreas Gohr    /**
79077c27b2SAndreas Gohr     * Have the settings been changed since loading from disk?
80077c27b2SAndreas Gohr     *
81077c27b2SAndreas Gohr     * @return bool
82077c27b2SAndreas Gohr     */
83077c27b2SAndreas Gohr    public function hasChanged() {
84077c27b2SAndreas Gohr        return $this->changed;
85077c27b2SAndreas Gohr    }
86077c27b2SAndreas Gohr
87077c27b2SAndreas Gohr    /**
88077c27b2SAndreas Gohr     * Check if the config can be written
89077c27b2SAndreas Gohr     *
90077c27b2SAndreas Gohr     * @return bool
91077c27b2SAndreas Gohr     */
92077c27b2SAndreas Gohr    public function isLocked() {
93077c27b2SAndreas Gohr        return $this->writer->isLocked();
94077c27b2SAndreas Gohr    }
95077c27b2SAndreas Gohr
96077c27b2SAndreas Gohr    /**
97077c27b2SAndreas Gohr     * Update the settings using the data provided
98077c27b2SAndreas Gohr     *
99077c27b2SAndreas Gohr     * @param array $input as posted
100077c27b2SAndreas Gohr     * @return bool true if all updates went through, false on errors
101077c27b2SAndreas Gohr     */
102077c27b2SAndreas Gohr    public function updateSettings($input) {
103077c27b2SAndreas Gohr        $ok = true;
104077c27b2SAndreas Gohr
105077c27b2SAndreas Gohr        foreach($this->settings as $key => $obj) {
106467c1427SAndreas Gohr            $value = $input[$key] ?? null;
107077c27b2SAndreas Gohr            if($obj->update($value)) {
108077c27b2SAndreas Gohr                $this->changed = true;
109077c27b2SAndreas Gohr            }
1105c17d2d3SAndreas Gohr            if($obj->hasError()) $ok = false;
111077c27b2SAndreas Gohr        }
112077c27b2SAndreas Gohr
113077c27b2SAndreas Gohr        return $ok;
114077c27b2SAndreas Gohr    }
115077c27b2SAndreas Gohr
116077c27b2SAndreas Gohr    /**
117077c27b2SAndreas Gohr     * Save the settings
118077c27b2SAndreas Gohr     *
11953f3816eSAndreas Gohr     * This save the current state as defined in this object, including the
12053f3816eSAndreas Gohr     * undefined settings
12153f3816eSAndreas Gohr     *
122077c27b2SAndreas Gohr     * @throws \Exception
123077c27b2SAndreas Gohr     */
124077c27b2SAndreas Gohr    public function save() {
1257a0ee538SAndreas Gohr        // only save the undefined settings that have not been handled in settings
1267a0ee538SAndreas Gohr        $undefined = array_diff_key($this->undefined, $this->settings);
1277a0ee538SAndreas Gohr        $this->writer->save(array_merge($this->settings, $undefined));
128077c27b2SAndreas Gohr    }
129077c27b2SAndreas Gohr
130077c27b2SAndreas Gohr    /**
131077c27b2SAndreas Gohr     * Touch the settings
132077c27b2SAndreas Gohr     *
133077c27b2SAndreas Gohr     * @throws \Exception
134077c27b2SAndreas Gohr     */
135077c27b2SAndreas Gohr    public function touch() {
136077c27b2SAndreas Gohr        $this->writer->touch();
137077c27b2SAndreas Gohr    }
138077c27b2SAndreas Gohr
139077c27b2SAndreas Gohr    /**
1405675a07cSAndreas Gohr     * Load the extension language strings
1415675a07cSAndreas Gohr     *
1425675a07cSAndreas Gohr     * @return array
1435675a07cSAndreas Gohr     */
1445675a07cSAndreas Gohr    public function getLangs() {
1455675a07cSAndreas Gohr        return $this->loader->loadLangs();
1465675a07cSAndreas Gohr    }
1475675a07cSAndreas Gohr
1485675a07cSAndreas Gohr    /**
149077c27b2SAndreas Gohr     * Initalizes the $settings and $undefined properties
150077c27b2SAndreas Gohr     */
151077c27b2SAndreas Gohr    protected function initSettings() {
152467c1427SAndreas Gohr        $keys = [
153467c1427SAndreas Gohr            ...array_keys($this->metadata),
154467c1427SAndreas Gohr            ...array_keys($this->default),
155467c1427SAndreas Gohr            ...array_keys($this->local),
156467c1427SAndreas Gohr            ...array_keys($this->protected)
157467c1427SAndreas Gohr        ];
158077c27b2SAndreas Gohr        $keys = array_unique($keys);
159077c27b2SAndreas Gohr
160077c27b2SAndreas Gohr        foreach($keys as $key) {
161077c27b2SAndreas Gohr            $obj = $this->instantiateClass($key);
162077c27b2SAndreas Gohr
163077c27b2SAndreas Gohr            if($obj->shouldHaveDefault() && !isset($this->default[$key])) {
1647a0ee538SAndreas Gohr                $this->undefined[$key] = new SettingNoDefault($key);
165077c27b2SAndreas Gohr            }
166077c27b2SAndreas Gohr
167467c1427SAndreas Gohr            $d = $this->default[$key] ?? null;
168467c1427SAndreas Gohr            $l = $this->local[$key] ?? null;
169467c1427SAndreas Gohr            $p = $this->protected[$key] ?? null;
170077c27b2SAndreas Gohr
171077c27b2SAndreas Gohr            $obj->initialize($d, $l, $p);
172077c27b2SAndreas Gohr        }
173077c27b2SAndreas Gohr    }
174077c27b2SAndreas Gohr
175077c27b2SAndreas Gohr    /**
176077c27b2SAndreas Gohr     * Instantiates the proper class for the given config key
177077c27b2SAndreas Gohr     *
178077c27b2SAndreas Gohr     * The class is added to the $settings or $undefined arrays and returned
179077c27b2SAndreas Gohr     *
180077c27b2SAndreas Gohr     * @param string $key
181077c27b2SAndreas Gohr     * @return Setting
182077c27b2SAndreas Gohr     */
183077c27b2SAndreas Gohr    protected function instantiateClass($key) {
184077c27b2SAndreas Gohr        if(isset($this->metadata[$key])) {
185077c27b2SAndreas Gohr            $param = $this->metadata[$key];
186077c27b2SAndreas Gohr            $class = $this->determineClassName(array_shift($param), $key); // first param is class
187077c27b2SAndreas Gohr            $obj = new $class($key, $param);
188d6987bddSAndreas Gohr            $this->settings[$key] = $obj;
189077c27b2SAndreas Gohr        } else {
190077c27b2SAndreas Gohr            $obj = new SettingUndefined($key);
191d6987bddSAndreas Gohr            $this->undefined[$key] = $obj;
192077c27b2SAndreas Gohr        }
193077c27b2SAndreas Gohr        return $obj;
194077c27b2SAndreas Gohr    }
195077c27b2SAndreas Gohr
196077c27b2SAndreas Gohr    /**
197077c27b2SAndreas Gohr     * Return the class to load
198077c27b2SAndreas Gohr     *
199077c27b2SAndreas Gohr     * @param string $class the class name as given in the meta file
200077c27b2SAndreas Gohr     * @param string $key the settings key
201077c27b2SAndreas Gohr     * @return string
202077c27b2SAndreas Gohr     */
203077c27b2SAndreas Gohr    protected function determineClassName($class, $key) {
204077c27b2SAndreas Gohr        // try namespaced class first
205b71f2463SAndreas Gohr        if(is_string($class)) {
206077c27b2SAndreas Gohr            $modern = str_replace('_', '', ucwords($class, '_'));
2070a5b05ebSAndreas Gohr            $modern = '\\dokuwiki\\plugin\\config\\core\\Setting\\Setting' . $modern;
208077c27b2SAndreas Gohr            if($modern && class_exists($modern)) return $modern;
2090a5b05ebSAndreas Gohr            // try class as given
2100a5b05ebSAndreas Gohr            if(class_exists($class)) return $class;
211077c27b2SAndreas Gohr            // class wasn't found add to errors
2127a0ee538SAndreas Gohr            $this->undefined[$key] = new SettingNoKnownClass($key);
213077c27b2SAndreas Gohr        } else {
214077c27b2SAndreas Gohr            // no class given, add to errors
2157a0ee538SAndreas Gohr            $this->undefined[$key] = new SettingNoClass($key);
216077c27b2SAndreas Gohr        }
2170a5b05ebSAndreas Gohr        return '\\dokuwiki\\plugin\\config\\core\\Setting\\Setting';
218077c27b2SAndreas Gohr    }
219077c27b2SAndreas Gohr}
220