lasthourcloud/app/Livewire/Project/Service/StackForm.php

96 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Service;
2024-03-21 11:44:32 +00:00
use App\Models\Service;
use Illuminate\Support\Collection;
use Livewire\Component;
class StackForm extends Component
{
2024-03-21 11:44:32 +00:00
public Service $service;
2024-06-10 20:43:34 +00:00
public Collection $fields;
2024-06-10 20:43:34 +00:00
protected $listeners = ['saveCompose'];
public $rules = [
'service.docker_compose_raw' => 'required',
'service.docker_compose' => 'required',
'service.name' => 'required',
'service.description' => 'nullable',
'service.connect_to_docker_network' => 'nullable',
];
2024-06-10 20:43:34 +00:00
public $validationAttributes = [];
2024-06-10 20:43:34 +00:00
public function mount()
{
$this->fields = collect([]);
$extraFields = $this->service->extraFields();
foreach ($extraFields as $serviceName => $fields) {
foreach ($fields as $fieldKey => $field) {
$key = data_get($field, 'key');
$value = data_get($field, 'value');
$rules = data_get($field, 'rules', 'nullable');
$isPassword = data_get($field, 'isPassword');
$this->fields->put($key, [
2024-06-10 20:43:34 +00:00
'serviceName' => $serviceName,
'key' => $key,
'name' => $fieldKey,
'value' => $value,
'isPassword' => $isPassword,
'rules' => $rules,
]);
$this->rules["fields.$key.value"] = $rules;
$this->validationAttributes["fields.$key.value"] = $fieldKey;
}
2023-11-11 20:32:41 +00:00
}
$this->fields = $this->fields->sortBy('name');
2023-11-11 20:32:41 +00:00
}
2024-06-10 20:43:34 +00:00
public function saveCompose($raw)
{
$this->service->docker_compose_raw = $raw;
$this->submit();
}
2024-06-10 20:43:34 +00:00
public function instantSave()
{
$this->service->save();
$this->dispatch('success', 'Service settings saved.');
}
public function submit()
{
try {
$this->validate();
2024-03-14 08:21:48 +00:00
$isValid = validateComposeFile($this->service->docker_compose_raw, $this->service->server->id);
if ($isValid !== 'OK') {
throw new \Exception("Invalid docker-compose file.\n$isValid");
}
$this->service->save();
$this->service->saveExtraFields($this->fields);
$this->service->parse();
$this->service->refresh();
$this->service->saveComposeConfigs();
2023-12-07 18:06:32 +00:00
$this->dispatch('refreshEnvs');
2024-02-22 13:53:42 +00:00
$this->dispatch('success', 'Service saved.');
} catch (\Throwable $e) {
return handleError($e, $this);
} finally {
if (is_null($this->service->config_hash)) {
$this->service->isConfigurationChanged(true);
} else {
$this->dispatch('configurationChanged');
}
}
}
2024-06-10 20:43:34 +00:00
public function render()
{
return view('livewire.project.service.stack-form');
}
}