xref: /dokuwiki/lib/plugins/config/core/Loader.php (revision d2a8ea656a16abfd37fe4ffb58d1cf7e4ddf2e15)
1<?php
2
3namespace dokuwiki\plugin\config\core;
4
5use dokuwiki\Extension\Event;
6
7/**
8 * Configuration loader
9 *
10 * Loads configuration meta data and settings from the various files. Honors the
11 * configuration cascade and installed plugins.
12 */
13class Loader {
14    /** @var ConfigParser */
15    protected $parser;
16
17    /** @var string[] list of enabled plugins */
18    protected $plugins;
19    /** @var string current template */
20    protected $template;
21
22    /**
23     * Loader constructor.
24     * @param ConfigParser $parser
25     * @triggers PLUGIN_CONFIG_PLUGINLIST
26     */
27    public function __construct(ConfigParser $parser) {
28        global $conf;
29        $this->parser = $parser;
30        $this->plugins = plugin_list('', false);
31        sort($this->plugins, SORT_REGULAR);
32        $this->template = $conf['template'];
33        // allow plugins to remove configurable plugins
34        Event::createAndTrigger('PLUGIN_CONFIG_PLUGINLIST', $this->plugins);
35    }
36
37    /**
38     * Read the settings meta data
39     *
40     * Reads the main file, plugins and template settings meta data
41     *
42     * @return array
43     */
44    public function loadMeta() {
45        // load main file
46        $meta = array();
47        include DOKU_PLUGIN . 'config/settings/config.metadata.php';
48
49        // plugins
50        foreach($this->plugins as $plugin) {
51            $meta = array_merge(
52                $meta,
53                $this->loadExtensionMeta(
54                    DOKU_PLUGIN . $plugin . '/conf/metadata.php',
55                    'plugin',
56                    $plugin
57                )
58            );
59        }
60
61        // current template
62        $meta = array_merge(
63            $meta,
64            $this->loadExtensionMeta(
65                tpl_incdir() . '/conf/metadata.php',
66                'tpl',
67                $this->template
68            )
69        );
70
71        return $meta;
72    }
73
74    /**
75     * Read the default values
76     *
77     * Reads the main file, plugins and template defaults
78     *
79     * @return array
80     */
81    public function loadDefaults() {
82        // load main files
83        global $config_cascade;
84        $conf = $this->loadConfigs($config_cascade['main']['default']);
85
86        // plugins
87        foreach($this->plugins as $plugin) {
88            $conf = array_merge(
89                $conf,
90                $this->loadExtensionConf(
91                    DOKU_PLUGIN . $plugin . '/conf/default.php',
92                    'plugin',
93                    $plugin
94                )
95            );
96        }
97
98        // current template
99        $conf = array_merge(
100            $conf,
101            $this->loadExtensionConf(
102                tpl_incdir() . '/conf/default.php',
103                'tpl',
104                $this->template
105            )
106        );
107
108        return $conf;
109    }
110
111    /**
112     * Reads the language strings
113     *
114     * Only reads extensions, main one is loaded the usual way
115     *
116     * @return array
117     */
118    public function loadLangs() {
119        $lang = array();
120
121        // plugins
122        foreach($this->plugins as $plugin) {
123            $lang = array_merge(
124                $lang,
125                $this->loadExtensionLang(
126                    DOKU_PLUGIN . $plugin . '/',
127                    'plugin',
128                    $plugin
129                )
130            );
131        }
132
133        // current template
134        $lang = array_merge(
135            $lang,
136            $this->loadExtensionLang(
137                tpl_incdir() . '/',
138                'tpl',
139                $this->template
140            )
141        );
142
143        return $lang;
144    }
145
146    /**
147     * Read the local settings
148     *
149     * @return array
150     */
151    public function loadLocal() {
152        global $config_cascade;
153        return $this->loadConfigs($config_cascade['main']['local']);
154    }
155
156    /**
157     * Read the protected settings
158     *
159     * @return array
160     */
161    public function loadProtected() {
162        global $config_cascade;
163        return $this->loadConfigs($config_cascade['main']['protected']);
164    }
165
166    /**
167     * Read the config values from the given files
168     *
169     * @param string[] $files paths to config php's
170     * @return array
171     */
172    protected function loadConfigs($files) {
173        $conf = array();
174        foreach($files as $file) {
175            $conf = array_merge($conf, $this->parser->parse($file));
176        }
177        return $conf;
178    }
179
180    /**
181     * Read settings file from an extension
182     *
183     * This is used to read the settings.php files of plugins and templates
184     *
185     * @param string $file php file to read
186     * @param string $type should be 'plugin' or 'tpl'
187     * @param string $extname name of the extension
188     * @return array
189     */
190    protected function loadExtensionMeta($file, $type, $extname) {
191        if(!file_exists($file)) return array();
192        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
193
194        // include file
195        $meta = array();
196        include $file;
197        if(empty($meta)) return array();
198
199        // read data
200        $data = array();
201        $data[$prefix . $type . '_settings_name'] = ['fieldset'];
202        foreach($meta as $key => $value) {
203            if($value[0] == 'fieldset') continue; //plugins only get one fieldset
204            $data[$prefix . $key] = $value;
205        }
206
207        return $data;
208    }
209
210    /**
211     * Read a default file from an extension
212     *
213     * This is used to read the default.php files of plugins and templates
214     *
215     * @param string $file php file to read
216     * @param string $type should be 'plugin' or 'tpl'
217     * @param string $extname name of the extension
218     * @return array
219     */
220    protected function loadExtensionConf($file, $type, $extname) {
221        if(!file_exists($file)) return array();
222        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
223
224        // parse file
225        $conf = $this->parser->parse($file);
226        if(empty($conf)) return array();
227
228        // read data
229        $data = array();
230        foreach($conf as $key => $value) {
231            $data[$prefix . $key] = $value;
232        }
233
234        return $data;
235    }
236
237    /**
238     * Read the language file of an extension
239     *
240     * @param string $dir directory of the extension
241     * @param string $type should be 'plugin' or 'tpl'
242     * @param string $extname name of the extension
243     * @return array
244     */
245    protected function loadExtensionLang($dir, $type, $extname) {
246        global $conf;
247        $ll = $conf['lang'];
248        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
249
250        // include files
251        $lang = array();
252        if(file_exists($dir . 'lang/en/settings.php')) {
253            include $dir . 'lang/en/settings.php';
254        }
255        if($ll != 'en' && file_exists($dir . 'lang/' . $ll . '/settings.php')) {
256            include $dir . 'lang/' . $ll . '/settings.php';
257        }
258
259        // set up correct keys
260        $strings = array();
261        foreach($lang as $key => $val) {
262            $strings[$prefix . $key] = $val;
263        }
264
265        // add fieldset key
266        $strings[$prefix . $type . '_settings_name'] = ucwords(str_replace('_', ' ', $extname));
267
268        return $strings;
269    }
270}
271