Newer
Older
Jaime Perez Crespo
committed
namespace SimpleSAML;
use SAML2\Constants;
use SimpleSAML\Error;
use SimpleSAML\Utils;
/**
* Configuration of SimpleSAMLphp
Andreas Åkre Solberg
committed
*
* @package SimpleSAMLphp
class Configuration implements Utils\ClearableState
/**
* The release version of this package
*/
public const VERSION = 'master';
/**
* A default value which means that the given option is required.
*
* @var string
public const REQUIRED_OPTION = '___REQUIRED_OPTION___';
/**
* Associative array with mappings from instance-names to configuration objects.
* @var array<string, \SimpleSAML\Configuration>
* Configuration directories.
*
* This associative array contains the mappings from configuration sets to
* configuration directories.
/**
* Cache of loaded configuration files.
*
* The index in the array is the full path to the file.
/**
* The configuration array.
/**
* The location which will be given when an error occurs.
*
* @var string
/**
* The file this configuration was loaded from.
*
* @var string|null
/**
* Initializes a configuration from the given array.
*
* @param array $config The configuration array.
* @param string $location The location which will be given when an error occurs.
*/
public function __construct(array $config, string $location)
{
$this->configuration = $config;
$this->location = $location;
}
/**
* Load the given 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
* @throws \Exception If the configuration file is invalid or missing.
private static function loadFromFile(string $filename, bool $required): Configuration
{
if (array_key_exists($filename, self::$loadedConfigs)) {
return self::$loadedConfigs[$filename];
}
if (file_exists($filename)) {
// the file initializes a variable named '$config'
Jaime Perez Crespo
committed
ob_start();
if (interface_exists('Throwable', false)) {
} catch (\ParseError $e) {
self::$loadedConfigs[$filename] = self::loadFromArray([], '[ARRAY]', 'simplesaml');
throw new Error\ConfigurationError($e->getMessage(), $filename, []);
}
} else {
require($filename);
}
Jaime Perez Crespo
committed
$spurious_output = ob_get_length() > 0;
ob_end_clean();
Jaime Perez Crespo
committed
// check that $config exists
if (!isset($config)) {
throw new Error\ConfigurationError(
Jaime Perez Crespo
committed
'$config is not defined in the configuration file.',
$filename
);
}
// check that $config is initialized to an array
throw new Error\ConfigurationError(
Jaime Perez Crespo
committed
'$config is not an array.',
$filename
);
}
// check that $config is not empty
if (empty($config)) {
throw new Error\ConfigurationError(
Jaime Perez Crespo
committed
'$config is empty.',
$filename
);
// file does not exist, but is required
throw new Error\ConfigurationError('Missing configuration file', $filename);
Jaime Perez Crespo
committed
// file does not exist, but is optional, so return an empty configuration object without saving it
$cfg = new Configuration([], $filename);
Jaime Perez Crespo
committed
$cfg->filename = $filename;
return $cfg;
$cfg = new Configuration($config, $filename);
$cfg->filename = $filename;
self::$loadedConfigs[$filename] = $cfg;
Jaime Perez Crespo
committed
if ($spurious_output) {
Logger::warning(
Jaime Perez Crespo
committed
"The configuration file '$filename' generates output. Please review your configuration."
);
}
return $cfg;
}
/**
* Set the directory for configuration files for the given configuration set.
*
* @param string $path The directory which contains the configuration files.
* @param string $configSet The configuration set. Defaults to 'simplesaml'.
*/
public static function setConfigDir(string $path, string $configSet = 'simplesaml'): void
{
self::$configDirs[$configSet] = $path;
}
/**
* Store a pre-initialized configuration.
*
* Allows consumers to create configuration objects without having them
* loaded from a file.
*
* @param \SimpleSAML\Configuration $config The configuration object to store
* @param string $filename The name of the configuration file.
* @param string $configSet The configuration set. Optional, defaults to 'simplesaml'.
public static function setPreLoadedConfig(
Configuration $config,
Loading
Loading full blame...