cleanup all.php

This commit is contained in:
ayntk-ai
2024-08-12 14:46:30 +02:00
parent 122491808c
commit e28289ce1e

View File

@@ -18,7 +18,7 @@ class All extends Component
protected $listeners = [ protected $listeners = [
'saveKey' => 'submit', 'saveKey' => 'submit',
//'environmentVariableDeleted' => 'refreshEnvs', 'environmentVariableDeleted' => 'refreshEnvs',
]; ];
protected $rules = [ protected $rules = [
@@ -41,23 +41,23 @@ class All extends Component
{ {
$sortBy = 'key'; // Always sort by key $sortBy = 'key'; // Always sort by key
$this->resource->load(['environment_variables', 'environment_variables_preview']); $this->resource->load(['environment_variables', 'environment_variables_preview']);
$this->resource->environment_variables = $this->resource->environment_variables->sortBy(function ($item) use ($sortBy) { $this->resource->environment_variables = $this->sortEnvironmentVariables($this->resource->environment_variables, $sortBy);
return strtolower($item->key); $this->resource->environment_variables_preview = $this->sortEnvironmentVariables($this->resource->environment_variables_preview, $sortBy);
}, SORT_NATURAL | SORT_FLAG_CASE)->values();
$this->resource->environment_variables_preview = $this->resource->environment_variables_preview->sortBy(function ($item) use ($sortBy) {
return strtolower($item->key);
}, SORT_NATURAL | SORT_FLAG_CASE)->values();
$this->getDevView(); $this->getDevView();
} }
private function sortEnvironmentVariables($variables, $sortBy)
{
return $variables->sortBy(function ($item) use ($sortBy) {
return strtolower($item->key);
}, SORT_NATURAL | SORT_FLAG_CASE)->values();
}
public function instantSave() public function instantSave()
{ {
if ($this->resourceClass === 'App\Models\Application' && data_get($this->resource, 'build_pack') !== 'dockercompose') { $this->resource->settings->save();
$this->resource->settings->save(); $this->sortMe();
$this->sortMe(); $this->dispatch('success', 'Environment variable settings updated.');
$this->dispatch('success', 'Environment variable settings updated.');
}
} }
public function getDevView() public function getDevView()
@@ -91,41 +91,9 @@ class All extends Component
{ {
try { try {
if ($data === null) { if ($data === null) {
$variables = parseEnvFormatToArray($this->variables); $this->handleBulkSubmit();
$this->deleteRemovedVariables(false, $variables);
$this->updateOrCreateVariables(false, $variables);
if ($this->showPreview) {
$previewVariables = parseEnvFormatToArray($this->variablesPreview);
$this->deleteRemovedVariables(true, $previewVariables);
$this->updateOrCreateVariables(true, $previewVariables);
}
$this->sortMe();
$this->dispatch('success', 'Environment variables updated.');
} else { } else {
$found = $this->resource->environment_variables()->where('key', $data['key'])->first(); $this->handleSingleSubmit($data);
if ($found) {
$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->sortMe(); $this->sortMe();
@@ -134,6 +102,53 @@ class All extends Component
} }
} }
private function handleBulkSubmit()
{
$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);
}
$this->dispatch('success', 'Environment variables updated.');
}
private function handleSingleSubmit($data)
{
$found = $this->resource->environment_variables()->where('key', $data['key'])->first();
if ($found) {
$this->dispatch('error', 'Environment variable already exists.');
return;
}
$environment = $this->createEnvironmentVariable($data);
$environment->save();
}
private function createEnvironmentVariable($data)
{
$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;
}
return $environment;
}
private function getResourceIdField($resourceType) private function getResourceIdField($resourceType)
{ {
$resourceTypes = [ $resourceTypes = [