123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- namespace dokuwiki\plugin\config\core;
- use dokuwiki\Extension\Event;
- /**
- * Configuration loader
- *
- * Loads configuration meta data and settings from the various files. Honors the
- * configuration cascade and installed plugins.
- */
- class Loader {
- /** @var ConfigParser */
- protected $parser;
- /** @var string[] list of enabled plugins */
- protected $plugins;
- /** @var string current template */
- protected $template;
- /**
- * Loader constructor.
- * @param ConfigParser $parser
- * @triggers PLUGIN_CONFIG_PLUGINLIST
- */
- public function __construct(ConfigParser $parser) {
- global $conf;
- $this->parser = $parser;
- $this->plugins = plugin_list();
- $this->template = $conf['template'];
- // allow plugins to remove configurable plugins
- Event::createAndTrigger('PLUGIN_CONFIG_PLUGINLIST', $this->plugins);
- }
- /**
- * Read the settings meta data
- *
- * Reads the main file, plugins and template settings meta data
- *
- * @return array
- */
- public function loadMeta() {
- // load main file
- $meta = array();
- include DOKU_PLUGIN . 'config/settings/config.metadata.php';
- // plugins
- foreach($this->plugins as $plugin) {
- $meta = array_merge(
- $meta,
- $this->loadExtensionMeta(
- DOKU_PLUGIN . $plugin . '/conf/metadata.php',
- 'plugin',
- $plugin
- )
- );
- }
- // current template
- $meta = array_merge(
- $meta,
- $this->loadExtensionMeta(
- tpl_incdir() . '/conf/metadata.php',
- 'tpl',
- $this->template
- )
- );
- return $meta;
- }
- /**
- * Read the default values
- *
- * Reads the main file, plugins and template defaults
- *
- * @return array
- */
- public function loadDefaults()
- {
- // initialize array
- $conf = array();
- // plugins
- foreach ($this->plugins as $plugin) {
- $conf = array_merge(
- $conf,
- $this->loadExtensionConf(
- DOKU_PLUGIN . $plugin . '/conf/default.php',
- 'plugin',
- $plugin
- )
- );
- }
- // current template
- $conf = array_merge(
- $conf,
- $this->loadExtensionConf(
- tpl_incdir() . '/conf/default.php',
- 'tpl',
- $this->template
- )
- );
- // load main files
- global $config_cascade;
- return array_merge(
- $conf,
- $this->loadConfigs($config_cascade['main']['default'])
- );
- }
- /**
- * Reads the language strings
- *
- * Only reads extensions, main one is loaded the usual way
- *
- * @return array
- */
- public function loadLangs() {
- $lang = array();
- // plugins
- foreach($this->plugins as $plugin) {
- $lang = array_merge(
- $lang,
- $this->loadExtensionLang(
- DOKU_PLUGIN . $plugin . '/',
- 'plugin',
- $plugin
- )
- );
- }
- // current template
- $lang = array_merge(
- $lang,
- $this->loadExtensionLang(
- tpl_incdir() . '/',
- 'tpl',
- $this->template
- )
- );
- return $lang;
- }
- /**
- * Read the local settings
- *
- * @return array
- */
- public function loadLocal() {
- global $config_cascade;
- return $this->loadConfigs($config_cascade['main']['local']);
- }
- /**
- * Read the protected settings
- *
- * @return array
- */
- public function loadProtected() {
- global $config_cascade;
- return $this->loadConfigs($config_cascade['main']['protected']);
- }
- /**
- * Read the config values from the given files
- *
- * @param string[] $files paths to config php's
- * @return array
- */
- protected function loadConfigs($files) {
- $conf = array();
- foreach($files as $file) {
- $conf = array_merge($conf, $this->parser->parse($file));
- }
- return $conf;
- }
- /**
- * Read settings file from an extension
- *
- * This is used to read the settings.php files of plugins and templates
- *
- * @param string $file php file to read
- * @param string $type should be 'plugin' or 'tpl'
- * @param string $extname name of the extension
- * @return array
- */
- protected function loadExtensionMeta($file, $type, $extname) {
- if(!file_exists($file)) return array();
- $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
- // include file
- $meta = array();
- include $file;
- if(empty($meta)) return array();
- // read data
- $data = array();
- $data[$prefix . $type . '_settings_name'] = ['fieldset'];
- foreach($meta as $key => $value) {
- if($value[0] == 'fieldset') continue; //plugins only get one fieldset
- $data[$prefix . $key] = $value;
- }
- return $data;
- }
- /**
- * Read a default file from an extension
- *
- * This is used to read the default.php files of plugins and templates
- *
- * @param string $file php file to read
- * @param string $type should be 'plugin' or 'tpl'
- * @param string $extname name of the extension
- * @return array
- */
- protected function loadExtensionConf($file, $type, $extname) {
- if(!file_exists($file)) return array();
- $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
- // parse file
- $conf = $this->parser->parse($file);
- if(empty($conf)) return array();
- // read data
- $data = array();
- foreach($conf as $key => $value) {
- $data[$prefix . $key] = $value;
- }
- return $data;
- }
- /**
- * Read the language file of an extension
- *
- * @param string $dir directory of the extension
- * @param string $type should be 'plugin' or 'tpl'
- * @param string $extname name of the extension
- * @return array
- */
- protected function loadExtensionLang($dir, $type, $extname) {
- global $conf;
- $ll = $conf['lang'];
- $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
- // include files
- $lang = array();
- if(file_exists($dir . 'lang/en/settings.php')) {
- include $dir . 'lang/en/settings.php';
- }
- if($ll != 'en' && file_exists($dir . 'lang/' . $ll . '/settings.php')) {
- include $dir . 'lang/' . $ll . '/settings.php';
- }
- // set up correct keys
- $strings = array();
- foreach($lang as $key => $val) {
- $strings[$prefix . $key] = $val;
- }
- // add fieldset key
- $strings[$prefix . $type . '_settings_name'] = ucwords(str_replace('_', ' ', $extname));
- return $strings;
- }
- }
|