forge/app/Providers/AppServiceProvider.php
Refringe 7e1c66f250
Download Count Review
Reviewed the download count PR work and made some changes:
- Updated the download link route to include the mod's slug for easier identification.
- Moved rate limiter from the route middleware (the entire controller) to just the show method in the controller.
- Created a ModVersionPolicy that the controller can check against.
- Moves download increment logic into the model.
- Defers the call to the download increment logic (now run in the background)
- Updated the route to have a name, and the downloadUrl methods to build the URL dynamically using the route name.
- Wrote some tests to check URL building, download counting, and rate limiting.

# Conflicts:
#	app/Http/Controllers/ModVersionController.php
#	app/Providers/AppServiceProvider.php
2024-09-25 17:04:46 -04:00

94 lines
2.4 KiB
PHP

<?php
namespace App\Providers;
use App\Models\Mod;
use App\Models\ModDependency;
use App\Models\ModVersion;
use App\Models\SptVersion;
use App\Models\User;
use App\Observers\ModDependencyObserver;
use App\Observers\ModObserver;
use App\Observers\ModVersionObserver;
use App\Observers\SptVersionObserver;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Number;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
// Allow mass assignment for all models. Be careful!
Model::unguard();
$this->registerObservers();
$this->registerNumberMacros();
$this->registerCarbonMacros();
// This gate determines who can access the Pulse dashboard.
Gate::define('viewPulse', function (User $user) {
return $user->isAdmin();
});
}
/**
* Register model observers.
*/
private function registerObservers(): void
{
Mod::observe(ModObserver::class);
ModVersion::observe(ModVersionObserver::class);
ModDependency::observe(ModDependencyObserver::class);
SptVersion::observe(SptVersionObserver::class);
}
/**
* Register custom number macros.
*/
private function registerNumberMacros(): void
{
// Format download numbers.
Number::macro('downloads', function (int|float $number) {
return Number::forHumans(
$number,
$number > 1000000 ? 2 : ($number > 1000 ? 1 : 0),
maxPrecision: null,
abbreviate: true
);
});
}
/**
* Register custom Carbon macros.
*/
private function registerCarbonMacros(): void
{
// Format dates dynamically based on the time passed.
Carbon::macro('dynamicFormat', function (Carbon $date) {
if ($date->diff(now())->m > 1) {
return $date->format('M jS, Y');
}
if ($date->diff(now())->d === 0) {
return $date->diffForHumans();
}
return $date->format('M jS, g:i A');
});
}
}