[ 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      {
  82  
  83          // initialize array
  84          $conf = array();
  85  
  86          // plugins
  87          foreach ($this->plugins as $plugin) {
  88              $conf = array_merge(
  89                  $conf,
  90                  $this->loadExtensionConf(
  91                      DOKU_PLUGIN . $plugin . '/conf/default.php',
  92                      'plugin',
  93                      $plugin
  94                  )
  95              );
  96          }
  97  
  98          // current template
  99          $conf = array_merge(
 100              $conf,
 101              $this->loadExtensionConf(
 102                  tpl_incdir() . '/conf/default.php',
 103                  'tpl',
 104                  $this->template
 105              )
 106          );
 107  
 108          // load main files
 109          global $config_cascade;
 110          return array_merge(
 111              $conf,
 112              $this->loadConfigs($config_cascade['main']['default'])
 113          );
 114      }
 115  
 116      /**
 117       * Reads the language strings
 118       *
 119       * Only reads extensions, main one is loaded the usual way
 120       *
 121       * @return array
 122       */
 123      public function loadLangs() {
 124          $lang = array();
 125  
 126          // plugins
 127          foreach($this->plugins as $plugin) {
 128              $lang = array_merge(
 129                  $lang,
 130                  $this->loadExtensionLang(
 131                      DOKU_PLUGIN . $plugin . '/',
 132                      'plugin',
 133                      $plugin
 134                  )
 135              );
 136          }
 137  
 138          // current template
 139          $lang = array_merge(
 140              $lang,
 141              $this->loadExtensionLang(
 142                  tpl_incdir() . '/',
 143                  'tpl',
 144                  $this->template
 145              )
 146          );
 147  
 148          return $lang;
 149      }
 150  
 151      /**
 152       * Read the local settings
 153       *
 154       * @return array
 155       */
 156      public function loadLocal() {
 157          global $config_cascade;
 158          return $this->loadConfigs($config_cascade['main']['local']);
 159      }
 160  
 161      /**
 162       * Read the protected settings
 163       *
 164       * @return array
 165       */
 166      public function loadProtected() {
 167          global $config_cascade;
 168          return $this->loadConfigs($config_cascade['main']['protected']);
 169      }
 170  
 171      /**
 172       * Read the config values from the given files
 173       *
 174       * @param string[] $files paths to config php's
 175       * @return array
 176       */
 177      protected function loadConfigs($files) {
 178          $conf = array();
 179          foreach($files as $file) {
 180              $conf = array_merge($conf, $this->parser->parse($file));
 181          }
 182          return $conf;
 183      }
 184  
 185      /**
 186       * Read settings file from an extension
 187       *
 188       * This is used to read the settings.php files of plugins and templates
 189       *
 190       * @param string $file php file to read
 191       * @param string $type should be 'plugin' or 'tpl'
 192       * @param string $extname name of the extension
 193       * @return array
 194       */
 195      protected function loadExtensionMeta($file, $type, $extname) {
 196          if(!file_exists($file)) return array();
 197          $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
 198  
 199          // include file
 200          $meta = array();
 201          include $file;
 202          if(empty($meta)) return array();
 203  
 204          // read data
 205          $data = array();
 206          $data[$prefix . $type . '_settings_name'] = ['fieldset'];
 207          foreach($meta as $key => $value) {
 208              if($value[0] == 'fieldset') continue; //plugins only get one fieldset
 209              $data[$prefix . $key] = $value;
 210          }
 211  
 212          return $data;
 213      }
 214  
 215      /**
 216       * Read a default file from an extension
 217       *
 218       * This is used to read the default.php files of plugins and templates
 219       *
 220       * @param string $file php file to read
 221       * @param string $type should be 'plugin' or 'tpl'
 222       * @param string $extname name of the extension
 223       * @return array
 224       */
 225      protected function loadExtensionConf($file, $type, $extname) {
 226          if(!file_exists($file)) return array();
 227          $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
 228  
 229          // parse file
 230          $conf = $this->parser->parse($file);
 231          if(empty($conf)) return array();
 232  
 233          // read data
 234          $data = array();
 235          foreach($conf as $key => $value) {
 236              $data[$prefix . $key] = $value;
 237          }
 238  
 239          return $data;
 240      }
 241  
 242      /**
 243       * Read the language file of an extension
 244       *
 245       * @param string $dir directory of the extension
 246       * @param string $type should be 'plugin' or 'tpl'
 247       * @param string $extname name of the extension
 248       * @return array
 249       */
 250      protected function loadExtensionLang($dir, $type, $extname) {
 251          global $conf;
 252          $ll = $conf['lang'];
 253          $prefix = $type . Configuration::KEYMARKER . $extname . Configuration::KEYMARKER;
 254  
 255          // include files
 256          $lang = array();
 257          if(file_exists($dir . 'lang/en/settings.php')) {
 258              include $dir . 'lang/en/settings.php';
 259          }
 260          if($ll != 'en' && file_exists($dir . 'lang/' . $ll . '/settings.php')) {
 261              include $dir . 'lang/' . $ll . '/settings.php';
 262          }
 263  
 264          // set up correct keys
 265          $strings = array();
 266          foreach($lang as $key => $val) {
 267              $strings[$prefix . $key] = $val;
 268          }
 269  
 270          // add fieldset key
 271          $strings[$prefix . $type . '_settings_name'] = ucwords(str_replace('_', ' ', $extname));
 272  
 273          return $strings;
 274      }
 275  }