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.
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
9e1aedfbad
commit
8080e9cbd0
|
|
@ -10,23 +10,57 @@
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
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\Container;
|
||||||
use SiteWeaver\Core\Config;
|
use SiteWeaver\Core\Config;
|
||||||
use SiteWeaver\Core\Router;
|
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();
|
$container = new Container();
|
||||||
|
|
||||||
// Register core services
|
// Register core services
|
||||||
$router = new Router();
|
$router = new Router();
|
||||||
|
$config = new Config();
|
||||||
|
$config->load($mergedEnv);
|
||||||
$container->add('router', $router);
|
$container->add('router', $router);
|
||||||
$container->add('config', function () use ($mergedEnv) {
|
$container->add('config', $config);
|
||||||
return new Config($mergedEnv);
|
|
||||||
});
|
|
||||||
|
|
||||||
return $container;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,51 +24,7 @@ class Config
|
||||||
/** @var array<string, mixed> */
|
/** @var array<string, mixed> */
|
||||||
private array $values = [];
|
private array $values = [];
|
||||||
|
|
||||||
public function __construct(?array $env = null)
|
public function __construct() {}
|
||||||
{
|
|
||||||
// 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 get(string $key, mixed $default = null): mixed
|
public function get(string $key, mixed $default = null): mixed
|
||||||
{
|
{
|
||||||
|
|
@ -82,5 +38,13 @@ class Config
|
||||||
{
|
{
|
||||||
return $this->values;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,8 @@ final class ConfigTest extends TestCase
|
||||||
'DEBUG' => true,
|
'DEBUG' => true,
|
||||||
];
|
];
|
||||||
|
|
||||||
$config = new Config($env);
|
$config = new Config();
|
||||||
|
$config->load($env);
|
||||||
|
|
||||||
$this->assertSame('testing', $config->get('APP_ENV'));
|
$this->assertSame('testing', $config->get('APP_ENV'));
|
||||||
$this->assertSame('localhost', $config->get('DB_HOST'));
|
$this->assertSame('localhost', $config->get('DB_HOST'));
|
||||||
|
|
@ -33,7 +34,8 @@ final class ConfigTest extends TestCase
|
||||||
'BAZ' => 123,
|
'BAZ' => 123,
|
||||||
];
|
];
|
||||||
|
|
||||||
$config = new Config($env);
|
$config = new Config();
|
||||||
|
$config->load($env);
|
||||||
|
|
||||||
$this->assertSame($env, $config->all());
|
$this->assertSame($env, $config->all());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue