|
4 | 4 |
|
5 | 5 | use Fleetbase\Models\Setting; |
6 | 6 | use Illuminate\Support\Arr; |
| 7 | +use Illuminate\Support\Str; |
7 | 8 |
|
8 | 9 | /** |
9 | 10 | * The EnvironmentMapper class is responsible for mapping system settings stored in the database |
@@ -216,4 +217,107 @@ protected static function setDefaultMailFrom(): void |
216 | 217 | config()->set('mail.from.address', Utils::getDefaultMailFromAddress()); |
217 | 218 | } |
218 | 219 | } |
| 220 | + |
| 221 | + /** |
| 222 | + * Optimized merge of database settings into application config. |
| 223 | + * |
| 224 | + * Batch-fetches all relevant setting records in one query, builds |
| 225 | + * a combined config payload, and applies it with a single config() call. |
| 226 | + */ |
| 227 | + public static function mergeConfigFromSettingsOptimized(): void |
| 228 | + { |
| 229 | + if (Setting::doesntHaveConnection()) { |
| 230 | + return; |
| 231 | + } |
| 232 | + |
| 233 | + // Build DB keys for env vars (prefixed with 'system.') |
| 234 | + $envDbKeys = array_map(fn ($path) => Str::startsWith($path, 'system.') |
| 235 | + ? $path |
| 236 | + : 'system.' . $path, array_values(static::$environmentVariables) |
| 237 | + ); |
| 238 | + |
| 239 | + // Build DB keys for settings (use first two segments for nested keys) |
| 240 | + $settingDbKeys = array_map(function ($map) { |
| 241 | + $key = $map['settingsKey']; |
| 242 | + $segments = explode('.', $key); |
| 243 | + if (count($segments) >= 3) { |
| 244 | + return 'system.' . $segments[0] . '.' . $segments[1]; |
| 245 | + } |
| 246 | + |
| 247 | + return Str::startsWith($key, 'system.') |
| 248 | + ? $key |
| 249 | + : 'system.' . $key; |
| 250 | + }, static::$settings); |
| 251 | + |
| 252 | + // Unique list of keys to fetch |
| 253 | + $fetchKeys = array_unique(array_merge($envDbKeys, $settingDbKeys)); |
| 254 | + |
| 255 | + // Fetch all values in one query |
| 256 | + $dbSettings = Setting::whereIn('key', $fetchKeys) |
| 257 | + ->pluck('value', 'key') |
| 258 | + ->all(); |
| 259 | + |
| 260 | + // Apply environment variables |
| 261 | + static::setEnvironmentVariablesOptimized($dbSettings); |
| 262 | + |
| 263 | + // Build new config payload |
| 264 | + $newConfig = []; |
| 265 | + foreach (static::$settings as $map) { |
| 266 | + $settingsKey = $map['settingsKey']; |
| 267 | + $configKey = $map['configKey']; |
| 268 | + $segments = explode('.', $settingsKey); |
| 269 | + |
| 270 | + // Determine the DB key we fetched |
| 271 | + $dbKey = count($segments) >= 3 |
| 272 | + ? 'system.' . $segments[0] . '.' . $segments[1] |
| 273 | + : 'system.' . $settingsKey; |
| 274 | + |
| 275 | + if (!isset($dbSettings[$dbKey])) { |
| 276 | + continue; |
| 277 | + } |
| 278 | + |
| 279 | + $rawValue = $dbSettings[$dbKey]; |
| 280 | + |
| 281 | + // Extract nested subkey if needed |
| 282 | + $value = count($segments) >= 3 |
| 283 | + ? data_get($rawValue, implode('.', array_slice($segments, 2))) |
| 284 | + : $rawValue; |
| 285 | + |
| 286 | + // Merge arrays rather than overwrite |
| 287 | + if (is_array($value) && is_array(config($configKey))) { |
| 288 | + $value = array_merge(config($configKey), array_filter($value)); |
| 289 | + } |
| 290 | + |
| 291 | + $newConfig[$configKey] = $value; |
| 292 | + } |
| 293 | + |
| 294 | + // Apply all config changes at once |
| 295 | + if (!empty($newConfig)) { |
| 296 | + config($newConfig); |
| 297 | + } |
| 298 | + |
| 299 | + // Ensure default mail 'from' fallback |
| 300 | + static::setDefaultMailFrom(); |
| 301 | + } |
| 302 | + |
| 303 | + /** |
| 304 | + * Sets environment variables using pre-fetched DB settings. |
| 305 | + * |
| 306 | + * @param array<string,mixed> $dbSettings Map of DB key => raw value |
| 307 | + */ |
| 308 | + protected static function setEnvironmentVariablesOptimized(array $dbSettings): void |
| 309 | + { |
| 310 | + foreach (static::$environmentVariables as $envVar => $configPath) { |
| 311 | + $dbKey = Str::startsWith($configPath, 'system.') |
| 312 | + ? $configPath |
| 313 | + : 'system.' . $configPath; |
| 314 | + |
| 315 | + if (empty(env($envVar)) && isset($dbSettings[$dbKey])) { |
| 316 | + $value = $dbSettings[$dbKey]; |
| 317 | + if (is_string($value) && $value !== '') { |
| 318 | + putenv(sprintf('%s="%s"', $envVar, addcslashes($value, '"'))); |
| 319 | + } |
| 320 | + } |
| 321 | + } |
| 322 | + } |
219 | 323 | } |
0 commit comments