[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/lib/plugins/config/core/ -> Loader.php (source)

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