themes how part of site settings

This commit is contained in:
Mark Gregory
2026-07-28 12:43:19 +01:00
parent eafc5e8063
commit f834328044
12 changed files with 144 additions and 18 deletions
+2
View File
@@ -2,6 +2,8 @@
return [
'default_theme' => env('STYLIST_DEFAULT_THEME', 'admin'),
'default_backend_theme' => env('STYLIST_BACKEND_THEME', 'admin'),
'default_frontend_theme' => env('STYLIST_FRONTEND_THEME', 'saasland'),
/*
| Themes are application resources, never module resources.
+14
View File
@@ -35,6 +35,20 @@ if (! function_exists('stylist_theme_view')) {
}
}
if (! function_exists('stylist_frontend_view')) {
function stylist_frontend_view(string $view, array $data = [], array $mergeData = []): View
{
return app(ThemeViewResolver::class)->makeForTheme(stylist()->frontend()->key(), $view, $data, $mergeData);
}
}
if (! function_exists('stylist_backend_view')) {
function stylist_backend_view(string $view, array $data = [], array $mergeData = []): View
{
return app(ThemeViewResolver::class)->makeForTheme(stylist()->backend()->key(), $view, $data, $mergeData);
}
}
if (! function_exists('theme_asset')) {
function theme_asset(string $path): string
{
@@ -3,11 +3,15 @@
namespace Modules\Stylist\Services;
use Illuminate\Support\Facades\Auth;
use Modules\Settings\Services\SettingsRepository;
final class TenantThemeResolver
{
public function resolve(): string
public function __construct(private readonly SettingsRepository $settings) {}
public function resolve(?string $context = null): string
{
$context ??= $this->context();
$column = config('stylist.tenant_theme_column', 'theme_key');
$user = Auth::user();
$company = $user?->company ?? null;
@@ -23,7 +27,26 @@ final class TenantThemeResolver
}
}
return config('stylist.default_theme', 'default');
$settingKey = $context === 'frontend' ? 'frontend_theme' : 'backend_theme';
$default = $context === 'frontend'
? config('stylist.default_frontend_theme', 'saasland')
: config('stylist.default_backend_theme', config('stylist.default_theme', 'admin'));
return (string) $this->settings->get($settingKey, $default, 'site');
}
public function context(): string
{
if (app()->runningInConsole()) {
return 'backend';
}
$request = request();
return $request->routeIs('home', 'blog.public.*', 'news.public.*', 'pages.public.*')
|| ! $request->user()
? 'frontend'
: 'backend';
}
public function variables(): array
+15 -3
View File
@@ -10,13 +10,15 @@ final class Stylist
{
public function __construct(private readonly Loader $loader, private readonly TenantThemeResolver $resolver, private readonly ThemeSettings $settings) {}
public function current(): Theme
public function current(?string $context = null): Theme
{
$requested = $this->resolver->resolve();
$requested = $this->resolver->resolve($context);
try {
return $this->loader->find($requested);
} catch (ThemeNotFoundException) {
$fallback = (string) config('stylist.default_theme', 'default');
$fallback = $context === 'frontend'
? (string) config('stylist.default_frontend_theme', 'saasland')
: (string) config('stylist.default_backend_theme', config('stylist.default_theme', 'admin'));
if ($requested !== $fallback) {
return $this->loader->find($fallback);
}
@@ -24,6 +26,16 @@ final class Stylist
}
}
public function frontend(): Theme
{
return $this->current('frontend');
}
public function backend(): Theme
{
return $this->current('backend');
}
public function variables(): array
{
$theme = $this->current();
+5
View File
@@ -64,6 +64,11 @@ final class Theme
return (string) ($this->meta['description'] ?? '');
}
public function type(): string
{
return (string) ($this->meta['type'] ?? 'frontend');
}
public function author(): string
{
return (string) ($this->meta['author'] ?? '');