cms/app/Providers/AppServiceProvider.php

70 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Gate;
use App\Models\User;
use App\Support\NavigationManager;
use App\Models\NavigationItem;
use App\Support\ThemeManager;
use App\Services\TranslationManager;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
$this->app->singleton(NavigationManager::class, function () {
return new NavigationManager();
});
$this->app->singleton(TranslationManager::class, function ($app) {
return new TranslationManager();
});
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
// Register default themes path for Blade views (static fallback)
View::addNamespace('themes', base_path('themes'));
// Register Gates
Gate::before(function (User $user, string $ability) {
if ($user->hasPermission($ability)) {
return true;
}
});
// Share navigation with all views
View::composer('*', function ($view) {
$navManager = app(NavigationManager::class);
// Load DB items if not already loaded (or just always merge for now)
// To avoid multiple DB calls, we could cache this or just do it once.
$dbItems = NavigationItem::with(['page', 'children.page'])->whereNull('parent_id')->orderBy('order')->get();
foreach ($dbItems as $item) {
$navManager->register((string)$item->id, $item->label, $item->final_url, [
'target' => $item->target,
'order' => $item->order,
'children' => $item->children->map(fn($child) => [
'label' => $child->label,
'url' => $child->final_url,
'target' => $child->target,
])->toArray()
]);
}
$view->with('navigation', $navManager->getItems());
});
}
}