ldc-legaulois/app/Providers/ViewServiceProvider.php

96 lines
3.9 KiB
PHP

<?php
namespace App\Providers;
use App\Enums\Settings\SettingsType;
use App\Enums\StaticPageType;
use App\Models\GeneralSetting;
use App\Models\Meat;
use App\Models\Range;
use App\Models\StaticPage;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ViewServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
* phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
*/
public function boot(): void
{
// phpcs:enable
$appInfos = Cache::remember('composer', 360, function () {
return json_decode(File::get(\app_path('../composer.json')));
});
// Share composer.json infos.
if (isset($appInfos->name) and isset($appInfos->version)) {
View::share('cpsr_name', $appInfos->name);
View::share('cpsr_version', $appInfos->version);
}
if (!app()->runningInConsole()) {
// * FORCE BOOTSTRAP PAGINATOR (or custom if in front)
// Early boot, request wont be filled as expected.
(collect(explode('/', \request()->getPathInfo()))->get(1) === 'bo')
? Paginator::defaultView('bo.modules.pagination')
: Paginator::useBootstrapFive();
// * Inject actual view name
View::composer('*', function ($view) {
$view->with('viewName', $view->getName());
});
// * Inject menuNodeList
View::share('menuNodeList', (function () {
/** @var \App\Models\GeneralSetting|null */
$mainMenu = app('settings')->where('type', \App\Enums\Settings\SettingsType::mainMenu)->first();
if (!$mainMenu) {
throw new \RuntimeException('Missing setting ' .
\App\Enums\Settings\SettingsType::mainMenu->label());
}
return $mainMenu->value;
})());
View::share('menuNodeDataList', app('menuNodeDataList'));
// * Inject configuration model
View::share('mainMenuSettingModel', (function () {
return GeneralSetting::query()->where('type', SettingsType::mainMenu)->first() ?: null;
})());
View::share('hotNewsSettingModel', (function () {
return GeneralSetting::query()->where('type', SettingsType::hotNews)->first() ?: null;
})());
View::share('recipeSettingModel', (function () {
return GeneralSetting::query()->where('type', SettingsType::recipe)->first() ?: null;
})());
View::share('recipesSettingModel', (function () {
return GeneralSetting::query()->where('type', SettingsType::recipes)->first() ?: null;
})());
// * Inject some data for the bo menu.
View::share('firstMeat', (function () {
return Meat::query()->first() ?: null;
})());
View::share('firstRange', (function () {
return Range::query()->first() ?: null;
})());
View::share('mainMenuMeatModels', (function () {
return Meat::query()->where('published', true)->get();
})());
View::share('storyStaticPageModel', (function () {
return StaticPage::query()->where('type', StaticPageType::story)->first() ?: null;
})());
View::share('recruitmentStaticPageModel', (function () {
return StaticPage::query()->where('type', StaticPageType::recruitment)->first() ?: null;
})());
View::share('sagaStaticPageModel', (function () {
return StaticPage::query()->where('type', StaticPageType::saga)->first() ?: null;
})());
} //end if
}
}