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

117 lines
3.6 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;
2023-07-13 11:16:24 +00:00
use Visus\Cuid2\Cuid2;
2023-05-04 20:29:14 +00:00
class Show extends Component
{
public $parameters;
2024-01-23 16:13:23 +00:00
public ModelsEnvironmentVariable|SharedEnvironmentVariable $env;
2023-09-22 09:23:49 +00:00
public ?string $modalId = null;
2023-09-27 13:48:19 +00:00
public bool $isDisabled = false;
2023-10-24 13:41:21 +00:00
public bool $isLocked = false;
2024-01-23 16:13:23 +00:00
public bool $isSharedVariable = false;
2023-09-22 09:23:49 +00:00
public string $type;
protected $listeners = [
"compose_loaded" => '$refresh',
];
2023-09-22 09:23:49 +00:00
2023-05-04 20:29:14 +00:00
protected $rules = [
'env.key' => 'required|string',
2023-09-26 12:45:52 +00:00
'env.value' => 'nullable',
2023-05-04 20:29:14 +00:00
'env.is_build_time' => 'required|boolean',
'env.is_multiline' => 'required|boolean',
2024-04-15 10:46:22 +00:00
'env.is_literal' => 'required|boolean',
2023-10-24 13:41:21 +00:00
'env.is_shown_once' => 'required|boolean',
2024-01-23 16:13:23 +00:00
'env.real_value' => 'nullable',
2023-05-04 20:29:14 +00:00
];
2023-06-16 10:35:40 +00:00
protected $validationAttributes = [
2024-01-23 16:13:23 +00:00
'env.key' => 'Key',
'env.value' => 'Value',
'env.is_build_time' => 'Build Time',
'env.is_multiline' => 'Multiline',
2024-04-15 10:46:22 +00:00
'env.is_literal' => 'Literal',
2024-01-23 16:13:23 +00:00
'env.is_shown_once' => 'Shown Once',
2023-06-16 10:35:40 +00:00
];
2023-05-04 20:29:14 +00:00
public function mount()
2023-10-24 13:41:21 +00:00
{
2024-01-23 16:13:23 +00:00
if ($this->env->getMorphClass() === 'App\Models\SharedEnvironmentVariable') {
$this->isSharedVariable = true;
}
2023-10-24 13:41:21 +00:00
$this->modalId = new Cuid2(7);
$this->parameters = get_route_parameters();
$this->checkEnvs();
}
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-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') {
data_forget($this->env, 'is_build_time');
}
}
2023-10-24 13:41:21 +00:00
public function lock()
{
$this->env->is_shown_once = true;
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
}
2023-08-11 20:41:47 +00:00
public function instantSave()
{
$this->submit();
}
2023-05-04 20:29:14 +00:00
public function submit()
{
2024-01-23 16:13:23 +00:00
try {
if ($this->isSharedVariable) {
$this->validate([
'env.key' => 'required|string',
'env.value' => 'nullable',
'env.is_shown_once' => 'required|boolean',
]);
} else {
$this->validate();
}
2024-01-31 12:40:15 +00:00
if (str($this->env->value)->startsWith('{{') && str($this->env->value)->endsWith('}}')) {
$type = str($this->env->value)->after("{{")->before(".")->value;
if (!collect(SHARED_VARIABLE_TYPES)->contains($type)) {
2024-01-31 12:40:15 +00:00
$this->dispatch('error', 'Invalid shared variable type.', "Valid types are: team, project, environment.");
return;
}
}
2024-01-23 16:13:23 +00:00
$this->serialize();
$this->env->save();
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Environment variable updated.');
2024-01-23 16:13:23 +00:00
$this->dispatch('refreshEnvs');
2024-01-31 12:40:15 +00:00
} 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('refreshEnvs');
} catch (\Exception $e) {
return handleError($e);
}
2023-05-04 20:29:14 +00:00
}
}