ldc-legaulois/app/Providers/ViewServiceProvider.php

165 lines
7.4 KiB
PHP

<?php
namespace App\Providers;
use App\Enums\Settings\SettingsType;
use App\Enums\StaticPageType;
use App\Models\Customer;
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.
switch (collect(explode('/', \request()->getPathInfo()))->get(1)) {
case 'bo':
Paginator::defaultView('bo.modules.pagination');
break;
case 'club':
Paginator::defaultView('club.modules.pagination');
break;
default:
Paginator::useBootstrapFive();
break;
}
// * 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} * 100, 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 breeding pages for the fo menu.
View::share('breedingPages', (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 StaticPage::getCachedBreedingStaticPages();
})());
// * Inject configuration model
collect([
['name' => 'mainMenuSettingModel', 'enum' => SettingsType::mainMenu],
['name' => 'hotNewsSettingModel', 'enum' => SettingsType::hotNews],
['name' => 'breederMapComponentSettingModel', 'enum' => SettingsType::breederMapComponent],
])->map(function ($array) {
View::share($array['name'], (function () use ($array) {
return GeneralSetting::getCachedGeneralSetting($array['enum']) ?: null;
})());
});
// * Inject some data for the bo menu.
collect([
['name' => 'customerModelsCount', 'value' => Customer::query()->count()],
['name' => 'firstMeat', 'value' => Meat::query()->first()],
['name' => 'firstRange', 'value' => Range::query()->first()],
['name' => 'mainMenuRangeModels', 'value' => Range::getPublishedOrdered()],
['name' => 'mainMenuMeatModels', 'value' => Meat::getPublishedOrdered()],
['name' => 'homeStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::home)],
['name' => 'storyStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::story)],
[
'name' => 'recruitmentStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::recruitment)
],
['name' => 'sagaStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::saga)],
[
'name' => 'recipesStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::recipes)
],
[
'name' => 'recipeStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::recipe)
],
[
'name' => 'productsStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::products)
],
['name' => 'legalsStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::legals)],
[
'name' => 'confidentialityStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::confidentiality)
],
[
'name' => 'sellingStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::selling)
],
[
'name' => 'accessibilityStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::accessibility)
],
[
'name' => 'productStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::product)
],
[
'name' => 'breedingPoultryStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::breedingPoultry)
],
[
'name' => 'breedingJobStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::breedingJob)
],
[
'name' => 'breedingFoodStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::breedingFood)
],
[
'name' => 'breedingHealthStaticPageModel',
'value' => StaticPage::getCachedStaticPage(StaticPageType::breedingHealth)
],
['name' => 'newsStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::news)],
])->map(function ($array) {
View::share($array['name'], (function () use ($array) {
return $array['value'] ?: null;
})());
});
} //end if
}
}