#!/usr/bin/env php * * All overrides are read from environment variables and are optional; an * unset or empty variable leaves the corresponding config value untouched: * DATABASE_URI -> $config['database']['uri'] * DATABASE_NAME -> $config['database']['database'] * APP_ENVIRONMENT -> $config['environment'] * SECURITY_SALT -> $config['security.salt'] */ $path = $argv[1] ?? null; if (!$path || !is_file($path)) { fwrite(STDERR, "Configuration file not found: {$path}\n"); exit(1); } $config = include $path; if (!is_array($config)) { fwrite(STDERR, "Configuration file did not return an array: {$path}\n"); exit(1); } // Nested overrides: env var => [top-level key, nested key] $nestedOverrides = [ 'DATABASE_URI' => ['database', 'uri'], 'DATABASE_NAME' => ['database', 'database'], ]; foreach ($nestedOverrides as $env => [$group, $key]) { $value = getenv($env); if ($value === false || $value === '') { continue; } $config[$group][$key] = $value; echo " - {$group}.{$key} overridden\n"; } // Flat overrides: env var => config key $flatOverrides = [ 'APP_ENVIRONMENT' => 'environment', 'SECURITY_SALT' => 'security.salt', ]; foreach ($flatOverrides as $env => $key) { $value = getenv($env); if ($value === false || $value === '') { continue; } $config[$key] = $value; echo " - {$key} overridden\n"; } $export = var_export($config, true); file_put_contents($path, "