ldc-tregalette/app/Providers/ViewServiceProvider.php
2023-12-15 17:49:43 +01:00

60 lines
1.8 KiB
PHP

<?php
namespace App\Providers;
use App\Models\Range;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ViewServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
// * Mobile detection
Blade::if('isMobile', function () {
return app('agent')->isMobile();
});
// * Tablet detection
Blade::if('isTablet', function () {
return app('agent')->isTablet();
});
// * Desktop detection
Blade::if('isDesktop', function () {
return app('agent')->isDesktop();
});
$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()) {
// Shares this data with all views.
if (Schema::hasTable('ranges')) {
View::share('allRanges', Range::getCached());
}
// * FORCE BOOTSTRAP PAGINATOR (or custom if in front)
// Early boot, request wont be filled as expected.
if (collect(explode('/', \request()->getPathInfo()))->get(1) === 'bo') {
Paginator::defaultView('bo.modules.pagination');
} else {
Paginator::useBootstrapFive();
}
}
}
}