Example #1
0
 /**
  * Fetches a configuration value.
  *
  * @param string|array $parents
  *   The path to the configuration value. Strings use dots as path separator.
  * @param NULL $default_value
  *   The default value to use if the config value isn't set.
  *
  * @return mixed
  *   The configuration value.
  */
 public static function get($parents, $default_value = NULL)
 {
     $options = \Drupal::service('variable')->get('openlayers_config');
     if (is_string($parents)) {
         $parents = explode('.', $parents);
     }
     if (is_array($parents)) {
         $notfound = FALSE;
         foreach ($parents as $parent) {
             if (isset($options[$parent])) {
                 $options = $options[$parent];
             } else {
                 $notfound = TRUE;
                 break;
             }
         }
         if (!$notfound) {
             return $options;
         }
     }
     if ($value = Config::defaults(implode('.', $parents))) {
         return $value;
     }
     if (is_null($default_value)) {
         return FALSE;
     }
     return $default_value;
 }
Example #2
0
 /**
  * Main loader, loads defaults, main config and virtualhost config if it exists
  * 
  * @param string $virtualhost the name of a particular virtualhost to load
  * 
  * @throws ConfigFileMissingException
  * @throws ConfigBadParameterException
  */
 private static function load($virtualhost = null)
 {
     if (!is_null(self::$parameters) && !$virtualhost) {
         return;
     }
     // Do not load twice, except if switching virtualhost
     // Load default configuration
     self::$parameters = array();
     // Load defaults if needed
     if (is_null(self::$defaults)) {
         self::$defaults = array();
         $defaults_file = APPLICATION_BASE . '/includes/core/ConfigDefaults.php';
         $default = array();
         if (file_exists($defaults_file)) {
             include_once $defaults_file;
         }
         //include: if file doesn't exists, execution will not stop, only gives a warning. if() not needed
         self::$defaults = $default;
         foreach ($default as $key => $value) {
             self::$parameters[$key] = $value;
         }
     }
     // Check if main config exists
     $main_config_file = APPLICATION_BASE . '/config/config.php';
     if (!file_exists($main_config_file)) {
         throw new ConfigFileMissingException($main_config_file);
     }
     $config = array();
     // Load base config
     include_once $main_config_file;
     if ($virtualhost != null) {
         $config['virtualhost'] = $virtualhost;
     }
     foreach ($config as $key => $value) {
         self::$parameters[$key] = $value;
     }
     // Merge
     // Load virtualhost config if used
     if ($virtualhost === null) {
         $virtualhost = self::get('virtualhost');
     }
     if ($virtualhost) {
         if (!is_string($virtualhost)) {
             throw new ConfigBadParameterException('virtualhost');
         }
         $config_file = APPLICATION_BASE . '/config/' . $virtualhost . '/config.php';
         if (!file_exists($config_file)) {
             throw new ConfigFileMissingException($config_file);
         }
         // Should exist even if empty
         $config = array();
         include_once $config_file;
         foreach ($config as $key => $value) {
             self::$parameters[$key] = $value;
         }
         // Merge
     }
     // Load config overrides if any
     $overrides_cfg = self::get('config_overrides');
     if ($overrides_cfg) {
         $overrides_file = APPLICATION_BASE . '/config/' . ($virtualhost ? $virtualhost . '/' : '') . 'config_overrides.json';
         $overrides = file_exists($overrides_file) ? json_decode(trim(file_get_contents($overrides_file))) : new StdClass();
         self::$override = array('file' => $overrides_file, 'parameters' => array());
         foreach ($overrides_cfg as $key => $dfn) {
             // Casting
             if (is_string($dfn)) {
                 $dfn = array('type' => $dfn);
             } else {
                 if (is_array($dfn) && !array_key_exists('type', $dfn)) {
                     $dfn = array('type' => 'enum', 'values' => $dfn);
                 } else {
                     if (!is_array($dfn)) {
                         throw new ConfigBadParameterException('config_overrides');
                     }
                 }
             }
             $dfn['value'] = property_exists($overrides, $key) ? $overrides->{$key} : null;
             self::$override['parameters'][$key] = $dfn;
         }
     }
 }