From a6467775d50141c31c1a65171242ef4ce8f4a356 Mon Sep 17 00:00:00 2001 From: Martin Kluska Date: Fri, 22 Nov 2024 12:31:16 +0100 Subject: [PATCH] feat(Http): support API versions with float + load also non-versioned routes file --- src/Contracts/HasVersionedApiRoutes.php | 2 +- .../Pipes/BootProviderRoutesPipe.php | 23 ++++++++++--------- .../AssertProviderRegistersRoutes.php | 6 ++--- .../LoadProviderRoutesPipeTest.php | 4 ++-- .../Http/routes/with_versioned_api_api.php | 8 +++++++ .../routes/with_versioned_api_api_v1.1.php | 8 +++++++ .../WithVersionedApiServiceProvider.php | 2 +- .../Pipes/LoadProviderRoutesPipeTest.php | 2 +- 8 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 tests/Feature/Providers/Pipes/LoadProviderRoutesPipe/WithVersionedApi/Http/routes/with_versioned_api_api.php create mode 100644 tests/Feature/Providers/Pipes/LoadProviderRoutesPipe/WithVersionedApi/Http/routes/with_versioned_api_api_v1.1.php diff --git a/src/Contracts/HasVersionedApiRoutes.php b/src/Contracts/HasVersionedApiRoutes.php index fce40461..62be577c 100644 --- a/src/Contracts/HasVersionedApiRoutes.php +++ b/src/Contracts/HasVersionedApiRoutes.php @@ -10,7 +10,7 @@ interface HasVersionedApiRoutes * Return api version you want to support. Automatically looks for * {Domain}/Http/routes/{domainPrefix}_api_v{version}.php file and uses a prefix api/v{version}. * - * @return array + * @return array */ public function apiVersions(): array; } diff --git a/src/Providers/Pipes/BootProviderRoutesPipe.php b/src/Providers/Pipes/BootProviderRoutesPipe.php index 2d9d3172..0b1b1f0b 100644 --- a/src/Providers/Pipes/BootProviderRoutesPipe.php +++ b/src/Providers/Pipes/BootProviderRoutesPipe.php @@ -21,7 +21,7 @@ use LogicException; use Psr\Log\LoggerInterface; -class BootProviderRoutesPipe implements AppServiceProviderPipeContract +final class BootProviderRoutesPipe implements AppServiceProviderPipeContract { public function __construct( private readonly Container $container, @@ -44,7 +44,7 @@ public function handle(AppServiceProviderEntity $appServiceProvider, Closure $ne $next($appServiceProvider); } - protected function loadRoutes(AppServiceProviderEntity $appServiceProvider): void + private function loadRoutes(AppServiceProviderEntity $appServiceProvider): void { $dir = $appServiceProvider->serviceRootDir; $serviceFileName = $appServiceProvider->serviceFileName; @@ -65,7 +65,7 @@ protected function loadRoutes(AppServiceProviderEntity $appServiceProvider): voi } } - protected function loadApiRoute(string $dir, string $serviceFileName, string $urlPrefix, ?int $version = null): bool + private function loadApiRoute(string $dir, string $serviceFileName, string $urlPrefix, ?float $version = null): bool { $versionFileSuffix = $version === null ? '' : '_v' . $version; $versionRoutePrefix = $version === null ? '' : 'v' . $version . '/'; @@ -79,7 +79,7 @@ protected function loadApiRoute(string $dir, string $serviceFileName, string $ur ); } - protected function registerRoute( + private function registerRoute( string $dir, string $fileSuffix, string $serviceFileName, @@ -100,12 +100,12 @@ protected function registerRoute( return true; } - protected function getRouteFilePath(string $dir, string $serviceFileName, string $fileSuffix): string + private function getRouteFilePath(string $dir, string $serviceFileName, string $fileSuffix): string { return $dir . sprintf('/Http/routes/%s_%s.php', $serviceFileName, $fileSuffix); } - protected function tryToLoadCustomRoutes( + private function tryToLoadCustomRoutes( AppServiceProviderEntity $appServiceProvider, string $dir, string $serviceFileName, @@ -180,18 +180,19 @@ protected function tryToLoadCustomRoutes( return $didLoadRoutes; } - protected function tryToLoadApiRoutes( + private function tryToLoadApiRoutes( AppServiceProviderEntity $appServiceProvider, string $dir, string $serviceFileName, string $urlPrefix ): bool { - // Force the user to use versioned api or un-versioned api routes + // Support using old non-versioned apis with versioned apis + $didLoadRoutes = $this->loadApiRoute($dir, $serviceFileName, $urlPrefix); + if ($appServiceProvider->serviceProvider instanceof HasVersionedApiRoutes === false) { - return $this->loadApiRoute($dir, $serviceFileName, $urlPrefix); + return $didLoadRoutes; } - $didLoadRoutes = false; foreach ($appServiceProvider->serviceProvider->apiVersions() as $version) { if ($this->loadApiRoute($dir, $serviceFileName, $urlPrefix, $version)) { $didLoadRoutes = true; @@ -201,7 +202,7 @@ protected function tryToLoadApiRoutes( return $didLoadRoutes; } - protected function makeRoute(): RouteRegistrar + private function makeRoute(): RouteRegistrar { return $this->container->make(RouteRegistrar::class); } diff --git a/src/Testing/Providers/Concerns/AssertProviderRegistersRoutes.php b/src/Testing/Providers/Concerns/AssertProviderRegistersRoutes.php index 47794aba..7e8fe770 100644 --- a/src/Testing/Providers/Concerns/AssertProviderRegistersRoutes.php +++ b/src/Testing/Providers/Concerns/AssertProviderRegistersRoutes.php @@ -44,14 +44,14 @@ public function assertRoutes( foreach ($expectUrlsByMethod as $method => $urls) { $registeredUrls = $routes->get($method); - $currentUrls = []; + $expectedUrls = []; foreach ($urls as $index => $value) { $url = is_callable($value) ? $index : $value; - $currentUrls[] = $url; + $expectedUrls[] = $url; } if ($onlyGiven) { - Assert::assertEquals(array_keys($registeredUrls), $currentUrls); + Assert::assertEquals($expectedUrls, array_keys($registeredUrls)); } $errorMessage = 'Not found in: ' . implode(', ', array_keys($registeredUrls)); diff --git a/tests/Feature/Providers/Pipes/LoadProviderRoutesPipe/LoadProviderRoutesPipeTest.php b/tests/Feature/Providers/Pipes/LoadProviderRoutesPipe/LoadProviderRoutesPipeTest.php index 248ce60f..21ef1bcf 100644 --- a/tests/Feature/Providers/Pipes/LoadProviderRoutesPipe/LoadProviderRoutesPipeTest.php +++ b/tests/Feature/Providers/Pipes/LoadProviderRoutesPipe/LoadProviderRoutesPipeTest.php @@ -78,10 +78,10 @@ public function testWithApiOnly(): void ], WithApiServiceProvider::class, true); } - public function testWithVersionedApiOnly(): void + public function testWithVersionedApi(): void { $this->assertRoutes($this->app(), [ - 'GET' => ['api/v1/test/1-api', 'api/v2/test/2-api'], + 'GET' => ['api/test/api', 'api/v1/test/1-api', 'api/v1.1/test/1.1-api', 'api/v2/test/2-api'], ], WithVersionedApiServiceProvider::class, true); } diff --git a/tests/Feature/Providers/Pipes/LoadProviderRoutesPipe/WithVersionedApi/Http/routes/with_versioned_api_api.php b/tests/Feature/Providers/Pipes/LoadProviderRoutesPipe/WithVersionedApi/Http/routes/with_versioned_api_api.php new file mode 100644 index 00000000..de6bf819 --- /dev/null +++ b/tests/Feature/Providers/Pipes/LoadProviderRoutesPipe/WithVersionedApi/Http/routes/with_versioned_api_api.php @@ -0,0 +1,8 @@ +