ldc-legaulois/app/Providers/ViewServiceProvider.php

124 lines
6.2 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\Blade;
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());
});
Blade::directive('money', function (string $amountVariable) {
// phpcs:ignore Generic.Files.LineLength.TooLong
return "<?php echo e(app('phpMoneyFormatter')->format(new Money({$amountVariable}, app('phpMoneyCurrency')))); ?>";
});
// * Inject menuNodeList
View::share('menuNodeList', (function () {
/** @var \Illuminate\Database\Eloquent\Collection<int|string, \App\Models\GeneralSetting> */
$settings = app('settings');
$mainMenu = $settings->where('type', SettingsType::mainMenu)->first();
if (!$mainMenu) {
throw new \RuntimeException('Missing setting ' . SettingsType::mainMenu->label());
}
return $mainMenu->value;
})());
View::share('menuNodeDataList', app('menuNodeDataList'));
// * Inject configuration model
collect([
['name' => 'mainMenuSettingModel', 'enum' => SettingsType::mainMenu],
['name' => 'hotNewsSettingModel', 'enum' => SettingsType::hotNews],
])->map(function ($array) {
View::share($array['name'], (function () use ($array) {
return GeneralSetting::query()->where('type', $array['enum'])->first() ?: null;
})());
});
// * Inject some data for the bo menu.
collect([
['name' => 'firstMeat', 'value' => Meat::query()->first()],
['name' => 'firstRange', 'value' => Range::query()->first()],
['name' => 'mainMenuRangeModels', 'value' => Range::query()->orderBy('slug', 'ASC')
->where('published', true)->get()],
['name' => 'mainMenuMeatModels', 'value' => Meat::query()->orderBy('slug', 'ASC')
->where('published', true)->get()],
['name' => 'homeStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::home)->first()],
['name' => 'storyStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::story)->first()],
['name' => 'recruitmentStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::recruitment)->first()],
['name' => 'sagaStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::saga)->first()],
['name' => 'recipesStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::recipes)->first()],
['name' => 'recipeStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::recipe)->first()],
['name' => 'productsStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::products)->first()],
['name' => 'legalsStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::legals)->first()],
['name' => 'confidentialityStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::confidentiality)->first()],
['name' => 'sellingStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::selling)->first()],
['name' => 'accessibilityStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::accessibility)->first()],
['name' => 'productStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::product)->first()],
['name' => 'breedingPoultryStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::breedingPoultry)->first()],
['name' => 'breedingJobStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::breedingJob)->first()],
['name' => 'breedingFoodStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::breedingFood)->first()],
['name' => 'breedingHealthStaticPageModel', 'value' => StaticPage::query()
->where('type', StaticPageType::breedingHealth)->first()],
])->map(function ($array) {
View::share($array['name'], (function () use ($array) {
return $array['value'] ?: null;
})());
});
} //end if
}
}