1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

update variables for .env instead of writing completely new file

This commit is contained in:
Benjamin Beganović 2020-11-24 11:45:27 +01:00
parent 3b765d32c1
commit 242d48cd1a
3 changed files with 90 additions and 72 deletions

View File

@ -61,8 +61,6 @@ class SetupController extends Controller
return response('Oops, something went wrong. Check your logs.'); /* We should never reach this block, but just in case. */
}
return $request->all();
$mail_driver = $request->input('mail_driver');
if (! $this->failsafeMailCheck($request)) {
@ -71,48 +69,38 @@ class SetupController extends Controller
$url = $request->input('url');
if (substr($url, -1) != '/')
if (substr($url, -1) != '/') {
$url = $url . '/';
}
$env_values = [
'APP_URL' => $url,
'REQUIRE_HTTPS' => $request->input('https') ? 'true' : 'false',
'APP_DEBUG' => $request->input('debug') ? 'true' : 'false',
$_ENV['APP_KEY'] = config('app.key');
$_ENV['APP_URL'] = $url;
$_ENV['APP_DEBUG'] = $request->input('debug') ? 'true' : 'false';
$_ENV['REQUIRE_HTTPS'] = $request->input('https') ? 'true' : 'false';
$_ENV['DB_TYPE'] = 'mysql';
$_ENV['DB_HOST1'] = $request->input('host');
$_ENV['DB_DATABASE1'] = $request->input('database');
$_ENV['DB_USERNAME1'] = $request->input('db_username');
$_ENV['DB_PASSWORD1'] = $request->input('db_password');
$_ENV['MAIL_MAILER'] = $mail_driver;
$_ENV['MAIL_PORT'] = $request->input('mail_port');
$_ENV['MAIL_ENCRYPTION'] = $request->input('encryption');
$_ENV['MAIL_HOST'] = $request->input('mail_host');
$_ENV['MAIL_USERNAME'] = $request->input('mail_username');
$_ENV['MAIL_FROM_NAME'] = $request->input('mail_name');
$_ENV['MAIL_FROM_ADDRESS'] = $request->input('mail_address');
$_ENV['MAIL_PASSWORD'] = $request->input('mail_password');
$_ENV['NINJA_ENVIRONMENT'] = 'selfhost';
$_ENV['DB_CONNECTION'] = 'db-ninja-01';
'DB_HOST1' => $request->input('host'),
'DB_DATABASE1' => $request->input('database'),
'DB_USERNAME1' => $request->input('db_username'),
'DB_PASSWORD1' => $request->input('db_password'),
$config = '';
'MAIL_MAILER' => $mail_driver,
'MAIL_PORT' => $request->input('mail_port'),
'MAIL_ENCRYPTION' => $request->input('encryption'),
'MAIL_HOST' => $request->input('mail_host'),
'MAIL_USERNAME' => $request->input('mail_username'),
'MAIL_FROM_NAME' => $request->input('mail_name'),
'MAIL_FROM_ADDRESS' => $request->input('mail_address'),
'MAIL_PASSWORD' => $request->input('mail_password'),
'NINJA_ENVIRONMENT' => 'selfhost',
'DB_CONNECTION' => 'db-ninja-01',
];
try {
foreach ($_ENV as $key => $val) {
if (is_array($val)) {
continue;
}
if (preg_match('/\s/', $val)) {
$val = "'{$val}'";
}
$config .= "{$key}={$val}\n";
foreach ($env_values as $property => $value) {
$this->updateEnvironmentProperty($property, $value);
}
/* Write the .env file */
$filePath = base_path() . '/.env';
$fp = fopen($filePath, 'w');
fwrite($fp, $config);
fclose($fp);
/* We need this in some environments that do not have STDIN defined */
define('STDIN', fopen('php://stdin', 'r'));

View File

@ -1,4 +1,5 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
@ -16,6 +17,7 @@ use App\Utils\Ninja;
use App\Utils\SystemHealth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
trait AppSetup
{
@ -35,10 +37,10 @@ trait AppSetup
$cached_tables = config('ninja.cached_tables');
foreach ($cached_tables as $name => $class) {
if (request()->has('clear_cache') || ! Cache::has($name) || $force) {
if (request()->has('clear_cache') || !Cache::has($name) || $force) {
// check that the table exists in case the migration is pending
if (! Schema::hasTable((new $class())->getTable())) {
if (!Schema::hasTable((new $class())->getTable())) {
continue;
}
if ($name == 'payment_terms') {
@ -58,7 +60,7 @@ trait AppSetup
}
/*Build template cache*/
if (request()->has('clear_cache') || ! Cache::has('templates'))
if (request()->has('clear_cache') || !Cache::has('templates'))
$this->buildTemplates();
}
@ -67,49 +69,77 @@ trait AppSetup
{
$data = [
'invoice' => [
'subject' => EmailTemplateDefaults::emailInvoiceSubject(),
'body' => EmailTemplateDefaults::emailInvoiceTemplate(),
'invoice' => [
'subject' => EmailTemplateDefaults::emailInvoiceSubject(),
'body' => EmailTemplateDefaults::emailInvoiceTemplate(),
],
'quote' => [
'subject' => EmailTemplateDefaults::emailQuoteSubject(),
'body' => EmailTemplateDefaults::emailQuoteTemplate(),
'quote' => [
'subject' => EmailTemplateDefaults::emailQuoteSubject(),
'body' => EmailTemplateDefaults::emailQuoteTemplate(),
],
'payment' => [
'subject' => EmailTemplateDefaults::emailPaymentSubject(),
'body' => EmailTemplateDefaults::emailPaymentTemplate(),
'payment' => [
'subject' => EmailTemplateDefaults::emailPaymentSubject(),
'body' => EmailTemplateDefaults::emailPaymentTemplate(),
],
'payment_partial' => [
'subject' => EmailTemplateDefaults::emailPaymentPartialSubject(),
'body' => EmailTemplateDefaults::emailPaymentPartialTemplate(),
'payment_partial' => [
'subject' => EmailTemplateDefaults::emailPaymentPartialSubject(),
'body' => EmailTemplateDefaults::emailPaymentPartialTemplate(),
],
'reminder1' => [
'subject' => EmailTemplateDefaults::emailReminder1Subject(),
'body' => EmailTemplateDefaults::emailReminder1Template(),
'reminder1' => [
'subject' => EmailTemplateDefaults::emailReminder1Subject(),
'body' => EmailTemplateDefaults::emailReminder1Template(),
],
'reminder2' => [
'subject' => EmailTemplateDefaults::emailReminder2Subject(),
'body' => EmailTemplateDefaults::emailReminder2Template(),
'reminder2' => [
'subject' => EmailTemplateDefaults::emailReminder2Subject(),
'body' => EmailTemplateDefaults::emailReminder2Template(),
],
'reminder3' => [
'subject' => EmailTemplateDefaults::emailReminder3Subject(),
'body' => EmailTemplateDefaults::emailReminder3Template(),
'reminder3' => [
'subject' => EmailTemplateDefaults::emailReminder3Subject(),
'body' => EmailTemplateDefaults::emailReminder3Template(),
],
'reminder_endless' => [
'subject' => EmailTemplateDefaults::emailReminderEndlessSubject(),
'body' => EmailTemplateDefaults::emailReminderEndlessTemplate(),
'reminder_endless' => [
'subject' => EmailTemplateDefaults::emailReminderEndlessSubject(),
'body' => EmailTemplateDefaults::emailReminderEndlessTemplate(),
],
'statement' => [
'subject' => EmailTemplateDefaults::emailStatementSubject(),
'body' => EmailTemplateDefaults::emailStatementTemplate(),
'statement' => [
'subject' => EmailTemplateDefaults::emailStatementSubject(),
'body' => EmailTemplateDefaults::emailStatementTemplate(),
],
'credit' => [
'subject' => EmailTemplateDefaults::emailCreditSubject(),
'body' => EmailTemplateDefaults::emailCreditTemplate(),
'credit' => [
'subject' => EmailTemplateDefaults::emailCreditSubject(),
'body' => EmailTemplateDefaults::emailCreditTemplate(),
],
];
Cache::forever($name, $data);
}
private function updateEnvironmentProperty(string $property, $value): void
{
$env = file(base_path('.env'));
$position = null;
foreach ((array) $env as $key => $variable) {
if (Str::startsWith($variable, $property)) {
$position = $key;
}
}
// This should never happen, but this is login just in case.
// Variables that will be replaced (updated with different content) should already be in .env file.
if (is_null($position)) {
$env[] = "{$property}=" . $value . "\n";
} else {
$env[$position] = "{$property}=" . $value . "\n";
}
try {
file_put_contents(base_path('.env'), $env);
} catch (\Exception $e) {
info($e->getMessage());
}
}
}

View File

@ -3227,7 +3227,7 @@ return [
'bank_account_not_linked' => 'To pay with bank account, first you have to add it as payment method.',
'application_settings_label' => 'Let\'s store basic information about your Invoice Ninja!',
'recommended_in_production' => 'Recommended in production',
'recommended_in_production' => 'Highly recommended in production',
'enable_only_for_development' => 'Enable only for development',
'test_pdf' => 'Test PDF',