Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

tighten/novapackages

336 stars
3 code files
View tighten/novapackages on GitHub

routes/api.php

Open in GitHub
use Illuminate\Support\Facades\Route;
 
Route::get('recent', 'Api\RecentController')->middleware('throttle:60');

app/Http/Controllers/Api/RecentController.php

Open in GitHub
use App\CacheKeys;
use App\Http\Controllers\Controller;
use App\Http\Resources\Package as PackageResource;
use App\Package;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
 
class RecentController extends Controller
{
const CACHE_LENGTH = 10;
 
public function __invoke(Request $request)
{
return PackageResource::collection($this->recent());
}
 
private function recent()
{
return Cache::remember(CacheKeys::recentPackages(), self::CACHE_LENGTH, function () {
return Package::latest()->with(['author', 'tags'])->take(10)->get();
});
}
}

app/Http/Resources/PackageResource.php

Open in GitHub
use App\Favorite;
use App\Package;
use Illuminate\Support\Str;
 
class PackageResource extends ModelResource
{
public $model = Package::class;
 
const CACHE_RATINGS_LENGTH = 5;
 
public function toArray($package)
{
return [
'id' => $package->id,
'name' => $package->name,
'composer_name' => $package->composer_name,
'packagist_namespace' => $package->composer_vendor,
'packagist_name' => $package->composer_package,
'abstract' => $package->abstract,
'is_disabled' => $package->is_disabled,
'icon_url' => $package->picture_url ?? 'https://api.adorable.io/avatars/285/'.Str::slug($package->name).'.png',
'url' => $package->url,
'average_rating' => $this->averageRating($package),
'rating_count' => $this->ratingCount($package),
'created_at' => $package->created_at->diffForHumans(),
'author' => [
'id' => $package->author_id,
'name' => $package->author->name,
'url' => $package->author->url,
'avatar_url' => $package->author->avatar ?: 'https://api.adorable.io/avatars/285/'.Str::slug($package->author->name).'.png',
'github_username' => $package->author->github_username,
],
];
}
//
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.