ldc-legaulois/app/Providers/ViewServiceProvider.php

117 lines
5.5 KiB
PHP
Raw 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\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
* phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
2024-01-10 14:49:46 +01:00
*/
public function boot(): void
{
// phpcs:enable
2024-01-10 14:49:46 +01:00
$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();
2024-04-09 12:31:45 +02:00
// * Inject actual view name
View::composer('*', function ($view) {
$view->with('viewName', $view->getName());
});
2024-07-01 16:02:15 +02:00
Blade::directive('money', function (string $amountVariable) {
// phpcs:ignore Generic.Files.LineLength.TooLong
return "<?php echo e(app('phpMoneyFormatter')->format(new Money({$amountVariable}, app('phpMoneyCurrency')))); ?>";
2024-06-28 10:06:04 +02:00
});
// * Inject menuNodeList
View::share('menuNodeList', (function () {
2024-06-24 14:20:09 +02:00
/** @var \Illuminate\Database\Eloquent\Collection<int|string, \App\Models\GeneralSetting> */
$settings = app('settings');
$mainMenu = $settings->where('type', SettingsType::mainMenu)->first();
if (!$mainMenu) {
2024-06-24 14:20:09 +02:00
throw new \RuntimeException('Missing setting ' . SettingsType::mainMenu->label());
}
return $mainMenu->value;
})());
2024-04-18 12:05:10 +02:00
View::share('menuNodeDataList', app('menuNodeDataList'));
// * Inject configuration model
2024-06-10 17:13:34 +02:00
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.
2024-06-10 17:13:34 +02:00
collect([
['name' => 'firstMeat', 'value' => Meat::query()->first()],
['name' => 'firstRange', 'value' => Range::query()->first()],
2024-06-20 13:09:41 +02:00
['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()],
2024-06-10 17:13:34 +02:00
['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()],
2024-06-10 17:13:34 +02:00
])->map(function ($array) {
View::share($array['name'], (function () use ($array) {
return $array['value'] ?: null;
})());
});
} //end if
2024-01-10 14:49:46 +01:00
}
}