-
routes/api.php
Open in GitHubuse App\Http\Controllers\Api\LaravelVersionsController; use Illuminate\Support\Facades\Route; Route::get('versions', [LaravelVersionsController::class, 'index'])->name('versions.index'); Route::get('versions/{version}', [LaravelVersionsController::class, 'show'])->name('versions.show');
-
app/Http/Controllers/Api/LaravelVersionsController.php
Open in GitHubuse App\Http\Controllers\Controller; use App\Http\Resources\LaravelVersionResource; use App\LaravelVersionFromPath; use App\Models\LaravelVersion; use Illuminate\Support\Facades\Cache; class LaravelVersionsController extends Controller { public function index() { $versions = Cache::remember('laravel-versions--released', 3600, function () { return LaravelVersion::released()->orderBy('major', 'desc')->orderBy('minor', 'desc')->get(); }); return LaravelVersionResource::collection($versions); } public function show($path) { [$version] = (new LaravelVersionFromPath())($path); return new LaravelVersionResource($version); } }
-
app/Http/Resources/LaravelVersionResource.php
Open in GitHubuse Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; class LaravelVersionResource extends JsonResource { public function toArray($request) { $minor_label = $this->major < 6 ? 'minor' : 'latest_minor'; $segments = $request->segments(); return [ 'major' => $this->major, $minor_label => $this->minor, 'latest_patch' => $this->patch, 'latest' => sprintf('%s.%s.%s', $this->major, $this->minor, $this->patch), 'is_lts' => $this->is_lts, 'released_at' => $this->released_at, 'ends_bugfixes_at' => $this->ends_bugfixes_at, 'ends_securityfixes_at' => $this->ends_securityfixes_at, 'status' => $this->status, $this->mergeWhen($this->specificVersionProvided($request), [ 'specific_version' => [ 'provided' => end($segments), 'needs_patch' => $request->url() !== $this->latest_patch_api_url, 'needs_major_upgrade' => $this->status === 'end-of-life', ], ]), 'links' => $this->links($request), ]; } public function specificVersionProvided(Request $request): bool { return ($this->api_url !== $request->url()) && ! $request->routeIs('api.versions.index'); } // }