xref: /dokuwiki/lib/plugins/config/core/Loader.php (revision 6850fd4acf3373d8e41a3299dbf6d5366c9edfe9)
1*6850fd4aSAndreas Gohr<?php
2*6850fd4aSAndreas Gohr
3*6850fd4aSAndreas Gohrnamespace dokuwiki\plugin\config\core;
4*6850fd4aSAndreas Gohr
5*6850fd4aSAndreas Gohr/**
6*6850fd4aSAndreas Gohr * Configuration loader
7*6850fd4aSAndreas Gohr *
8*6850fd4aSAndreas Gohr * Loads configuration meta data and settings from the various files. Honors the
9*6850fd4aSAndreas Gohr * configuration cascade and installed plugins.
10*6850fd4aSAndreas Gohr */
11*6850fd4aSAndreas Gohrclass Loader {
12*6850fd4aSAndreas Gohr    /** @var ConfigParser */
13*6850fd4aSAndreas Gohr    protected $parser;
14*6850fd4aSAndreas Gohr
15*6850fd4aSAndreas Gohr    /** @var string[] list of enabled plugins */
16*6850fd4aSAndreas Gohr    protected $plugins;
17*6850fd4aSAndreas Gohr    /** @var string current template */
18*6850fd4aSAndreas Gohr    protected $template;
19*6850fd4aSAndreas Gohr
20*6850fd4aSAndreas Gohr    /**
21*6850fd4aSAndreas Gohr     * Loader constructor.
22*6850fd4aSAndreas Gohr     * @param ConfigParser $parser
23*6850fd4aSAndreas Gohr     */
24*6850fd4aSAndreas Gohr    public function __construct(ConfigParser $parser) {
25*6850fd4aSAndreas Gohr        global $conf;
26*6850fd4aSAndreas Gohr        $this->parser = $parser;
27*6850fd4aSAndreas Gohr        $this->plugins = plugin_list();
28*6850fd4aSAndreas Gohr        $this->template = $conf['template'];
29*6850fd4aSAndreas Gohr    }
30*6850fd4aSAndreas Gohr
31*6850fd4aSAndreas Gohr    /**
32*6850fd4aSAndreas Gohr     * Read the settings meta data
33*6850fd4aSAndreas Gohr     *
34*6850fd4aSAndreas Gohr     * Reads the main file, plugins and template settings meta data
35*6850fd4aSAndreas Gohr     *
36*6850fd4aSAndreas Gohr     * @return array
37*6850fd4aSAndreas Gohr     */
38*6850fd4aSAndreas Gohr    public function loadMeta() {
39*6850fd4aSAndreas Gohr        // load main file
40*6850fd4aSAndreas Gohr        $meta = array();
41*6850fd4aSAndreas Gohr        include DOKU_PLUGIN . 'config/settings/config.metadata.php';
42*6850fd4aSAndreas Gohr
43*6850fd4aSAndreas Gohr        // plugins
44*6850fd4aSAndreas Gohr        foreach($this->plugins as $plugin) {
45*6850fd4aSAndreas Gohr            array_merge(
46*6850fd4aSAndreas Gohr                $meta,
47*6850fd4aSAndreas Gohr                $this->loadExtensionMeta(
48*6850fd4aSAndreas Gohr                    DOKU_PLUGIN . $plugin . '/conf/settings.php',
49*6850fd4aSAndreas Gohr                    'plugin',
50*6850fd4aSAndreas Gohr                    $plugin
51*6850fd4aSAndreas Gohr                )
52*6850fd4aSAndreas Gohr            );
53*6850fd4aSAndreas Gohr        }
54*6850fd4aSAndreas Gohr
55*6850fd4aSAndreas Gohr        // current template
56*6850fd4aSAndreas Gohr        array_merge(
57*6850fd4aSAndreas Gohr            $meta,
58*6850fd4aSAndreas Gohr            $this->loadExtensionMeta(
59*6850fd4aSAndreas Gohr                tpl_incdir() . '/conf/settings.php',
60*6850fd4aSAndreas Gohr                'tpl',
61*6850fd4aSAndreas Gohr                $this->template
62*6850fd4aSAndreas Gohr            )
63*6850fd4aSAndreas Gohr        );
64*6850fd4aSAndreas Gohr
65*6850fd4aSAndreas Gohr        return $meta;
66*6850fd4aSAndreas Gohr    }
67*6850fd4aSAndreas Gohr
68*6850fd4aSAndreas Gohr    /**
69*6850fd4aSAndreas Gohr     * Read the default values
70*6850fd4aSAndreas Gohr     *
71*6850fd4aSAndreas Gohr     * Reads the main file, plugins and template defaults
72*6850fd4aSAndreas Gohr     *
73*6850fd4aSAndreas Gohr     * @return array
74*6850fd4aSAndreas Gohr     */
75*6850fd4aSAndreas Gohr    public function loadDefaults() {
76*6850fd4aSAndreas Gohr        // load main files
77*6850fd4aSAndreas Gohr        global $config_cascade;
78*6850fd4aSAndreas Gohr        $conf = $this->loadConfigs($config_cascade['main']['default']);
79*6850fd4aSAndreas Gohr
80*6850fd4aSAndreas Gohr        // plugins
81*6850fd4aSAndreas Gohr        foreach($this->plugins as $plugin) {
82*6850fd4aSAndreas Gohr            array_merge(
83*6850fd4aSAndreas Gohr                $conf,
84*6850fd4aSAndreas Gohr                $this->loadExtensionConf(
85*6850fd4aSAndreas Gohr                    DOKU_PLUGIN . $plugin . '/conf/default.php',
86*6850fd4aSAndreas Gohr                    'plugin',
87*6850fd4aSAndreas Gohr                    $plugin
88*6850fd4aSAndreas Gohr                )
89*6850fd4aSAndreas Gohr            );
90*6850fd4aSAndreas Gohr        }
91*6850fd4aSAndreas Gohr
92*6850fd4aSAndreas Gohr        // current template
93*6850fd4aSAndreas Gohr        array_merge(
94*6850fd4aSAndreas Gohr            $conf,
95*6850fd4aSAndreas Gohr            $this->loadExtensionConf(
96*6850fd4aSAndreas Gohr                tpl_incdir() . '/conf/default.php',
97*6850fd4aSAndreas Gohr                'tpl',
98*6850fd4aSAndreas Gohr                $this->template
99*6850fd4aSAndreas Gohr            )
100*6850fd4aSAndreas Gohr        );
101*6850fd4aSAndreas Gohr
102*6850fd4aSAndreas Gohr        return $conf;
103*6850fd4aSAndreas Gohr    }
104*6850fd4aSAndreas Gohr
105*6850fd4aSAndreas Gohr    /**
106*6850fd4aSAndreas Gohr     * Read the local settings
107*6850fd4aSAndreas Gohr     *
108*6850fd4aSAndreas Gohr     * @return array
109*6850fd4aSAndreas Gohr     */
110*6850fd4aSAndreas Gohr    public function loadLocal() {
111*6850fd4aSAndreas Gohr        global $config_cascade;
112*6850fd4aSAndreas Gohr        return $this->loadConfigs($config_cascade['main']['local']);
113*6850fd4aSAndreas Gohr    }
114*6850fd4aSAndreas Gohr
115*6850fd4aSAndreas Gohr    /**
116*6850fd4aSAndreas Gohr     * Read the protected settings
117*6850fd4aSAndreas Gohr     *
118*6850fd4aSAndreas Gohr     * @return array
119*6850fd4aSAndreas Gohr     */
120*6850fd4aSAndreas Gohr    public function loadProtected() {
121*6850fd4aSAndreas Gohr        global $config_cascade;
122*6850fd4aSAndreas Gohr        return $this->loadConfigs($config_cascade['main']['protected']);
123*6850fd4aSAndreas Gohr    }
124*6850fd4aSAndreas Gohr
125*6850fd4aSAndreas Gohr    /**
126*6850fd4aSAndreas Gohr     * Read the config values from the given files
127*6850fd4aSAndreas Gohr     *
128*6850fd4aSAndreas Gohr     * @param string[] $files paths to config php's
129*6850fd4aSAndreas Gohr     * @return array
130*6850fd4aSAndreas Gohr     */
131*6850fd4aSAndreas Gohr    protected function loadConfigs($files) {
132*6850fd4aSAndreas Gohr        $conf = array();
133*6850fd4aSAndreas Gohr        foreach($files as $file) {
134*6850fd4aSAndreas Gohr            $conf = array_merge($conf, $this->parser->parse($file));
135*6850fd4aSAndreas Gohr        }
136*6850fd4aSAndreas Gohr        return $conf;
137*6850fd4aSAndreas Gohr    }
138*6850fd4aSAndreas Gohr
139*6850fd4aSAndreas Gohr    /**
140*6850fd4aSAndreas Gohr     * Read settings file from an extension
141*6850fd4aSAndreas Gohr     *
142*6850fd4aSAndreas Gohr     * This is used to read the settings.php files of plugins and templates
143*6850fd4aSAndreas Gohr     *
144*6850fd4aSAndreas Gohr     * @param string $file php file to read
145*6850fd4aSAndreas Gohr     * @param string $type should be 'plugin' or 'tpl'
146*6850fd4aSAndreas Gohr     * @param string $extname name of the extension
147*6850fd4aSAndreas Gohr     * @return array
148*6850fd4aSAndreas Gohr     */
149*6850fd4aSAndreas Gohr    protected function loadExtensionMeta($file, $type, $extname) {
150*6850fd4aSAndreas Gohr        if(!file_exists($file)) return array();
151*6850fd4aSAndreas Gohr        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
152*6850fd4aSAndreas Gohr
153*6850fd4aSAndreas Gohr        // include file
154*6850fd4aSAndreas Gohr        $meta = array();
155*6850fd4aSAndreas Gohr        include $file;
156*6850fd4aSAndreas Gohr        if(empty($meta)) return array();
157*6850fd4aSAndreas Gohr
158*6850fd4aSAndreas Gohr        // read data
159*6850fd4aSAndreas Gohr        $data = array();
160*6850fd4aSAndreas Gohr        $data[$prefix . $type . '_settings_name'] = ['fieldset'];
161*6850fd4aSAndreas Gohr        foreach($meta as $key => $value) {
162*6850fd4aSAndreas Gohr            if($value[0] == 'fieldset') continue; //plugins only get one fieldset
163*6850fd4aSAndreas Gohr            $data[$prefix . $key] = $value;
164*6850fd4aSAndreas Gohr        }
165*6850fd4aSAndreas Gohr
166*6850fd4aSAndreas Gohr        return $data;
167*6850fd4aSAndreas Gohr    }
168*6850fd4aSAndreas Gohr
169*6850fd4aSAndreas Gohr    /**
170*6850fd4aSAndreas Gohr     * Read a default file from an extension
171*6850fd4aSAndreas Gohr     *
172*6850fd4aSAndreas Gohr     * This is used to read the default.php files of plugins and templates
173*6850fd4aSAndreas Gohr     *
174*6850fd4aSAndreas Gohr     * @param string $file php file to read
175*6850fd4aSAndreas Gohr     * @param string $type should be 'plugin' or 'tpl'
176*6850fd4aSAndreas Gohr     * @param string $extname name of the extension
177*6850fd4aSAndreas Gohr     * @return array
178*6850fd4aSAndreas Gohr     */
179*6850fd4aSAndreas Gohr    protected function loadExtensionConf($file, $type, $extname) {
180*6850fd4aSAndreas Gohr        if(!file_exists($file)) return array();
181*6850fd4aSAndreas Gohr        $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
182*6850fd4aSAndreas Gohr
183*6850fd4aSAndreas Gohr        // parse file
184*6850fd4aSAndreas Gohr        $conf = $this->parser->parse($file);
185*6850fd4aSAndreas Gohr        if(empty($conf)) return array();
186*6850fd4aSAndreas Gohr
187*6850fd4aSAndreas Gohr        // read data
188*6850fd4aSAndreas Gohr        $data = array();
189*6850fd4aSAndreas Gohr        foreach($conf as $key => $value) {
190*6850fd4aSAndreas Gohr            $data[$prefix . $key] = $value;
191*6850fd4aSAndreas Gohr        }
192*6850fd4aSAndreas Gohr
193*6850fd4aSAndreas Gohr        return $data;
194*6850fd4aSAndreas Gohr    }
195*6850fd4aSAndreas Gohr}
196