Skip to content

Commit

Permalink
feat(Http): support API versions with float + load also non-versioned…
Browse files Browse the repository at this point in the history
… routes file
  • Loading branch information
pionl committed Nov 22, 2024
1 parent 9039c7b commit a646777
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/Contracts/HasVersionedApiRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>
* @return array<float>
*/
public function apiVersions(): array;
}
23 changes: 12 additions & 11 deletions src/Providers/Pipes/BootProviderRoutesPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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 . '/';
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -201,7 +202,7 @@ protected function tryToLoadApiRoutes(
return $didLoadRoutes;
}

protected function makeRoute(): RouteRegistrar
private function makeRoute(): RouteRegistrar
{
return $this->container->make(RouteRegistrar::class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;

Route::get('api', static function () {
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;

Route::get('1.1-api', static function () {
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public function getRoutePrefix(string $generatedPrefix): string

public function apiVersions(): array
{
return [1, 2];
return [1, 1.1, 2];
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Providers/Pipes/LoadProviderRoutesPipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Psr\Log\NullLogger;
use stdClass;

class LoadProviderRoutesPipeTest extends TestCase
final class LoadProviderRoutesPipeTest extends TestCase
{
public function invalidNumericRoutes(): array
{
Expand Down

0 comments on commit a646777

Please sign in to comment.