pingcrm/app/Providers/AppServiceProvider.php

147 lines
4.9 KiB
PHP
Raw Normal View History

2019-03-05 18:10:11 -03:00
<?php
namespace App\Providers;
2019-03-18 08:53:00 -03:00
use Inertia\Inertia;
2019-08-09 12:33:47 -03:00
use League\Glide\Server;
2019-03-18 08:53:00 -03:00
use Carbon\CarbonImmutable;
use Illuminate\Support\Collection;
use Illuminate\Pagination\UrlWindow;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
2019-08-09 12:33:47 -03:00
use Illuminate\Support\Facades\Storage;
2019-03-05 18:10:11 -03:00
use Illuminate\Support\ServiceProvider;
2019-03-18 08:53:00 -03:00
use Illuminate\Pagination\LengthAwarePaginator;
2019-03-05 18:10:11 -03:00
class AppServiceProvider extends ServiceProvider
{
2019-03-18 08:53:00 -03:00
public function boot()
{
Date::use(CarbonImmutable::class);
}
2019-03-05 18:10:11 -03:00
public function register()
2019-08-09 12:33:47 -03:00
{
$this->registerInertia();
$this->registerGlide();
$this->registerLengthAwarePaginator();
}
public function registerInertia()
2019-03-05 18:10:11 -03:00
{
2019-04-18 06:51:28 -03:00
Inertia::version(function () {
return md5_file(public_path('mix-manifest.json'));
});
Inertia::share(function () {
2019-05-21 20:12:49 -03:00
return [
'auth' => [
'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' => [
'success' => Session::get('success'),
],
2019-08-08 09:50:52 -03:00
'errors' => Session::get('errors')
? Session::get('errors')->getBag('default')->getMessages()
: (object) [],
2019-05-21 20:12:49 -03:00
];
});
2019-08-09 12:33:47 -03:00
}
2019-03-18 08:53:00 -03:00
2019-08-09 12:33:47 -03:00
protected function registerGlide()
{
$this->app->bind(Server::class, function ($app) {
return Server::create([
'source' => Storage::getDriver(),
'cache' => Storage::getDriver(),
'cache_folder' => '.glide-cache',
2019-08-09 13:50:45 -03:00
'base_url' => 'img',
2019-08-09 12:33:47 -03:00
]);
});
2019-03-05 18:10:11 -03:00
}
2019-03-18 08:53:00 -03:00
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,
]);
}
};
});
}
2019-03-05 18:10:11 -03:00
}