ldc-agis/app/Providers/ViewServiceProvider.php

63 lines
2.2 KiB
PHP
Raw Normal View History

2023-12-19 15:09:12 +00:00
<?php
namespace App\Providers;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
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.
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)
2024-01-08 10:54:20 +01:00
// For the moment, we exclude the seo page because the new pagination system doesn't work in the package.
if (
collect(explode('/', \request()->getPathInfo()))->get(1) === 'bo' &&
collect(explode('/', \request()->getPathInfo()))->last() !== 'seo'
) {
2023-12-19 15:09:12 +00:00
Paginator::defaultView('bo.modules.pagination');
} else {
Paginator::useBootstrapFive();
}
// Get all the stamps that are pubished.
$stamps = Cache::remember('randomStampsFooter', 360, function () {
return DB::table('stamps')
2024-01-08 10:54:20 +01:00
->select('picture', 'picture_alt', 'picture_title')
->where('published', true)
->get();
2023-12-19 15:09:12 +00:00
});
/**
* Share a collection of 1 or 2 random stamps for footer.
* First, check if $stamps is not empty, if not,
* we count if the number of stamp in $stamps is greater than 2.
* Then, pick 1 or 2 stamp(s) depending on the number of stamps.
*/
View::share(
'randomStampsFooter',
$stamps->isNotEmpty() ? $stamps->random($stamps->count() >= 2 ? 2 : 1) : []
);
2024-01-08 10:54:20 +01:00
} //end if
2023-12-19 15:09:12 +00:00
}
}