97 lines
2.7 KiB
PHP
97 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Stylist\Services;
|
|
|
|
use App\Models\SiteSetting;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
final class TenantThemeResolver
|
|
{
|
|
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;
|
|
|
|
/*
|
|
* The legacy company theme_key is a public-facing tenant theme.
|
|
* It must never replace the application's backend/admin theme.
|
|
*/
|
|
if ($context === 'frontend' && $company && filled($company->{$column} ?? null)) {
|
|
return (string) $company->{$column};
|
|
}
|
|
|
|
$settingKey = $context === 'frontend' ? 'frontend_theme' : 'backend_theme';
|
|
$default = $context === 'frontend'
|
|
? config('stylist.default_frontend_theme', 'frontend')
|
|
: config('stylist.default_backend_theme', config('stylist.default_theme', 'backend'));
|
|
|
|
try {
|
|
return (string) SiteSetting::valueOf($settingKey, $default);
|
|
} catch (\Throwable) {
|
|
return (string) $default;
|
|
}
|
|
}
|
|
|
|
public function context(): string
|
|
{
|
|
if (app()->runningInConsole()) {
|
|
return 'backend';
|
|
}
|
|
|
|
$request = request();
|
|
|
|
return $request->is(
|
|
'dashboard',
|
|
'dashboard/*',
|
|
'settings',
|
|
'settings/*',
|
|
'admin',
|
|
'admin/*',
|
|
'helpdesk',
|
|
'helpdesk/*',
|
|
'login',
|
|
'register',
|
|
'forgot-password',
|
|
'reset-password',
|
|
'reset-password/*',
|
|
'confirm-password',
|
|
'user/confirm-password',
|
|
'two-factor-challenge',
|
|
'verify-email',
|
|
'verify-email/*',
|
|
'email/verify',
|
|
'email/verify/*',
|
|
) || $request->routeIs(
|
|
'dashboard',
|
|
'profile.edit',
|
|
'appearance.edit',
|
|
'security.edit',
|
|
'admin.*',
|
|
'helpdesk.*',
|
|
'login',
|
|
'register',
|
|
'password.*',
|
|
'verification.*',
|
|
'two-factor.*',
|
|
)
|
|
? 'backend'
|
|
: 'frontend';
|
|
}
|
|
|
|
public function variables(): array
|
|
{
|
|
$variables = config('stylist.fallback_variables', []);
|
|
$company = Auth::user()?->company ?? null;
|
|
|
|
foreach (config('stylist.tenant_brand_columns', []) as $column => $variable) {
|
|
if ($company && filled($company->{$column} ?? null)) {
|
|
$variables[$variable] = (string) $company->{$column};
|
|
}
|
|
}
|
|
|
|
return $variables;
|
|
}
|
|
}
|