info("Original XChaCha key: $original[0]"); //Overwrite with new key $current = preg_replace('/^XCHACHA_KEY=.+$/m', "XCHACHA_KEY=$b64", $current); } else { //Append the key to the end of file $current .= PHP_EOL."XCHACHA_KEY=$b64"; } file_put_contents($path, $current); } $this->info('Successfully generated new key for XChaCha'); public function updateEnv($data = array()) { if (!count($data)) { return; } $pattern = '/([^\=]*)\=[^\n]*/'; $envFile = base_path() . '/.env'; $lines = file($envFile); $newLines = []; foreach ($lines as $line) { preg_match($pattern, $line, $matches); if (!count($matches)) { $newLines[] = $line; continue; } if (!key_exists(trim($matches[1]), $data)) { $newLines[] = $line; continue; } $line = trim($matches[1]) . "={$data[trim($matches[1])]}\n"; $newLines[] = $line; } $newContent = implode('', $newLines); file_put_contents($envFile, $newContent); }