diff options
author | Jaime Perez Crespo <jaime.perez@uninett.no> | 2016-02-02 14:33:36 +0100 |
---|---|---|
committer | Jaime Perez Crespo <jaime.perez@uninett.no> | 2016-02-02 14:33:36 +0100 |
commit | 85ff5bf356b62a5ad8428ceb0ecd1389d0340074 (patch) | |
tree | 1b4341e04995f4ec47b0a652c58215c582919143 /lib | |
parent | 5d1b14218f7ef4f6eae15ebfb54459dd7a04deee (diff) | |
download | simplesamlphp-85ff5bf356b62a5ad8428ceb0ecd1389d0340074.zip simplesamlphp-85ff5bf356b62a5ad8428ceb0ecd1389d0340074.tar.gz simplesamlphp-85ff5bf356b62a5ad8428ceb0ecd1389d0340074.tar.bz2 |
Make it possible to pre-load a configuration array into SimpleSAML_Configuration for subsequent calls to getInstance() to use it. This opens up for unit-testing code that calls SimpleSAML_Configuration::getInstance().
Diffstat (limited to 'lib')
-rw-r--r-- | lib/SimpleSAML/Configuration.php | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/lib/SimpleSAML/Configuration.php b/lib/SimpleSAML/Configuration.php index a559aa5..2a52bf1 100644 --- a/lib/SimpleSAML/Configuration.php +++ b/lib/SimpleSAML/Configuration.php @@ -214,15 +214,22 @@ class SimpleSAML_Configuration * * @param array $config The configuration array. * @param string $location The location which will be given when an error occurs. Optional. + * @param string|null $instance The name of this instance. If specified, the configuration will be loaded and an + * instance with that name will be kept for it to be retrieved later with getInstance($instance). If null, the + * configuration will not be kept for later use. Defaults to null. * * @return SimpleSAML_Configuration The configuration object. */ - public static function loadFromArray($config, $location = '[ARRAY]') + public static function loadFromArray($config, $location = '[ARRAY]', $instance = null) { assert('is_array($config)'); assert('is_string($location)'); - return new SimpleSAML_Configuration($config, $location); + $c = new SimpleSAML_Configuration($config, $location); + if ($instance !== null) { + self::$instance[$instance] = $c; + } + return $c; } @@ -245,14 +252,16 @@ class SimpleSAML_Configuration { assert('is_string($instancename)'); + // check if the instance exists already + if (array_key_exists($instancename, self::$instance)) { + return self::$instance[$instancename]; + } + if ($instancename === 'simplesaml') { return self::getConfig(); } - if (!array_key_exists($instancename, self::$instance)) { - throw new Exception('Configuration with name '.$instancename.' is not initialized.'); - } - return self::$instance[$instancename]; + throw new Exception('Configuration with name '.$instancename.' is not initialized.'); } |