51 lines
1.7 KiB
PHP
51 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Stylist\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\View;
|
|
use Modules\Stylist\Theme\Loader;
|
|
use Modules\Stylist\Theme\Stylist;
|
|
use Throwable;
|
|
|
|
final class ApplyActiveTheme
|
|
{
|
|
public function handle(Request $request, Closure $next)
|
|
{
|
|
$context = $request->is('admin', 'admin/*', 'stylist', 'stylist/*')
|
|
|| $request->routeIs('admin.*', 'stylist.*') ? 'backend' : 'frontend';
|
|
|
|
try {
|
|
$loader = app(Loader::class);
|
|
$theme = app(Stylist::class)->current($context);
|
|
if ($theme->type() !== $context) {
|
|
$theme = $loader->find($context === 'backend' ? 'sonardesk-admin' : 'sonardesk');
|
|
}
|
|
|
|
$paths = [];
|
|
$seen = [];
|
|
while ($theme && ! isset($seen[$theme->key()])) {
|
|
$seen[$theme->key()] = true;
|
|
$path = $theme->path().'/views';
|
|
if (is_dir($path)) {
|
|
$paths[] = $path;
|
|
}
|
|
$theme = $theme->parent() ? $loader->find($theme->parent()) : null;
|
|
}
|
|
$baseline = $loader->find($context === 'backend' ? 'sonardesk-admin' : 'sonardesk');
|
|
$paths[] = $baseline->path().'/views';
|
|
|
|
// Finder uses first match. Prepend in reverse order to preserve precedence.
|
|
foreach (array_reverse(array_unique($paths)) as $path) {
|
|
View::getFinder()->prependLocation($path);
|
|
}
|
|
} catch (Throwable $exception) {
|
|
report($exception);
|
|
// Original Laravel views remain available for recovery.
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
}
|