Newer
Older
Jaime Perez Crespo
committed
namespace SimpleSAML;
use Exception;
use ParseError;
use SAML2\Constants;
use SimpleSAML\Assert\Assert;
use SimpleSAML\Error;
use SimpleSAML\Utils;
use Symfony\Component\Filesystem\Filesystem;
use function array_key_exists;
use function array_keys;
use function dirname;
use function implode;
use function interface_exists;
use function in_array;
use function is_array;
use function is_bool;
use function is_int;
use function is_string;
use function ob_end_clean;
use function ob_get_length;
use function ob_start;
use function preg_match;
use function preg_replace;
use function rtrim;
use function substr;
use function var_export;
/**
* 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
* configuration file is missing.
* @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];
}
$fileSystem = new Filesystem();
if ($fileSystem->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;
}
/**
Loading
Loading full blame...