1<?php 2 3namespace 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 */ 11class 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 * Read the local settings 107 * 108 * @return array 109 */ 110 public function loadLocal() { 111 global $config_cascade; 112 return $this->loadConfigs($config_cascade['main']['local']); 113 } 114 115 /** 116 * Read the protected settings 117 * 118 * @return array 119 */ 120 public function loadProtected() { 121 global $config_cascade; 122 return $this->loadConfigs($config_cascade['main']['protected']); 123 } 124 125 /** 126 * Read the config values from the given files 127 * 128 * @param string[] $files paths to config php's 129 * @return array 130 */ 131 protected function loadConfigs($files) { 132 $conf = array(); 133 foreach($files as $file) { 134 $conf = array_merge($conf, $this->parser->parse($file)); 135 } 136 return $conf; 137 } 138 139 /** 140 * Read settings file from an extension 141 * 142 * This is used to read the settings.php files of plugins and templates 143 * 144 * @param string $file php file to read 145 * @param string $type should be 'plugin' or 'tpl' 146 * @param string $extname name of the extension 147 * @return array 148 */ 149 protected function loadExtensionMeta($file, $type, $extname) { 150 if(!file_exists($file)) return array(); 151 $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER; 152 153 // include file 154 $meta = array(); 155 include $file; 156 if(empty($meta)) return array(); 157 158 // read data 159 $data = array(); 160 $data[$prefix . $type . '_settings_name'] = ['fieldset']; 161 foreach($meta as $key => $value) { 162 if($value[0] == 'fieldset') continue; //plugins only get one fieldset 163 $data[$prefix . $key] = $value; 164 } 165 166 return $data; 167 } 168 169 /** 170 * Read a default file from an extension 171 * 172 * This is used to read the default.php files of plugins and templates 173 * 174 * @param string $file php file to read 175 * @param string $type should be 'plugin' or 'tpl' 176 * @param string $extname name of the extension 177 * @return array 178 */ 179 protected function loadExtensionConf($file, $type, $extname) { 180 if(!file_exists($file)) return array(); 181 $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER; 182 183 // parse file 184 $conf = $this->parser->parse($file); 185 if(empty($conf)) return array(); 186 187 // read data 188 $data = array(); 189 foreach($conf as $key => $value) { 190 $data[$prefix . $key] = $value; 191 } 192 193 return $data; 194 } 195} 196