Files
Atomcms-edit/app/Services/ThemeService.php
T

70 lines
1.6 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\View;
class ThemeService
{
private const string DEFAULT_THEME = 'atom';
private ?string $activeTheme = null;
private ?string $parentTheme = null;
public function set(string $theme, ?string $parentTheme = null): void
{
$this->clear();
$basePath = config('theme.base_path', base_path('resources/themes'));
if ($parentTheme) {
View::prependLocation("{$basePath}/{$parentTheme}/views");
$this->parentTheme = $parentTheme;
}
View::prependLocation("{$basePath}/{$theme}/views");
$this->activeTheme = $theme;
$this->registerVendorNamespaces($theme, $basePath);
}
public function active(): ?string
{
return $this->activeTheme;
}
public function parent(): ?string
{
return $this->parentTheme;
}
public function resolve(?string $theme = null): string
{
return $theme ?? $this->activeTheme ?? self::DEFAULT_THEME;
}
public function clear(): void
{
$this->activeTheme = null;
$this->parentTheme = null;
}
private function registerVendorNamespaces(string $theme, string $basePath): void
{
$vendorPath = "{$basePath}/{$theme}/views/vendor";
if (! is_dir($vendorPath)) {
return;
}
$directories = scandir($vendorPath);
foreach ($directories as $namespace) {
if ($namespace !== '.' && $namespace !== '..') {
View::prependNamespace($namespace, "{$vendorPath}/{$namespace}");
}
}
}
}