Skip to content
Snippets Groups Projects
Commit e42591a3 authored by Olav Morken's avatar Olav Morken
Browse files

SimpleSAML_Configuration: Add getOptionalConfig().

This patch adds a getOptionalConfig() function which can be used to load
configuration files which are not required.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@1714 44740490-163a-0410-bde0-09ae8108e29a
parent 3e5f6efd
No related branches found
No related tags found
No related merge requests found
...@@ -76,28 +76,36 @@ class SimpleSAML_Configuration { ...@@ -76,28 +76,36 @@ class SimpleSAML_Configuration {
* Load the given configuration file. * Load the given configuration file.
* *
* @param string $filename The full path of the configuration file. * @param string $filename The full path of the configuration file.
* @param bool @required Whether the file is required.
* @return SimpleSAML_Configuration The configuration file. An exception will be thrown if the * @return SimpleSAML_Configuration The configuration file. An exception will be thrown if the
* configuration file is missing. * configuration file is missing.
*/ */
private static function loadFromFile($filename) { private static function loadFromFile($filename, $required) {
assert('is_string($filename)'); assert('is_string($filename)');
assert('is_bool($required)');
if (array_key_exists($filename, self::$loadedConfigs)) { if (array_key_exists($filename, self::$loadedConfigs)) {
return self::$loadedConfigs[$filename]; return self::$loadedConfigs[$filename];
} }
if (!file_exists($filename)) { if (file_exists($filename)) {
throw new Exception('Missing configuration file: ' . $filename); $config = 'UNINITIALIZED';
}
$config = 'UNINITIALIZED'; /* The file initializes a variable named '$config'. */
require($filename);
/* Check that $config is initialized to an array. */
if (!is_array($config)) {
throw new Exception('Invalid configuration file: ' . $filename);
}
/* The file initializes a variable named '$config'. */ } elseif ($required) {
require($filename); /* File does not exist, but is required. */
throw new Exception('Missing configuration file: ' . $filename);
/* Check that $config is initialized to an array. */ } else {
if (!is_array($config)) { /* File does not exist, but is optional. */
throw new Exception('Invalid configuration file: ' . $filename); $config = array();
} }
if (array_key_exists('override.host', $config)) { if (array_key_exists('override.host', $config)) {
...@@ -157,7 +165,34 @@ class SimpleSAML_Configuration { ...@@ -157,7 +165,34 @@ class SimpleSAML_Configuration {
$dir = self::$configDirs[$configSet]; $dir = self::$configDirs[$configSet];
$filePath = $dir . '/' . $filename; $filePath = $dir . '/' . $filename;
return self::loadFromFile($filePath); return self::loadFromFile($filePath, TRUE);
}
/**
* Load a configuration file from a configuration set.
*
* This function will return a configuration object even if the file does not exist.
*
* @param string $filename The name of the configuration file.
* @param string $configSet The configuration set. Optional, defaults to 'simplesaml'.
* @return SimpleSAML_Configuration A configuration object.
*/
public static function getOptionalConfig($filename = 'config.php', $configSet = 'simplesaml') {
assert('is_string($filename)');
assert('is_string($configSet)');
if (!array_key_exists($configSet, self::$configDirs)) {
if ($configSet !== 'simplesaml') {
throw new Exception('Configuration set \'' . $configSet . '\' not initialized.');
} else {
self::$configDirs['simplesaml'] = dirname(dirname(dirname(__FILE__))) . '/config';
}
}
$dir = self::$configDirs[$configSet];
$filePath = $dir . '/' . $filename;
return self::loadFromFile($filePath, FALSE);
} }
...@@ -222,7 +257,7 @@ class SimpleSAML_Configuration { ...@@ -222,7 +257,7 @@ class SimpleSAML_Configuration {
return self::$instance[$instancename]; return self::$instance[$instancename];
} }
self::$instance[$instancename] = self::loadFromFile($path . '/' . $configfilename); self::$instance[$instancename] = self::loadFromFile($path . '/' . $configfilename, TRUE);
} }
...@@ -249,7 +284,7 @@ class SimpleSAML_Configuration { ...@@ -249,7 +284,7 @@ class SimpleSAML_Configuration {
self::setConfigDir($path, 'simplesaml'); self::setConfigDir($path, 'simplesaml');
} }
self::$instance[$instancename] = self::loadFromFile($dir . '/' . $filename); self::$instance[$instancename] = self::loadFromFile($dir . '/' . $filename, TRUE);
return self::$instance[$instancename]; return self::$instance[$instancename];
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment