Completed
Push — psr2-config ( 077c27...5675a0 )
by Andreas
06:18 queued 03:15
created

Loader::loadLangs()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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