Failed Conditions
Pull Request — psr2 (#2382)
by Andreas
08:02 queued 12s
created

Loader   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 257
rs 10
wmc 25
lcom 1
cbo 1

10 Methods

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