97 lines
3.0 KiB
PHP
97 lines
3.0 KiB
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Enums\Morph;
|
|
use App\Services\Search\CreateSearchInstanceCommand;
|
|
use App\Services\Search\Search;
|
|
use App\Services\Storage\Image\ResizeCommandHandler;
|
|
use App\Services\Storage\ImageService;
|
|
use App\Services\Storage\StorageCommandHandler;
|
|
use Illuminate\Cache\RateLimiting\Limit;
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
|
use Illuminate\Contracts\Foundation\Application;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Illuminate\Support\Facades\URL;
|
|
use Illuminate\Validation\Rules\Password;
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Register any application services.
|
|
*/
|
|
public function register(): void
|
|
{
|
|
$this->app->bind(CreateSearchInstanceCommand::class, function () {
|
|
return new CreateSearchInstanceCommand(Search::class);
|
|
});
|
|
|
|
$this->app->bind(StorageCommandHandler::class, function () {
|
|
return new StorageCommandHandler(disc: (string) config('storage.disk'));
|
|
});
|
|
|
|
$this->app->bind(ImageService::class, function (Application $app) {
|
|
return new ImageService(
|
|
storageCommandHandler: $app->make(StorageCommandHandler::class),
|
|
resizeCommandHandler: $app->make(ResizeCommandHandler::class),
|
|
maxImageWidth: (int) config('storage.max_image_width', 4000),
|
|
maxImageHeight: (int) config('storage.max_image_height', 4000),
|
|
);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Bootstrap any application services.
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
if (config('app.force_https') === true) {
|
|
URL::forceScheme('https');
|
|
}
|
|
|
|
$this->passwordDefaults();
|
|
|
|
Relation::enforceMorphMap(Morph::map());
|
|
|
|
Route::pattern('language', '[a-z_]+');
|
|
Route::pattern('project', '[a-z0-9_-]+');
|
|
|
|
$this->configureRateLimiting();
|
|
Gate::define('AdminPanel', [\App\Policies\AdminPanel::class, 'view']);
|
|
}
|
|
|
|
/**
|
|
* Configure the rate limiters for the application.
|
|
*/
|
|
private function configureRateLimiting(): void
|
|
{
|
|
RateLimiter::for('login', function (Request $request) {
|
|
return [
|
|
Limit::perHour(config('rate_limiting.login_max_request', 50))->by($request->getClientIp()),
|
|
Limit::perHour(config('rate_limiting.login_max_email_request', 10))->by($request->getClientIp() . '-' . $request->input('email')),
|
|
];
|
|
});
|
|
}
|
|
|
|
private function passwordDefaults(): void
|
|
{
|
|
Password::defaults(function () {
|
|
$rule = Password::min(8);
|
|
|
|
if ($this->app->isProduction()) {
|
|
$rule->letters()
|
|
->mixedCase()
|
|
->numbers()
|
|
->symbols()
|
|
->uncompromised();
|
|
}
|
|
|
|
return $rule;
|
|
});
|
|
}
|
|
}
|