coolify/app/Livewire/Project/Service/EditDomain.php

93 lines
3 KiB
PHP
Raw Normal View History

2024-04-11 13:42:37 +00:00
<?php
namespace App\Livewire\Project\Service;
use App\Models\ServiceApplication;
use Livewire\Component;
use Spatie\Url\Url;
2024-04-11 13:42:37 +00:00
class EditDomain extends Component
{
public $applicationId;
2024-06-10 20:43:34 +00:00
2024-04-11 13:42:37 +00:00
public ServiceApplication $application;
2024-06-10 20:43:34 +00:00
public $domainConflicts = [];
public $showDomainConflictModal = false;
public $forceSaveDomains = false;
2024-04-11 13:42:37 +00:00
protected $rules = [
'application.fqdn' => 'nullable',
'application.required_fqdn' => 'required|boolean',
];
2024-06-10 20:43:34 +00:00
public function mount()
{
$this->application = ServiceApplication::find($this->applicationId);
2024-04-11 13:42:37 +00:00
}
2024-10-17 20:08:23 +00:00
public function confirmDomainUsage()
{
$this->forceSaveDomains = true;
$this->showDomainConflictModal = false;
$this->submit();
}
public function submit()
2024-04-11 13:42:37 +00:00
{
try {
$this->application->fqdn = str($this->application->fqdn)->replaceEnd(',', '')->trim();
$this->application->fqdn = str($this->application->fqdn)->replaceStart(',', '')->trim();
$this->application->fqdn = str($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
$domain = trim($domain);
Url::fromString($domain, ['http', 'https']);
2024-10-17 20:08:23 +00:00
return str($domain)->lower();
});
$this->application->fqdn = $this->application->fqdn->unique()->implode(',');
$warning = sslipDomainWarning($this->application->fqdn);
if ($warning) {
$this->dispatch('warning', __('warning.sslipdomain'));
}
// Check for domain conflicts if not forcing save
if (! $this->forceSaveDomains) {
$result = checkDomainUsage(resource: $this->application);
if ($result['hasConflicts']) {
$this->domainConflicts = $result['conflicts'];
$this->showDomainConflictModal = true;
return;
}
} else {
// Reset the force flag after using it
$this->forceSaveDomains = false;
}
2024-04-11 13:42:37 +00:00
$this->validate();
$this->application->save();
updateCompose($this->application);
if (str($this->application->fqdn)->contains(',')) {
$this->dispatch('warning', 'Some services do not support multiple domains, which can lead to problems and is NOT RECOMMENDED.<br><br>Only use multiple domains if you know what you are doing.');
}
$this->application->service->parse();
2024-04-11 13:42:37 +00:00
$this->dispatch('refresh');
fix: prevent duplicate services on image change and enable real-time UI refresh This commit addresses two critical issues with Docker Compose service management: ## Issue 1: Duplicate Services Created on Image Change When changing the image in a docker-compose file, the parser was creating new ServiceApplication/ServiceDatabase records instead of updating existing ones. **Root Cause**: The parsers used `firstOrCreate()` with `['name', 'image', 'service_id']`, meaning any image change would create a new record. **Fix**: Remove `image` from `firstOrCreate()` queries and update it separately after finding or creating the service record. **Changes**: - `bootstrap/helpers/parsers.php` (serviceParser v3): Fixed in presave loop (lines 1188-1203) and main parsing loop (lines 1519-1539) - `bootstrap/helpers/shared.php` (parseDockerComposeFile v2): Fixed null check logic (lines 1308-1348) ## Issue 2: UI Not Refreshing After Changes When compose file or domain was modified, the Configuration component wasn't receiving events to refresh its data, requiring manual page refresh to see updates. **Root Cause**: The Configuration component wasn't listening for refresh events dispatched by child components (StackForm, EditDomain). **Fix**: Add event listeners and dispatchers to enable real-time UI updates. **Changes**: - `app/Livewire/Project/Service/Configuration.php`: Added listeners for `refreshServices` and `refresh` events (lines 36-37) - `app/Livewire/Project/Service/EditDomain.php`: Added `refreshServices` dispatch (line 76) - Note: `app/Livewire/Project/Service/StackForm.php` already had the dispatch ## Tests Added - `tests/Unit/ServiceParserImageUpdateTest.php`: 4 tests verifying no duplicates created - `tests/Unit/ServiceConfigurationRefreshTest.php`: 4 tests verifying event dispatching All 8 new tests pass, and all existing unit tests continue to pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-14 08:12:36 +00:00
$this->dispatch('refreshServices');
$this->dispatch('configurationChanged');
} catch (\Throwable $e) {
$originalFqdn = $this->application->getOriginal('fqdn');
if ($originalFqdn !== $this->application->fqdn) {
$this->application->fqdn = $originalFqdn;
}
2024-10-17 20:08:23 +00:00
return handleError($e, $this);
2024-04-11 13:42:37 +00:00
}
}
2024-06-10 20:43:34 +00:00
2024-04-11 13:42:37 +00:00
public function render()
{
return view('livewire.project.service.edit-domain');
}
}