coolify/app/Livewire/Project/Shared/EnvironmentVariable/Show.php

182 lines
5.2 KiB
PHP
Raw Normal View History

2023-05-04 20:29:14 +00:00
<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Shared\EnvironmentVariable;
2023-05-04 20:29:14 +00:00
use App\Models\EnvironmentVariable as ModelsEnvironmentVariable;
2024-01-23 16:13:23 +00:00
use App\Models\SharedEnvironmentVariable;
2023-05-04 20:29:14 +00:00
use Livewire\Component;
class Show extends Component
{
public $parameters;
2024-06-10 20:43:34 +00:00
2024-01-23 16:13:23 +00:00
public ModelsEnvironmentVariable|SharedEnvironmentVariable $env;
2024-06-10 20:43:34 +00:00
2023-09-27 13:48:19 +00:00
public bool $isDisabled = false;
2024-06-10 20:43:34 +00:00
2023-10-24 13:41:21 +00:00
public bool $isLocked = false;
2024-06-10 20:43:34 +00:00
2024-01-23 16:13:23 +00:00
public bool $isSharedVariable = false;
2024-06-10 20:43:34 +00:00
2023-09-22 09:23:49 +00:00
public string $type;
2024-06-10 20:43:34 +00:00
public string $key;
public ?string $value = null;
public ?string $real_value = null;
public bool $is_shared = false;
public bool $is_build_time = false;
public bool $is_multiline = false;
public bool $is_literal = false;
public bool $is_shown_once = false;
public bool $is_required = false;
public bool $is_really_required = false;
protected $listeners = [
'refreshEnvs' => 'refresh',
'refresh',
2024-06-10 20:43:34 +00:00
'compose_loaded' => '$refresh',
];
2023-09-22 09:23:49 +00:00
2023-05-04 20:29:14 +00:00
protected $rules = [
'key' => 'required|string',
'value' => 'nullable',
'is_build_time' => 'required|boolean',
'is_multiline' => 'required|boolean',
'is_literal' => 'required|boolean',
'is_shown_once' => 'required|boolean',
'real_value' => 'nullable',
'is_required' => 'required|boolean',
2023-05-04 20:29:14 +00:00
];
2024-06-10 20:43:34 +00:00
public function mount()
{
$this->syncData();
if ($this->env->getMorphClass() === \App\Models\SharedEnvironmentVariable::class) {
$this->isSharedVariable = true;
}
$this->parameters = get_route_parameters();
$this->checkEnvs();
}
public function refresh()
{
$this->syncData();
$this->checkEnvs();
}
public function syncData(bool $toModel = false)
2023-10-24 13:41:21 +00:00
{
if ($toModel) {
if ($this->isSharedVariable) {
$this->validate([
'key' => 'required|string',
'value' => 'nullable',
'is_multiline' => 'required|boolean',
'is_literal' => 'required|boolean',
'is_shown_once' => 'required|boolean',
'real_value' => 'nullable',
]);
} else {
$this->validate();
$this->env->is_build_time = $this->is_build_time;
$this->env->is_required = $this->is_required;
$this->env->is_shared = $this->is_shared;
}
$this->env->key = $this->key;
$this->env->value = $this->value;
$this->env->is_multiline = $this->is_multiline;
$this->env->is_literal = $this->is_literal;
$this->env->is_shown_once = $this->is_shown_once;
$this->env->save();
} else {
$this->key = $this->env->key;
$this->value = $this->env->value;
$this->is_build_time = $this->env->is_build_time ?? false;
$this->is_multiline = $this->env->is_multiline;
$this->is_literal = $this->env->is_literal;
$this->is_shown_once = $this->env->is_shown_once;
$this->is_required = $this->env->is_required ?? false;
$this->is_really_required = $this->env->is_really_required ?? false;
$this->is_shared = $this->env->is_shared ?? false;
$this->real_value = $this->env->real_value;
2024-01-23 16:13:23 +00:00
}
2023-10-24 13:41:21 +00:00
}
2024-06-10 20:43:34 +00:00
2023-10-24 13:41:21 +00:00
public function checkEnvs()
2023-05-04 20:29:14 +00:00
{
2023-09-27 13:48:19 +00:00
$this->isDisabled = false;
if (str($this->env->key)->startsWith('SERVICE_FQDN') || str($this->env->key)->startsWith('SERVICE_URL')) {
$this->isDisabled = true;
}
2023-10-24 13:41:21 +00:00
if ($this->env->is_shown_once) {
$this->isLocked = true;
}
}
2024-06-10 20:43:34 +00:00
2024-01-31 12:40:15 +00:00
public function serialize()
{
2024-01-23 16:13:23 +00:00
data_forget($this->env, 'real_value');
if ($this->env->getMorphClass() === \App\Models\SharedEnvironmentVariable::class) {
2024-01-23 16:13:23 +00:00
data_forget($this->env, 'is_build_time');
}
}
2024-06-10 20:43:34 +00:00
2023-10-24 13:41:21 +00:00
public function lock()
{
$this->env->is_shown_once = true;
if ($this->isSharedVariable) {
unset($this->env->is_required);
}
2024-01-23 16:13:23 +00:00
$this->serialize();
2023-10-24 13:41:21 +00:00
$this->env->save();
$this->checkEnvs();
2023-12-07 18:06:32 +00:00
$this->dispatch('refreshEnvs');
2023-05-04 20:29:14 +00:00
}
2024-06-10 20:43:34 +00:00
2023-08-11 20:41:47 +00:00
public function instantSave()
{
$this->submit();
}
2024-06-10 20:43:34 +00:00
2023-05-04 20:29:14 +00:00
public function submit()
{
2024-01-23 16:13:23 +00:00
try {
if (! $this->isSharedVariable && $this->is_required && str($this->value)->isEmpty()) {
2024-10-11 12:38:22 +00:00
$oldValue = $this->env->getOriginal('value');
$this->value = $oldValue;
$this->dispatch('error', 'Required environment variables cannot be empty.');
2024-10-11 12:38:22 +00:00
return;
2024-10-11 12:38:22 +00:00
}
2024-10-18 10:29:33 +00:00
2024-01-23 16:13:23 +00:00
$this->serialize();
$this->syncData(true);
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Environment variable updated.');
$this->dispatch('envsUpdated');
} catch (\Exception $e) {
2024-01-23 16:13:23 +00:00
return handleError($e);
}
2023-05-04 20:29:14 +00:00
}
2023-05-04 20:29:14 +00:00
public function delete()
{
2023-12-20 15:15:13 +00:00
try {
$this->env->delete();
$this->dispatch('environmentVariableDeleted');
$this->dispatch('success', 'Environment variable deleted successfully.');
} catch (\Exception $e) {
2023-12-20 15:15:13 +00:00
return handleError($e);
}
2023-05-04 20:29:14 +00:00
}
2024-09-04 12:33:16 +00:00
}