diff --git a/src/Site/Core/Config.php b/src/Site/Core/Config.php index c7e3003..976edc6 100644 --- a/src/Site/Core/Config.php +++ b/src/Site/Core/Config.php @@ -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 } } } - diff --git a/tests/unit/Core/ConfigTest.php b/tests/unit/Core/ConfigTest.php index 26035e6..c74b814 100644 --- a/tests/unit/Core/ConfigTest.php +++ b/tests/unit/Core/ConfigTest.php @@ -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()); + } }