From 8080e9cbd07b80f178478ccff8626718e439f798 Mon Sep 17 00:00:00 2001 From: Funky Waddle Date: Thu, 10 Sep 2026 23:52:57 -0500 Subject: [PATCH] Modify Config to have a load function. Modify bootstrap to load the environment variables and pass them into Config, instead of Config doing the loading. --- config/bootstrap.php | 54 +++++++++++++++++++++++++++------- src/Site/Core/Config.php | 54 ++++++---------------------------- tests/unit/Core/ConfigTest.php | 6 ++-- 3 files changed, 57 insertions(+), 57 deletions(-) diff --git a/config/bootstrap.php b/config/bootstrap.php index eda6522..b954056 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -10,23 +10,57 @@ */ declare(strict_types=1); -require __DIR__ . '/../vendor/autoload.php'; - -$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..'); -$dotenv->safeLoad(); - -$mergedEnv = $_ENV + $_SERVER; - use SiteWeaver\Core\Container; use SiteWeaver\Core\Config; use SiteWeaver\Core\Router; +use Infisical\SDK\InfisicalSDK; +use Infisical\SDK\Models\ListSecretsParameters; + +require __DIR__ . '/../vendor/autoload.php'; + +load_env(); + +$mergedEnv = $_ENV + $_SERVER; + $container = new Container(); // Register core services $router = new Router(); +$config = new Config(); +$config->load($mergedEnv); $container->add('router', $router); -$container->add('config', function () use ($mergedEnv) { - return new Config($mergedEnv); -}); +$container->add('config', $config); return $container; + +function load_env(): void { + // Load .env file if there is one + $dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..'); + $dotenv->safeLoad(); + + // Load Infisical vars if set + $url = $_ENV['INFISICAL_URL'] ?? null; + $machine_id = $_ENV['INFISICAL_MACHINE_ID'] ?? null; + $secret_id = $_ENV['INFISICAL_SECRET_ID'] ?? null; + $project_id = $_ENV['INFISICAL_PROJECT_ID'] ?? null; + $env = $_ENV['INFISICAL_ENV'] ?? null; + + if (!empty($url)) { + $sdk = new InfisicalSDK($url); + + $response = $sdk->auth()->universalAuth()->login($machine_id, $secret_id); + + $params = new ListSecretsParameters( + environment: $env, + secretPath: "/", + projectId: $project_id + ); + + $secrets = $sdk->secrets()->list($params); + + foreach ($secrets as $secret) { + putenv("{$secret->secretKey}={$secret->secretValue}"); + $_ENV[$secret->secretKey] = $secret->secretValue; + } + } +} diff --git a/src/Site/Core/Config.php b/src/Site/Core/Config.php index 09cad2e..c7e3003 100644 --- a/src/Site/Core/Config.php +++ b/src/Site/Core/Config.php @@ -24,51 +24,7 @@ class Config /** @var array */ private array $values = []; - public function __construct(?array $env = null) - { - // Use provided env array or fallback to $_ENV - $this->values = $env ?? $_ENV; - - // If Infisical integration variables are present, attempt to fetch secrets - if ($this->hasInfisicalConfig()) { - try { - $sdk = new InfisicalSDK($this->get('INFISICAL_URL')); - - // Optional authentication using client credentials - $clientId = $this->get('INFISICAL_CLIENT_ID'); - $clientSecret = $this->get('INFISICAL_CLIENT_SECRET'); - if ($clientId !== null && $clientSecret !== null) { - /** @var UniversalAuthService $auth */ - $auth = $sdk->auth()->universalAuth(); - /** @var MachineIdentityCredential $credential */ - $credential = $auth->login($clientId, $clientSecret); - // The SDK will internally set the auth token via callback - } - - // Fetch secrets and merge into values if not already defined - foreach ($sdk->secrets()->list() as $secret) { - $key = $secret->secretKey ?? null; - $value = $secret->secretValue ?? null; - if ($key !== null && $value !== null && !array_key_exists($key, $this->values)) { - $this->values[$key] = $value; - } - } - } catch (\Throwable $e) { - // If Infisical integration fails, rethrow as runtime exception - throw new \RuntimeException('Failed to fetch secrets from Infisical: ' . $e->getMessage(), 0, $e); - } - } - } - - /** - * Check if required Infisical config variables are present. - */ - private function hasInfisicalConfig(): bool - { - return !empty($this->values['INFISICAL_URL']) && - !empty($this->values['INFISICAL_ENV']) && - !empty($this->values['INFISICAL_PROJECT_ID']); - } + public function __construct() {} public function get(string $key, mixed $default = null): mixed { @@ -82,5 +38,13 @@ class Config { return $this->values; } + + public function load(array $settings): void { + foreach($settings as $key => $value) { + if ($key !== null && $value !== null && !array_key_exists($key, $this->values)) { + $this->values[$key] = $value; + } + } + } } diff --git a/tests/unit/Core/ConfigTest.php b/tests/unit/Core/ConfigTest.php index e453f64..26035e6 100644 --- a/tests/unit/Core/ConfigTest.php +++ b/tests/unit/Core/ConfigTest.php @@ -16,7 +16,8 @@ final class ConfigTest extends TestCase 'DEBUG' => true, ]; - $config = new Config($env); + $config = new Config(); + $config->load($env); $this->assertSame('testing', $config->get('APP_ENV')); $this->assertSame('localhost', $config->get('DB_HOST')); @@ -33,7 +34,8 @@ final class ConfigTest extends TestCase 'BAZ' => 123, ]; - $config = new Config($env); + $config = new Config(); + $config->load($env); $this->assertSame($env, $config->all()); }