149 lines
8.3 KiB
PHP
149 lines
8.3 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' => 'breederMapSettingModel', 'enum' => SettingsType::breederMap],
|
|
])->map(function ($array) {
|
|
View::share($array['name'], (function () use ($array) {
|
|
return 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();
|
|
});
|
|
// @phpcs:disable Generic.Files.LineLength.TooLong
|
|
collect([
|
|
['name' => 'customerModelsCount', 'value' => $customerModelsCount],
|
|
['name' => 'firstMeat', 'value' => $firstMeat],
|
|
['name' => 'firstRange', 'value' => $firstRange],
|
|
['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' => 'sitemapStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::sitemap)],
|
|
['name' => 'rulesStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::rules)],
|
|
['name' => 'contactStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::contact)],
|
|
['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)],
|
|
['name' => 'searchStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::search)],
|
|
['name' => 'exitOutsideStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::exitOutside)],
|
|
['name' => 'ouiCestBonStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::ouiCBon)],
|
|
['name' => 'ourFarmsStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::ourFarms)],
|
|
['name' => 'tdfStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::tdf)],
|
|
['name' => 'respecttheEarthStaticPageModel', 'value' => StaticPage::getCachedStaticPage(StaticPageType::respectTheEarth)],
|
|
])->map(function ($array) {
|
|
View::share($array['name'], (function () use ($array) {
|
|
return $array['value'] ?: null;
|
|
})());
|
|
});
|
|
// phpcs:enable
|
|
} //end if
|
|
}
|
|
}
|