145 lines
4.7 KiB
PHP
145 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Pagination\UrlWindow;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Inertia\Inertia;
|
|
use League\Glide\Server;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
$this->registerInertia();
|
|
$this->registerGlide();
|
|
$this->registerLengthAwarePaginator();
|
|
}
|
|
|
|
public function registerInertia()
|
|
{
|
|
Inertia::version(function () {
|
|
return md5_file(public_path('mix-manifest.json'));
|
|
});
|
|
|
|
Inertia::share([
|
|
'auth' => function () {
|
|
return [
|
|
'user' => Auth::user() ? [
|
|
'id' => Auth::user()->id,
|
|
'first_name' => Auth::user()->first_name,
|
|
'last_name' => Auth::user()->last_name,
|
|
'email' => Auth::user()->email,
|
|
'role' => Auth::user()->role,
|
|
'account' => [
|
|
'id' => Auth::user()->account->id,
|
|
'name' => Auth::user()->account->name,
|
|
],
|
|
] : null,
|
|
];
|
|
},
|
|
'flash' => function () {
|
|
return [
|
|
'success' => Session::get('success'),
|
|
'error' => Session::get('error'),
|
|
];
|
|
},
|
|
]);
|
|
}
|
|
|
|
protected function registerGlide()
|
|
{
|
|
$this->app->bind(Server::class, function ($app) {
|
|
return Server::create([
|
|
'source' => Storage::getDriver(),
|
|
'cache' => Storage::getDriver(),
|
|
'cache_folder' => '.glide-cache',
|
|
'base_url' => 'img',
|
|
]);
|
|
});
|
|
}
|
|
|
|
protected function registerLengthAwarePaginator()
|
|
{
|
|
$this->app->bind(LengthAwarePaginator::class, function ($app, $values) {
|
|
return new class(...array_values($values)) extends LengthAwarePaginator {
|
|
public function only(...$attributes)
|
|
{
|
|
return $this->transform(function ($item) use ($attributes) {
|
|
return $item->only($attributes);
|
|
});
|
|
}
|
|
|
|
public function transform($callback)
|
|
{
|
|
$this->items->transform($callback);
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function toArray()
|
|
{
|
|
return [
|
|
'data' => $this->items->toArray(),
|
|
'links' => $this->links(),
|
|
];
|
|
}
|
|
|
|
public function links($view = null, $data = [])
|
|
{
|
|
$this->appends(Request::all());
|
|
|
|
$window = UrlWindow::make($this);
|
|
|
|
$elements = array_filter([
|
|
$window['first'],
|
|
is_array($window['slider']) ? '...' : null,
|
|
$window['slider'],
|
|
is_array($window['last']) ? '...' : null,
|
|
$window['last'],
|
|
]);
|
|
|
|
return Collection::make($elements)->flatMap(function ($item) {
|
|
if (is_array($item)) {
|
|
return Collection::make($item)->map(function ($url, $page) {
|
|
return [
|
|
'url' => $url,
|
|
'label' => $page,
|
|
'active' => $this->currentPage() === $page,
|
|
];
|
|
});
|
|
} else {
|
|
return [
|
|
[
|
|
'url' => null,
|
|
'label' => '...',
|
|
'active' => false,
|
|
],
|
|
];
|
|
}
|
|
})->prepend([
|
|
'url' => $this->previousPageUrl(),
|
|
'label' => 'Previous',
|
|
'active' => false,
|
|
])->push([
|
|
'url' => $this->nextPageUrl(),
|
|
'label' => 'Next',
|
|
'active' => false,
|
|
]);
|
|
}
|
|
};
|
|
});
|
|
}
|
|
}
|