2023-08-07 20:14:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire\Project\Shared\EnvironmentVariable;
|
|
|
|
|
|
|
|
use App\Models\EnvironmentVariable;
|
|
|
|
use Livewire\Component;
|
|
|
|
use Visus\Cuid2\Cuid2;
|
2023-09-15 09:19:36 +00:00
|
|
|
use Illuminate\Support\Str;
|
2023-08-07 20:14:21 +00:00
|
|
|
|
|
|
|
class All extends Component
|
|
|
|
{
|
|
|
|
public $resource;
|
2023-09-08 14:16:59 +00:00
|
|
|
public bool $showPreview = false;
|
2023-08-07 20:14:21 +00:00
|
|
|
public string|null $modalId = null;
|
2023-09-08 14:16:59 +00:00
|
|
|
public ?string $variables = null;
|
|
|
|
public ?string $variablesPreview = null;
|
|
|
|
public string $view = 'normal';
|
2023-08-07 20:14:21 +00:00
|
|
|
protected $listeners = ['refreshEnvs', 'submit'];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-07 20:14:21 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2023-09-08 14:16:59 +00:00
|
|
|
$resourceClass = get_class($this->resource);
|
|
|
|
$resourceWithPreviews = ['App\Models\Application'];
|
|
|
|
$simpleDockerfile = !is_null(data_get($this->resource, 'dockerfile'));
|
|
|
|
if (Str::of($resourceClass)->contains($resourceWithPreviews) && !$simpleDockerfile) {
|
|
|
|
$this->showPreview = true;
|
|
|
|
}
|
2023-08-07 20:14:21 +00:00
|
|
|
$this->modalId = new Cuid2(7);
|
2023-09-08 14:16:59 +00:00
|
|
|
$this->getDevView();
|
|
|
|
}
|
|
|
|
public function getDevView()
|
|
|
|
{
|
|
|
|
$this->variables = $this->resource->environment_variables->map(function ($item) {
|
|
|
|
return "$item->key=$item->value";
|
|
|
|
})->sort()->join('
|
|
|
|
');
|
|
|
|
if ($this->showPreview) {
|
|
|
|
$this->variablesPreview = $this->resource->environment_variables_preview->map(function ($item) {
|
|
|
|
return "$item->key=$item->value";
|
|
|
|
})->sort()->join('
|
|
|
|
');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public function switch()
|
|
|
|
{
|
|
|
|
$this->view = $this->view === 'normal' ? 'dev' : 'normal';
|
|
|
|
}
|
|
|
|
public function saveVariables($isPreview)
|
|
|
|
{
|
|
|
|
if ($isPreview) {
|
|
|
|
$variables = parseEnvFormatToArray($this->variablesPreview);
|
|
|
|
$existingVariables = $this->resource->environment_variables_preview();
|
|
|
|
$this->resource->environment_variables_preview()->delete();
|
|
|
|
} else {
|
|
|
|
$variables = parseEnvFormatToArray($this->variables);
|
|
|
|
$existingVariables = $this->resource->environment_variables();
|
|
|
|
$this->resource->environment_variables()->delete();
|
|
|
|
}
|
|
|
|
foreach ($variables as $key => $variable) {
|
|
|
|
$found = $existingVariables->where('key', $key)->first();
|
|
|
|
if ($found) {
|
|
|
|
$found->value = $variable;
|
|
|
|
$found->save();
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
$environment = new EnvironmentVariable();
|
|
|
|
$environment->key = $key;
|
|
|
|
$environment->value = $variable;
|
|
|
|
$environment->is_build_time = false;
|
|
|
|
$environment->is_preview = $isPreview ? true : false;
|
2023-09-22 09:23:49 +00:00
|
|
|
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 'service':
|
|
|
|
$environment->service_id = $this->resource->id;
|
|
|
|
break;
|
2023-09-08 14:16:59 +00:00
|
|
|
}
|
|
|
|
$environment->save();
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 20:29:47 +00:00
|
|
|
if ($isPreview) {
|
|
|
|
$this->emit('success', 'Preview environment variables updated successfully.');
|
|
|
|
} else {
|
|
|
|
$this->emit('success', 'Environment variables updated successfully.');
|
|
|
|
}
|
2023-09-08 14:16:59 +00:00
|
|
|
$this->refreshEnvs();
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
|
|
|
public function refreshEnvs()
|
|
|
|
{
|
|
|
|
$this->resource->refresh();
|
2023-09-08 14:16:59 +00:00
|
|
|
$this->getDevView();
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-07 20:14:21 +00:00
|
|
|
public function submit($data)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$found = $this->resource->environment_variables()->where('key', $data['key'])->first();
|
|
|
|
if ($found) {
|
|
|
|
$this->emit('error', 'Environment variable already exists.');
|
|
|
|
return;
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
$environment = new EnvironmentVariable();
|
2023-08-07 20:14:21 +00:00
|
|
|
$environment->key = $data['key'];
|
|
|
|
$environment->value = $data['value'];
|
|
|
|
$environment->is_build_time = $data['is_build_time'];
|
|
|
|
$environment->is_preview = $data['is_preview'];
|
|
|
|
|
2023-09-26 12:45:52 +00:00
|
|
|
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 'service':
|
|
|
|
$environment->service_id = $this->resource->id;
|
|
|
|
break;
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
|
|
|
$environment->save();
|
2023-09-08 14:16:59 +00:00
|
|
|
$this->refreshEnvs();
|
2023-08-07 20:14:21 +00:00
|
|
|
$this->emit('success', 'Environment variable added successfully.');
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-15 13:34:25 +00:00
|
|
|
return handleError($e, $this);
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|