|
14 | 14 | use RonasIT\ProjectInitializator\Enums\RoleEnum;
|
15 | 15 | use RonasIT\ProjectInitializator\Enums\AppTypeEnum;
|
16 | 16 | use Winter\LaravelConfigWriter\ArrayFile;
|
| 17 | +use Winter\LaravelConfigWriter\EnvFile; |
17 | 18 |
|
18 | 19 | class InitCommand extends Command implements Isolatable
|
19 | 20 | {
|
@@ -92,17 +93,28 @@ public function handle(): void
|
92 | 93 |
|
93 | 94 | $envFile = (file_exists('.env')) ? '.env' : '.env.example';
|
94 | 95 |
|
95 |
| - $this->createOrUpdateConfigFile($envFile, '=', [ |
| 96 | + $this->updateEnvFile($envFile, [ |
96 | 97 | 'APP_NAME' => $this->appName,
|
| 98 | + 'DB_CONNECTION' => 'pgsql', |
| 99 | + 'DB_HOST' => 'pgsql', |
| 100 | + 'DB_PORT' => '5432', |
| 101 | + 'DB_DATABASE' => 'postgres', |
| 102 | + 'DB_USERNAME' => 'postgres', |
| 103 | + 'DB_PASSWORD' => '', |
97 | 104 | ]);
|
98 | 105 |
|
99 | 106 | if (!file_exists('.env.development')) {
|
100 | 107 | copy('.env.example', '.env.development');
|
101 | 108 | }
|
102 | 109 |
|
103 |
| - $this->createOrUpdateConfigFile('.env.development', '=', [ |
| 110 | + $this->updateEnvFile('.env.development', [ |
104 | 111 | 'APP_NAME' => $this->appName,
|
105 | 112 | 'APP_URL' => $this->appUrl,
|
| 113 | + 'APP_MAINTENANCE_DRIVER' => 'cache', |
| 114 | + 'CACHE_STORE' => 'redis', |
| 115 | + 'QUEUE_CONNECTION' => 'redis', |
| 116 | + 'SESSION_DRIVER' => 'redis', |
| 117 | + 'DB_CONNECTION' => 'pgsql', |
106 | 118 | ]);
|
107 | 119 |
|
108 | 120 | $this->publishWebLogin();
|
@@ -137,11 +149,11 @@ public function handle(): void
|
137 | 149 | $data['CLERK_ALLOWED_ORIGINS'] = '';
|
138 | 150 | }
|
139 | 151 |
|
140 |
| - $this->createOrUpdateConfigFile('.env.development', '=', $data); |
141 |
| - $this->createOrUpdateConfigFile('.env.example', '=', $data); |
| 152 | + $this->updateEnvFile('.env.development', $data); |
| 153 | + $this->updateEnvFile($envFile, $data); |
142 | 154 |
|
143 |
| - if ($envFile === '.env') { |
144 |
| - $this->createOrUpdateConfigFile($envFile, '=', $data); |
| 155 | + if ($envFile !== '.env.example') { |
| 156 | + $this->updateEnvFile('.env.example', $data); |
145 | 157 | }
|
146 | 158 | }
|
147 | 159 |
|
@@ -459,44 +471,23 @@ protected function publishMigration(View $view, string $migrationName): void
|
459 | 471 | $this->publishClass($view, $migrationName, 'database/migrations');
|
460 | 472 | }
|
461 | 473 |
|
462 |
| - protected function createOrUpdateConfigFile(string $fileName, string $separator, array $data): void |
| 474 | + protected function updateEnvFile(string $fileName, array $data): void |
463 | 475 | {
|
464 |
| - $parsed = file_get_contents($fileName); |
| 476 | + $env = EnvFile::open($fileName); |
465 | 477 |
|
466 |
| - $lines = explode("\n", $parsed); |
| 478 | + // TODO: After updating wintercms/laravel-config-writer, remove the key comparison check and keep only $env->addEmptyLine(); |
| 479 | + $envKeys = array_column($env->getAst(), 'match'); |
| 480 | + $dataKeys = array_keys($data); |
467 | 481 |
|
468 |
| - $previousKey = null; |
| 482 | + $hasMissingKeys = count(array_intersect($dataKeys, $envKeys)) !== count($dataKeys); |
469 | 483 |
|
470 |
| - foreach ($data as $key => $value) { |
471 |
| - $value = $this->addQuotes($value); |
472 |
| - |
473 |
| - foreach ($lines as &$line) { |
474 |
| - if (Str::contains($line, $key)) { |
475 |
| - $line = "{$key}{$separator}{$value}"; |
476 |
| - |
477 |
| - continue 2; |
478 |
| - } |
479 |
| - } |
480 |
| - |
481 |
| - $item = "{$key}{$separator}{$value}"; |
482 |
| - |
483 |
| - if (!empty($previousKey) && $this->configKeysHaveSamePrefix($key, $previousKey)) { |
484 |
| - $lines[] = $item; |
485 |
| - } else { |
486 |
| - $lines[] = "\n{$item}"; |
487 |
| - } |
488 |
| - |
489 |
| - $previousKey = $key; |
| 484 | + if ($hasMissingKeys) { |
| 485 | + $env->addEmptyLine(); |
490 | 486 | }
|
491 | 487 |
|
492 |
| - $ymlSettings = implode("\n", $lines); |
| 488 | + $env->set($data); |
493 | 489 |
|
494 |
| - file_put_contents($fileName, $ymlSettings); |
495 |
| - } |
496 |
| - |
497 |
| - protected function configKeysHaveSamePrefix(string $key, string $previousKey): bool |
498 |
| - { |
499 |
| - return Str::before($key, '_') === Str::before($previousKey, '_'); |
| 490 | + $env->write(); |
500 | 491 | }
|
501 | 492 |
|
502 | 493 | protected function loadReadmePart(string $fileName): string
|
|
0 commit comments