Skip to content

Commit 161d0cc

Browse files
committed
Merge remote-tracking branch 'origin/main' into generate-env-development
2 parents b19187a + 974b698 commit 161d0cc

16 files changed

+839
-1107
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"laravel/framework": ">=11.30",
3232
"php-mock/php-mock-phpunit": "^2.10",
3333
"ext-json": "*",
34-
"ronasit/laravel-helpers": "^3.3",
34+
"ronasit/laravel-helpers": ">=3.5.7",
3535
"winter/laravel-config-writer": "^1.2"
3636
},
3737
"require-dev": {

composer.lock

Lines changed: 397 additions & 331 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Commands/InitCommand.php

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use RonasIT\ProjectInitializator\Enums\RoleEnum;
1515
use RonasIT\ProjectInitializator\Enums\AppTypeEnum;
1616
use Winter\LaravelConfigWriter\ArrayFile;
17+
use Winter\LaravelConfigWriter\EnvFile;
1718

1819
class InitCommand extends Command implements Isolatable
1920
{
@@ -92,17 +93,28 @@ public function handle(): void
9293

9394
$envFile = (file_exists('.env')) ? '.env' : '.env.example';
9495

95-
$this->createOrUpdateConfigFile($envFile, '=', [
96+
$this->updateEnvFile($envFile, [
9697
'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' => '',
97104
]);
98105

99106
if (!file_exists('.env.development')) {
100107
copy('.env.example', '.env.development');
101108
}
102109

103-
$this->createOrUpdateConfigFile('.env.development', '=', [
110+
$this->updateEnvFile('.env.development', [
104111
'APP_NAME' => $this->appName,
105112
'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',
106118
]);
107119

108120
$this->publishWebLogin();
@@ -137,11 +149,11 @@ public function handle(): void
137149
$data['CLERK_ALLOWED_ORIGINS'] = '';
138150
}
139151

140-
$this->createOrUpdateConfigFile('.env.development', '=', $data);
141-
$this->createOrUpdateConfigFile('.env.example', '=', $data);
152+
$this->updateEnvFile('.env.development', $data);
153+
$this->updateEnvFile($envFile, $data);
142154

143-
if ($envFile === '.env') {
144-
$this->createOrUpdateConfigFile($envFile, '=', $data);
155+
if ($envFile !== '.env.example') {
156+
$this->updateEnvFile('.env.example', $data);
145157
}
146158
}
147159

@@ -459,44 +471,23 @@ protected function publishMigration(View $view, string $migrationName): void
459471
$this->publishClass($view, $migrationName, 'database/migrations');
460472
}
461473

462-
protected function createOrUpdateConfigFile(string $fileName, string $separator, array $data): void
474+
protected function updateEnvFile(string $fileName, array $data): void
463475
{
464-
$parsed = file_get_contents($fileName);
476+
$env = EnvFile::open($fileName);
465477

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);
467481

468-
$previousKey = null;
482+
$hasMissingKeys = count(array_intersect($dataKeys, $envKeys)) !== count($dataKeys);
469483

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();
490486
}
491487

492-
$ymlSettings = implode("\n", $lines);
488+
$env->set($data);
493489

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();
500491
}
501492

502493
protected function loadReadmePart(string $fileName): string

0 commit comments

Comments
 (0)