97 lines
2.7 KiB
PHP
97 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Contracts\Directories\ProductDirectory;
|
|
use App\Contracts\Directories\RecipeDirectory;
|
|
use App\Directories\EloquentProductDirectory;
|
|
use App\Directories\EloquentRecipeDirectory;
|
|
use App\Services\AdminAuthService;
|
|
use Domain\Models\ValueObjects\Pagination;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Illuminate\Support\Facades\Request;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Illuminate\Support\Facades\Vite;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
public function register(): void
|
|
{
|
|
$this->app->singleton(
|
|
AdminAuthService::class,
|
|
function () {
|
|
$guard = Auth::guard('backend');
|
|
|
|
return new AdminAuthService($guard);
|
|
}
|
|
);
|
|
$this->app->singleton(
|
|
ProductDirectory::class,
|
|
fn () => new EloquentProductDirectory()
|
|
);
|
|
$this->app->singleton(
|
|
RecipeDirectory::class,
|
|
fn () => new EloquentRecipeDirectory()
|
|
);
|
|
|
|
$this->requestMacro();
|
|
}
|
|
|
|
public function boot(): void
|
|
{
|
|
$this->dateSetup();
|
|
$this->laravelGlobalSetup();
|
|
}
|
|
|
|
private function laravelGlobalSetup(): void
|
|
{
|
|
Model::shouldBeStrict(app()->environment('local'));
|
|
|
|
Vite::useCspNonce();
|
|
|
|
URL::forceScheme('https');
|
|
}
|
|
|
|
private function dateSetup(): void
|
|
{
|
|
foreach ([\LC_TIME, \LC_MESSAGES, \LC_COLLATE, \LC_CTYPE, \LC_MONETARY] as $constant) {
|
|
setlocale(
|
|
$constant,
|
|
sprintf(
|
|
'%s_%s.UTF-8',
|
|
Config::string('app.locale'),
|
|
strtoupper(Config::string('app.locale'))
|
|
)
|
|
);
|
|
}
|
|
Date::setLocale(Config::string('app.locale'));
|
|
Date::setFallbackLocale(Config::string('app.fallback_locale'));
|
|
}
|
|
|
|
private function requestMacro(): void
|
|
{
|
|
Request::macro('pagination', function (int $defaultPageSize = 25): Pagination {
|
|
if ($this->has('offset') || $this->has('limit')) {
|
|
return Pagination::ofOffsetAndLimit(
|
|
$this->integer('offset', 0),
|
|
$this->integer('limit', $defaultPageSize)
|
|
);
|
|
}
|
|
|
|
return Pagination::ofPage(
|
|
$this->integer('page', 1),
|
|
$this->integer(
|
|
'per_page',
|
|
// * Default laravel paginator name
|
|
$this->integer('pagination', $defaultPageSize)
|
|
)
|
|
);
|
|
});
|
|
}
|
|
}
|