Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix manual safe button and few simplifications

ayntk-ai93444ea872ca
diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Livewire/Project/Shared/EnvironmentVariable/All.php
index dbd5c2b9bf..2c90427c08 100644
--- a/app/Livewire/Project/Shared/EnvironmentVariable/All.php
+++ b/app/Livewire/Project/Shared/EnvironmentVariable/All.php
@@ -9,17 +9,11 @@ use Visus\Cuid2\Cuid2;
class All extends Component
{
public $resource;
-
public string $resourceClass;
-
public bool $showPreview = false;
-
public ?string $modalId = null;
-
public ?string $variables = null;
-
public ?string $variablesPreview = null;
-
public string $view = 'normal';
protected $listeners = [
@@ -35,25 +29,20 @@ class All extends Component
{
$this->resourceClass = get_class($this->resource);
$resourceWithPreviews = ['App\Models\Application'];
- $simpleDockerfile = ! is_null(data_get($this->resource, 'dockerfile'));
- if (str($this->resourceClass)->contains($resourceWithPreviews) && ! $simpleDockerfile) {
+ $simpleDockerfile = !is_null(data_get($this->resource, 'dockerfile'));
+ if (str($this->resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) {
$this->showPreview = true;
}
$this->modalId = new Cuid2;
$this->sortMe();
- $this->getDevView();
}
public function sortMe()
{
if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') {
- if ($this->resource->settings->is_env_sorting_enabled) {
- $this->resource->environment_variables = $this->resource->environment_variables->sortBy('key');
- $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy('key');
- } else {
- $this->resource->environment_variables = $this->resource->environment_variables->sortBy('id');
- $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy('id');
- }
+ $sortBy = $this->resource->settings->is_env_sorting_enabled ? 'key' : 'id';
+ $this->resource->environment_variables = $this->resource->environment_variables->sortBy($sortBy);
+ $this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy($sortBy);
}
$this->getDevView();
}
@@ -69,117 +58,147 @@ class All extends Component
public function getDevView()
{
- $this->variables = $this->resource->environment_variables->map(function ($item) {
+ $this->variables = $this->formatEnvironmentVariables($this->resource->environment_variables);
+ if ($this->showPreview) {
+ $this->variablesPreview = $this->formatEnvironmentVariables($this->resource->environment_variables_preview);
+ }
+ }
+
+ private function formatEnvironmentVariables($variables)
+ {
+ return $variables->map(function ($item) {
if ($item->is_shown_once) {
return "$item->key=(locked secret)";
}
if ($item->is_multiline) {
return "$item->key=(multiline, edit in normal view)";
}
-
return "$item->key=$item->value";
- })->join('
-');
- if ($this->showPreview) {
- $this->variablesPreview = $this->resource->environment_variables_preview->map(function ($item) {
- if ($item->is_shown_once) {
- return "$item->key=(locked secret)";
- }
- if ($item->is_multiline) {
- return "$item->key=(multiline, edit in normal view)";
- }
-
- return "$item->key=$item->value";
- })->join('
-');
- }
+ })->join("\n");
}
public function switch()
{
- if ($this->view === 'normal') {
- $this->view = 'dev';
- } else {
- $this->view = 'normal';
- }
+ $this->view = $this->view === 'normal' ? 'dev' : 'normal';
$this->sortMe();
}
- public function submit()
+ public function submit($data = null)
{
try {
- $isPreview = $this->showPreview;
- $variables = $isPreview ? parseEnvFormatToArray($this->variablesPreview) : parseEnvFormatToArray($this->variables);
+ if ($data === null) {
+ // Handle saving in developer view
+ $variables = parseEnvFormatToArray($this->variables);
+ $this->deleteRemovedVariables(false, $variables);
+ $this->updateOrCreateVariables(false, $variables);
+
+ if ($this->showPreview) {
+ $previewVariables = parseEnvFormatToArray($this->variablesPreview);
+ $this->deleteRemovedVariables(true, $previewVariables);
+ $this->updateOrCreateVariables(true, $previewVariables);
+ }
- if ($isPreview) {
- $this->resource->environment_variables_preview()->whereNotIn('key', array_keys($variables))->delete();
+ $this->dispatch('success', 'Environment variables updated.');
} else {
- $this->resource->environment_variables()->whereNotIn('key', array_keys($variables))->delete();
- }
-
- foreach ($variables as $key => $value) {
- $found = $isPreview
- ? $this->resource->environment_variables_preview()->where('key', $key)->first()
- : $this->resource->environment_variables()->where('key', $key)->first();
-
+ // Handle the case when adding a single variable
+ $found = $this->resource->environment_variables()->where('key', $data['key'])->first();
if ($found) {
- if (!$found->is_shown_once && !$found->is_multiline) {
- $found->value = $value;
- $found->save();
- }
- } else {
- $environment = new EnvironmentVariable;
- $environment->key = $key;
- $environment->value = $value;
- $environment->is_build_time = false;
- $environment->is_multiline = false;
- $environment->is_preview = $isPreview;
-
- $this->setEnvironmentResourceId($environment);
- $environment->save();
+ $this->dispatch('error', 'Environment variable already exists.');
+ return;
+ }
+
+ $environment = new EnvironmentVariable;
+ $environment->key = $data['key'];
+ $environment->value = $data['value'];
+ $environment->is_build_time = $data['is_build_time'] ?? false;
+ $environment->is_multiline = $data['is_multiline'] ?? false;
+ $environment->is_literal = $data['is_literal'] ?? false;
+ $environment->is_preview = $data['is_preview'] ?? false;
+
+ $resourceType = $this->resource->type();
+ $resourceIdField = $this->getResourceIdField($resourceType);
+
+ if ($resourceIdField) {
+ $environment->$resourceIdField = $this->resource->id;
}
+
+ $environment->save();
}
$this->refreshEnvs();
- $this->dispatch('success', 'Environment variables saved successfully.');
+ $this->sortMe();
} catch (\Throwable $e) {
return handleError($e, $this);
}
}
+ private function getResourceIdField($resourceType)
+ {
+ $resourceTypes = [
+ 'application' => 'application_id',
+ 'standalone-postgresql' => 'standalone_postgresql_id',
+ 'standalone-redis' => 'standalone_redis_id',
+ 'standalone-mongodb' => 'standalone_mongodb_id',
+ 'standalone-mysql' => 'standalone_mysql_id',
+ 'standalone-mariadb' => 'standalone_mariadb_id',
+ 'standalone-keydb' => 'standalone_keydb_id',
+ 'standalone-dragonfly' => 'standalone_dragonfly_id',
+ 'standalone-clickhouse' => 'standalone_clickhouse_id',
+ 'service' => 'service_id',
+ ];
+
+ return $resourceTypes[$resourceType] ?? null;
+ }
+
+ private function deleteRemovedVariables($isPreview, $variables)
+ {
+ $method = $isPreview ? 'environment_variables_preview' : 'environment_variables';
+ $this->resource->$method()->whereNotIn('key', array_keys($variables))->delete();
+ }
+
+ private function updateOrCreateVariables($isPreview, $variables)
+ {
+ foreach ($variables as $key => $value) {
+ $method = $isPreview ? 'environment_variables_preview' : 'environment_variables';
+ $found = $this->resource->$method()->where('key', $key)->first();
+
+ if ($found) {
+ if (!$found->is_shown_once && !$found->is_multiline) {
+ $found->value = $value;
+ $found->save();
+ }
+ } else {
+ $environment = new EnvironmentVariable;
+ $environment->key = $key;
+ $environment->value = $value;
+ $environment->is_build_time = false;
+ $environment->is_multiline = false;
+ $environment->is_preview = $isPreview;
+
+ $this->setEnvironmentResourceId($environment);
+ $environment->save();
+ }
+ }
+ }
+
private function setEnvironmentResourceId($environment)
{
- switch ($this->resource->type()) {
- case 'application':
- $environment->application_id = $this->resource->id;
- break;
- case 'standalone-postgresql':
- $environment->standalone_postgresql_id = $this->resource->id;
- break;
- case 'standalone-redis':
- $environment->standalone_redis_id = $this->resource->id;
- break;
- case 'standalone-mongodb':
- $environment->standalone_mongodb_id = $this->resource->id;
- break;
- case 'standalone-mysql':
- $environment->standalone_mysql_id = $this->resource->id;
- break;
- case 'standalone-mariadb':
- $environment->standalone_mariadb_id = $this->resource->id;
- break;
- case 'standalone-keydb':
- $environment->standalone_keydb_id = $this->resource->id;
- break;
- case 'standalone-dragonfly':
- $environment->standalone_dragonfly_id = $this->resource->id;
- break;
- case 'standalone-clickhouse':
- $environment->standalone_clickhouse_id = $this->resource->id;
- break;
- case 'service':
- $environment->service_id = $this->resource->id;
- break;
+ $resourceTypes = [
+ 'application' => 'application_id',
+ 'standalone-postgresql' => 'standalone_postgresql_id',
+ 'standalone-redis' => 'standalone_redis_id',
+ 'standalone-mongodb' => 'standalone_mongodb_id',
+ 'standalone-mysql' => 'standalone_mysql_id',
+ 'standalone-mariadb' => 'standalone_mariadb_id',
+ 'standalone-keydb' => 'standalone_keydb_id',
+ 'standalone-dragonfly' => 'standalone_dragonfly_id',
+ 'standalone-clickhouse' => 'standalone_clickhouse_id',
+ 'service' => 'service_id',
+ ];
+
+ $resourceType = $this->resource->type();
+ if (isset($resourceTypes[$resourceType])) {
+ $environment->{$resourceTypes[$resourceType]} = $this->resource->id;
}
}
diff --git a/resources/views/livewire/project/shared/environment-variable/all.blade.php b/resources/views/livewire/project/shared/environment-variable/all.blade.php
index 5226199c69..1fd4160ae6 100644
--- a/resources/views/livewire/project/shared/environment-variable/all.blade.php
+++ b/resources/views/livewire/project/shared/environment-variable/all.blade.php
@@ -2,9 +2,11 @@
<div>
<div class="flex items-center gap-2">
<h2>Environment Variables</h2>
- <x-modal-input buttonTitle="+ Add" title="New Environment Variable">
- <livewire:project.shared.environment-variable.add />
- </x-modal-input>
+ <div class="flex flex-col items-center">
+ <x-modal-input buttonTitle="+ Add" title="New Environment Variable">
+ <livewire:project.shared.environment-variable.add />
+ </x-modal-input>
+ </div>
<x-forms.button
wire:click='switch'>{{ $view === 'normal' ? 'Developer view' : 'Normal view' }}</x-forms.button>
</div>
@@ -49,14 +51,14 @@
@endif
@else
<form wire:submit.prevent='submit' class="flex flex-col gap-2">
- <x-forms.textarea rows="10" class="whitespace-pre-wrap" id="variables"></x-forms.textarea>
+ <x-forms.textarea rows="10" class="whitespace-pre-wrap" id="variables" wire:model="variables" label="Production Environment Variables"></x-forms.textarea>
@if ($showPreview)
<x-forms.textarea rows="10" class="whitespace-pre-wrap" label="Preview Environment Variables"
- id="variablesPreview"></x-forms.textarea>
+ id="variablesPreview" wire:model="variablesPreview"></x-forms.textarea>
@endif
- <x-forms.button type="submit" class="btn btn-primary">Save Environment Variables</x-forms.button>
+ <x-forms.button type="submit" class="btn btn-primary">Save All Environment Variables</x-forms.button>
</form>
@endif
</div>
\ No newline at end of file