Files
fewo-stommer/app/Themes/ThemeFactory.php
LarsandCursor 8ebc1a3878 Assign the existing site to one landlord so stommer.info can run tenancy.
Seebad 0.3 stays in this release. With a single landlord every host still reaches the default website.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-23 12:00:11 +02:00

118 lines
3.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Themes;
use App\Models\Appearance;
use App\Models\Site;
use App\Models\SiteDomain;
use App\Models\Tenant;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Schema;
/**
* Builds the theme context: the site by host name (else the default site), the appearance in effect today –
* or the one named in a signed preview link (?vorschau=<id>).
*/
class ThemeFactory
{
public function __construct(private ThemeRegistry $registry) {}
public function forRequest(Request $request): Theme
{
$site = $this->siteForRequest($request->getHost());
if ($site === null) {
abort(404);
}
$isSigned = ($request->query('vorschau') || $request->query('theme')) && $request->hasValidSignature();
$preview = $isSigned && $request->query('vorschau') ? $site->appearances()->find($request->query('vorschau')) : null;
// Theme preview: the site is shown with another installed theme without changing it (not saved).
$previewTheme = $isSigned ? (string) $request->query('theme') : '';
if ($previewTheme !== '' && isset($this->registry->all()[$previewTheme])) {
$site = (clone $site)->setAttribute('theme', $previewTheme);
return $this->forSite($site, $preview, isPreview: true);
}
return $this->forSite($site, $preview);
}
public function forSite(Site $site, ?Appearance $preview = null, bool $isPreview = false): Theme
{
return new Theme(
$this->registry,
$site,
$preview ?? ($site->exists ? $site->appearanceOn(today()) : null),
isPreview: $isPreview || $preview !== null,
);
}
/**
* The website for a console command: a matching host, otherwise the default website.
*/
public function site(?string $host = null): Site
{
return $this->matchedSite($host) ?? $this->defaultSite();
}
/**
* The website for an HTTP request. With two or more landlords an unknown host is not a website.
*/
public function siteForRequest(?string $host): ?Site
{
if (! Schema::hasTable('sites')) {
return $this->fallbackSite();
}
$matched = $this->matchedSite($host);
if ($matched !== null) {
return $matched;
}
if (Schema::hasTable('tenants') && Tenant::query()->count() > 1) {
return null;
}
return $this->defaultSite();
}
private function matchedSite(?string $host): ?Site
{
$host = is_string($host) ? mb_strtolower(trim($host)) : '';
if ($host === '' || ! Schema::hasTable('sites')) {
return null;
}
if (Schema::hasTable('site_domains')) {
$siteId = SiteDomain::query()->where('host', $host)->value('site_id');
if ($siteId) {
return Site::withoutGlobalScope('tenant')->find($siteId);
}
}
return Site::withoutGlobalScope('tenant')->whereRaw('lower(domain) = ?', [$host])->first();
}
private function defaultSite(): Site
{
if (! Schema::hasTable('sites')) {
return $this->fallbackSite();
}
return Site::withoutGlobalScope('tenant')->where('is_default', true)->first()
?? Site::withoutGlobalScope('tenant')->orderBy('id')->first()
?? $this->fallbackSite();
}
private function fallbackSite(): Site
{
return new Site(['name' => config('app.name'), 'theme' => config('themes.base')]);
}
}