ldc-legaulois/app/Providers/ViewServiceProvider.php

161 lines
6.5 KiB
PHP
Raw Permalink Normal View History

2024-01-10 14:49:46 +01:00
<?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;
2024-01-10 14:49:46 +01:00
use Illuminate\Pagination\Paginator;
2024-06-28 10:06:04 +02:00
use Illuminate\Support\Facades\Blade;
2024-01-10 14:49:46 +01:00
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
*/
public function boot(): void
{
$appInfos = Cache::remember('composer', 360, function () {
return json_decode(File::get(\app_path('../composer.json')));
});
// Share composer.json infos.
2026-09-25 16:25:31 +02:00
if (isset($appInfos->name, $appInfos->version)) {
2024-01-10 14:49:46 +01:00
View::share('cpsr_name', $appInfos->name);
View::share('cpsr_version', $appInfos->version);
}
2026-09-23 11:46:20 +02:00
// * MJML mails
Blade::anonymousComponentPath(resource_path('views/components/mjml'), 'mjml');
if (app()->runningInConsole()) {
return;
}
// * 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')))); ?>";
});
$this->app->booted(function () {
$this->shareViewData();
});
}
/**
* Share data with all views, must run after the routes are registered.
*
* @return void
* @throws \RuntimeException Missing setting main menu label.
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
*/
private function shareViewData(): void
{
// phpcs:enable
// * Inject menuNodeList
/** @var \Illuminate\Database\Eloquent\Collection<int|string, \App\Models\GeneralSetting> $settings */
$settings = app('settings');
$mainMenu = $settings->where('type', SettingsType::mainMenu)->first();
if (!$mainMenu) {
throw new \RuntimeException('Missing setting ' . SettingsType::mainMenu->label());
}
View::share('menuNodeList', $mainMenu->value);
View::share('menuNodeDataList', app('menuNodeDataList'));
// * Inject breeding pages for the fo menu.
View::share('breedingPages', StaticPage::getCachedBreedingStaticPages());
// * Inject configuration model
collect([
['name' => 'mainMenuSettingModel', 'enum' => SettingsType::mainMenu],
['name' => 'hotNewsSettingModel', 'enum' => SettingsType::hotNews],
['name' => 'breederMapSettingModel', 'enum' => SettingsType::breederMap],
])->each(function ($array) {
View::share($array['name'], GeneralSetting::getCachedGeneralSetting($array['enum']) ?: null);
});
// * Inject some data for the bo menu.
$customerModelsCount = Cache::remember('customerModelsCount', 86400, function () {
return Customer::query()->count();
});
$firstMeat = Cache::remember('firstMeat', 86400, function () {
return Meat::query()->first();
});
$firstRange = Cache::remember('firstRange', 86400, function () {
return Range::query()->first();
});
$staticPages = [
'homeStaticPageModel' => StaticPageType::home,
'storyStaticPageModel' => StaticPageType::story,
'recruitmentStaticPageModel' => StaticPageType::recruitment,
'sagaStaticPageModel' => StaticPageType::saga,
'recipesStaticPageModel' => StaticPageType::recipes,
'recipeStaticPageModel' => StaticPageType::recipe,
'productsStaticPageModel' => StaticPageType::products,
'legalsStaticPageModel' => StaticPageType::legals,
'confidentialityStaticPageModel' => StaticPageType::confidentiality,
'sellingStaticPageModel' => StaticPageType::selling,
'accessibilityStaticPageModel' => StaticPageType::accessibility,
'sitemapStaticPageModel' => StaticPageType::sitemap,
'rulesStaticPageModel' => StaticPageType::rules,
'contactStaticPageModel' => StaticPageType::contact,
'productStaticPageModel' => StaticPageType::product,
'breedingPoultryStaticPageModel' => StaticPageType::breedingPoultry,
'breedingJobStaticPageModel' => StaticPageType::breedingJob,
'breedingFoodStaticPageModel' => StaticPageType::breedingFood,
'breedingHealthStaticPageModel' => StaticPageType::breedingHealth,
'newsStaticPageModel' => StaticPageType::news,
'searchStaticPageModel' => StaticPageType::search,
'exitOutsideStaticPageModel' => StaticPageType::exitOutside,
'ouiCestBonStaticPageModel' => StaticPageType::ouiCBon,
'ourFarmsStaticPageModel' => StaticPageType::ourFarms,
'tdfStaticPageModel' => StaticPageType::tdf,
];
$values = [
'customerModelsCount' => $customerModelsCount,
'firstMeat' => $firstMeat,
'firstRange' => $firstRange,
'mainMenuRangeModels' => Range::getPublishedOrdered(),
'mainMenuMeatModels' => Meat::getPublishedOrdered(),
];
foreach ($staticPages as $name => $type) {
$values[$name] = StaticPage::getCachedStaticPage($type);
}
$values['commitmmentsPages'] = StaticPage::getCachedCommitmentsStaticPages();
foreach ($values as $name => $value) {
View::share($name, $value ?: null);
}
2024-01-10 14:49:46 +01:00
}
}