From 3d03f6d75f190ae8db589ac52ae8ec3a1d8d3deb Mon Sep 17 00:00:00 2001 From: Dominic Benson <dominic.benson@thirdlight.com> Date: Wed, 21 Mar 2018 11:31:33 +0000 Subject: [PATCH] Allow a configuration to be pre-loaded as if from a file With a dynamic configuration, it is not desirable to load from literal files By storing a configuration object against a filepath, subsequent calls to getConfig work transparently, bypassing the disk load step. --- lib/SimpleSAML/Configuration.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/SimpleSAML/Configuration.php b/lib/SimpleSAML/Configuration.php index 3440f1e9d..7461a2ba3 100644 --- a/lib/SimpleSAML/Configuration.php +++ b/lib/SimpleSAML/Configuration.php @@ -194,6 +194,35 @@ class SimpleSAML_Configuration implements \SimpleSAML\Utils\ClearableState 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(SimpleSAML_Configuration $config, $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; + + self::$loadedConfigs[$filePath] = $config; + } + /** * Load a configuration file from a configuration set. -- GitLab