Modify Config to add individual set method with override capabilities. Add 2 tests to test override functionality
Some checks are pending
CI / build_and_test (push) Waiting to run
Some checks are pending
CI / build_and_test (push) Waiting to run
This commit is contained in:
parent
8080e9cbd0
commit
a8b2038d63
|
|
@ -12,10 +12,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace SiteWeaver\Core;
|
||||
|
||||
use Infisical\SDK\InfisicalSDK;
|
||||
use Infisical\SDK\Services\UniversalAuthService;
|
||||
use Infisical\SDK\Models\MachineIdentityCredential;
|
||||
|
||||
/**
|
||||
* Configuration service that loads environment variables and optionally fetches secrets from Infisical.
|
||||
*/
|
||||
|
|
@ -31,6 +27,22 @@ class Config
|
|||
return $this->values[$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Explicitly set or overwrite a configuration value.
|
||||
*
|
||||
* @param string $key The configuration key.
|
||||
* @param mixed $value The new value.
|
||||
*/
|
||||
public function set(string $key, mixed $value, bool $override = false): void
|
||||
{
|
||||
if ($override || !array_key_exists($key, $this->values)) {
|
||||
// Allow null values to be stored as well.
|
||||
if ($key !== null) {
|
||||
$this->values[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all configuration values.
|
||||
*/
|
||||
|
|
@ -47,4 +59,3 @@ class Config
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -39,4 +39,35 @@ final class ConfigTest extends TestCase
|
|||
|
||||
$this->assertSame($env, $config->all());
|
||||
}
|
||||
|
||||
public function testNoOverrideByDefault(): void {
|
||||
$env = [
|
||||
'FOO' => 'bar',
|
||||
'BAZ' => 123,
|
||||
];
|
||||
|
||||
$config = new Config();
|
||||
$config->load($env);
|
||||
$config->set('FOO', 'asdf');
|
||||
|
||||
$this->assertSame($env, $config->all());
|
||||
}
|
||||
|
||||
public function testForcedOverride(): void {
|
||||
$env = [
|
||||
'FOO' => 'bar',
|
||||
'BAZ' => 123,
|
||||
];
|
||||
|
||||
$config = new Config();
|
||||
$config->load($env);
|
||||
$config->set('FOO', 'asdf', true);
|
||||
|
||||
$new_env = [
|
||||
'FOO' => 'asdf',
|
||||
'BAZ' => 123,
|
||||
];
|
||||
|
||||
$this->assertSame($new_env, $config->all());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue