coolify/app/Support/DomainUrlParts.php
Andras Bacsai 6028461f92 fix(domains): unify editing and queued DNS checks
Add indexing, redirect, and hostname regeneration controls to domain editors. Queue DNS checks only for scheme or hostname changes and show progress consistently across application, service, and preview domains.
2026-09-10 14:00:50 +02:00

65 lines
2 KiB
PHP

<?php
namespace App\Support;
class DomainUrlParts
{
public static function compose(string $scheme, string $host, ?string $port = '', string $path = ''): string
{
$scheme = strtolower(trim($scheme)) === 'http' ? 'http' : 'https';
$host = trim($host);
$port = trim((string) $port);
$path = trim($path);
if ($path !== '' && ! str_starts_with($path, '/') && ! str_starts_with($path, '?') && ! str_starts_with($path, '#')) {
$path = '/'.$path;
}
return $scheme.'://'.$host.($port !== '' ? ':'.$port : '').$path;
}
/**
* @return array{scheme: string, host: string, port: string, path: string}
*/
public static function split(?string $url): array
{
$parts = filled($url) ? parse_url($url) : false;
if (! is_array($parts) || blank($parts['host'] ?? null)) {
return self::empty();
}
$path = $parts['path'] ?? '';
if (isset($parts['query'])) {
$path .= '?'.$parts['query'];
}
if (isset($parts['fragment'])) {
$path .= '#'.$parts['fragment'];
}
return [
'scheme' => in_array(strtolower($parts['scheme'] ?? ''), ['http', 'https'], true)
? strtolower($parts['scheme'])
: 'https',
'host' => (string) $parts['host'],
'port' => isset($parts['port']) ? (string) $parts['port'] : '',
'path' => $path,
];
}
public static function hasDnsRelevantChange(string $oldUrl, string $newUrl): bool
{
$old = self::split($oldUrl);
$new = self::split($newUrl);
return $old['scheme'] !== $new['scheme']
|| strtolower($old['host']) !== strtolower($new['host']);
}
/**
* @return array{scheme: string, host: string, port: string, path: string}
*/
public static function empty(): array
{
return ['scheme' => 'https', 'host' => '', 'port' => '', 'path' => ''];
}
}