77 lines
2.6 KiB
PHP
77 lines
2.6 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use Illuminate\Console\Command;
|
||
|
||
class MigrateEnvReverb extends Command
|
||
{
|
||
protected $signature = 'mailwolt:migrate-env-reverb';
|
||
protected $description = 'Migriert veraltete REVERB_* .env-Werte auf Domain-Basis';
|
||
|
||
public function handle(): int
|
||
{
|
||
$env = base_path('.env');
|
||
if (!file_exists($env)) {
|
||
$this->warn('.env nicht gefunden.');
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
$content = file_get_contents($env);
|
||
|
||
// APP_HOST ermitteln – Fallback auf APP_URL
|
||
$appHost = $this->extractVar($content, 'APP_HOST');
|
||
if (!$appHost) {
|
||
$appUrl = $this->extractVar($content, 'APP_URL');
|
||
$appHost = parse_url($appUrl ?: '', PHP_URL_HOST) ?: '';
|
||
}
|
||
|
||
if (!$appHost || preg_match('/^\d+\.\d+\.\d+\.\d+$/', $appHost)) {
|
||
$this->warn('Kein gültiger Hostname gefunden – Migration übersprungen.');
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
$viteHost = $this->extractVar($content, 'VITE_REVERB_HOST');
|
||
if ($viteHost === '${REVERB_HOST}' || $viteHost === $appHost) {
|
||
$this->info('REVERB-Werte bereits korrekt.');
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
$scheme = file_exists("/etc/letsencrypt/live/{$appHost}/fullchain.pem") ? 'https' : 'http';
|
||
$port = $scheme === 'https' ? '443' : '80';
|
||
|
||
$fixes = [
|
||
'REVERB_HOST' => '${APP_HOST}',
|
||
'REVERB_PORT' => $port,
|
||
'REVERB_SCHEME' => $scheme,
|
||
'REVERB_PATH' => '/ws',
|
||
'REVERB_SERVER_HOST' => '127.0.0.1',
|
||
'REVERB_SERVER_PORT' => '8080',
|
||
'REVERB_SERVER_SCHEME' => 'http',
|
||
'REVERB_SERVER_PATH' => '',
|
||
'VITE_REVERB_HOST' => '${REVERB_HOST}',
|
||
'VITE_REVERB_PORT' => '${REVERB_PORT}',
|
||
'VITE_REVERB_SCHEME' => '${REVERB_SCHEME}',
|
||
'VITE_REVERB_PATH' => '${REVERB_PATH}',
|
||
];
|
||
|
||
foreach ($fixes as $key => $val) {
|
||
if (preg_match("/^{$key}=/m", $content)) {
|
||
$content = preg_replace("/^{$key}=.*/m", "{$key}={$val}", $content);
|
||
} else {
|
||
$content .= "\n{$key}={$val}";
|
||
}
|
||
}
|
||
|
||
file_put_contents($env, $content);
|
||
$this->info("REVERB .env migriert für Host: {$appHost}");
|
||
return self::SUCCESS;
|
||
}
|
||
|
||
private function extractVar(string $content, string $key): string
|
||
{
|
||
preg_match("/^{$key}=(.*)$/m", $content, $m);
|
||
return trim($m[1] ?? '', " \t\"'");
|
||
}
|
||
}
|