Skip to content

Commit 2855565

Browse files
authored
Merge pull request #2107 from cicnavi/feat/returned-config
Add support for returned config array in config files
2 parents 0970efc + f9f49d5 commit 2855565

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/SimpleSAML/Configuration.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,23 @@ private static function loadFromFile(string $filename, bool $required): Configur
153153
ob_start();
154154
if (interface_exists('Throwable', false)) {
155155
try {
156-
require($filename);
156+
$returnedConfig = require($filename);
157157
} catch (ParseError $e) {
158158
self::$loadedConfigs[$filename] = self::loadFromArray([], '[ARRAY]', 'simplesaml');
159159
throw new Error\ConfigurationError($e->getMessage(), $filename, []);
160160
}
161161
} else {
162-
require($filename);
162+
$returnedConfig = require($filename);
163163
}
164164

165165
$spurious_output = ob_get_length() > 0;
166166
ob_end_clean();
167167

168+
// Check if the config file actually returned an array instead of defining $config variable.
169+
if (is_array($returnedConfig)) {
170+
$config = $returnedConfig;
171+
}
172+
168173
// check that $config exists
169174
if (!isset($config)) {
170175
throw new Error\ConfigurationError(

tests/config/defined_config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$config = [
6+
'defined' => 'config',
7+
];

tests/config/returned_config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'returned' => 'config',
7+
];

tests/src/SimpleSAML/ConfigurationTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,26 @@ public function testGetConfigNonexistentFilePreload(): void
10861086
}
10871087

10881088

1089+
public function testCanLoadDefinedConfigFromFile(): void
1090+
{
1091+
$testConfigDir = dirname(__FILE__, 3) . DIRECTORY_SEPARATOR . 'config';
1092+
putenv('SIMPLESAMLPHP_CONFIG_DIR=' . $testConfigDir);
1093+
1094+
$config = Configuration::getConfig('defined_config.php');
1095+
$this->assertArrayHasKey('defined', $config->toArray());
1096+
}
1097+
1098+
1099+
public function testCanLoadReturnedConfigFromFile(): void
1100+
{
1101+
$testConfigDir = dirname(__FILE__, 3) . DIRECTORY_SEPARATOR . 'config';
1102+
putenv('SIMPLESAMLPHP_CONFIG_DIR=' . $testConfigDir);
1103+
1104+
$config = Configuration::getConfig('returned_config.php');
1105+
$this->assertArrayHasKey('returned', $config->toArray());
1106+
}
1107+
1108+
10891109
/**
10901110
* Test that Configuration objects can be initialized from an array.
10911111
*

0 commit comments

Comments
 (0)