koel/app/Providers/RouteServiceProvider.php

48 lines
1.4 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
2024-01-10 00:47:09 +00:00
use Illuminate\Support\Facades\File;
2016-09-26 06:30:00 +00:00
use Illuminate\Support\Facades\Route;
2022-06-10 10:47:46 +00:00
use Webmozart\Assert\Assert;
2015-12-13 04:42:28 +00:00
class RouteServiceProvider extends ServiceProvider
{
protected $namespace = 'App\Http\Controllers';
2020-12-22 20:11:22 +00:00
public function map(): void
2016-09-26 06:30:00 +00:00
{
2022-06-10 10:47:46 +00:00
self::loadVersionAwareRoutes('web');
self::loadVersionAwareRoutes('api');
2016-09-26 06:30:00 +00:00
}
2016-09-26 07:33:53 +00:00
2022-06-10 10:47:46 +00:00
private static function loadVersionAwareRoutes(string $type): void
2016-09-26 06:30:00 +00:00
{
2022-06-10 10:47:46 +00:00
Assert::oneOf($type, ['web', 'api']);
2016-09-26 07:33:53 +00:00
2022-06-10 10:47:46 +00:00
Route::group([], base_path(sprintf('routes/%s.base.php', $type)));
2022-07-26 20:08:31 +00:00
$apiVersion = self::getApiVersion();
2022-06-10 10:47:46 +00:00
$routeFile = $apiVersion ? base_path(sprintf('routes/%s.%s.php', $type, $apiVersion)) : null;
2024-01-10 00:47:09 +00:00
if ($routeFile && File::exists($routeFile)) {
2022-06-10 10:47:46 +00:00
Route::group([], $routeFile);
}
2015-12-13 04:42:28 +00:00
}
2022-07-26 20:08:31 +00:00
private static function getApiVersion(): ?string
{
// In the test environment, the route service provider is loaded _before_ the request is made,
// so we can't rely on the header.
// Instead, we manually set the API version as an env variable in applicable test cases.
$version = app()->runningUnitTests() ? env('X_API_VERSION') : request()->header('X-Api-Version');
if ($version) {
Assert::oneOf($version, ['v6']);
2022-07-26 20:08:31 +00:00
}
return $version;
2022-07-26 20:08:31 +00:00
}
2015-12-13 04:42:28 +00:00
}