generated from Flycro/laravel-nuxt
Initial commit
This commit is contained in:
83
app/Providers/AppServiceProvider.php
Normal file
83
app/Providers/AppServiceProvider.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Helpers\Image;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Auth\Notifications\ResetPassword;
|
||||
use Illuminate\Auth\Notifications\VerifyEmail;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
// Register Telescope only in local environment
|
||||
if ($this->app->environment('local') && class_exists(\Laravel\Telescope\TelescopeServiceProvider::class)) {
|
||||
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
|
||||
$this->app->register(TelescopeServiceProvider::class);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
RateLimiter::for('api', function (Request $request) {
|
||||
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
|
||||
});
|
||||
|
||||
RateLimiter::for('verification-notification', function (Request $request) {
|
||||
return Limit::perMinute(1)->by($request->user()?->email ?: $request->ip());
|
||||
});
|
||||
|
||||
RateLimiter::for('uploads', function (Request $request) {
|
||||
return $request->user()?->hasRole('admin')
|
||||
? Limit::none()
|
||||
: Limit::perMinute(10)->by($request->ip());
|
||||
});
|
||||
|
||||
ResetPassword::createUrlUsing(function (object $notifiable, string $token) {
|
||||
return config('app.frontend_url') . "/password-reset/{$token}?email={$notifiable->getEmailForPasswordReset()}";
|
||||
});
|
||||
|
||||
VerifyEmail::createUrlUsing(function (object $notifiable) {
|
||||
$url = url()->temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(config('auth.verification.expire', 60)),
|
||||
[
|
||||
'ulid' => $notifiable->ulid,
|
||||
'hash' => sha1($notifiable->getEmailForVerification()),
|
||||
]
|
||||
);
|
||||
|
||||
return config('app.frontend_url') . '/auth/verify?verify_url=' . urlencode($url);
|
||||
});
|
||||
|
||||
/**
|
||||
* Convert uploaded image to webp, jpeg or png format and resize it
|
||||
*/
|
||||
UploadedFile::macro('convert', function (int $width = null, int $height = null, string $extension = 'webp', int $quality = 90): UploadedFile {
|
||||
return tap($this, function (UploadedFile $file) use ($width, $height, $extension, $quality) {
|
||||
Image::convert($file->path(), $file->path(), $width, $height, $extension, $quality);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Remove all special characters from a string
|
||||
*/
|
||||
Str::macro('onlyWords', function (string $text): string {
|
||||
// \p{L} matches any kind of letter from any language
|
||||
// \d matches a digit in any script
|
||||
return Str::replaceMatches('/[^\p{L}\d ]/u', '', $text);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user