Skip to content

Commit

Permalink
Add ignoreServers option
Browse files Browse the repository at this point in the history
  • Loading branch information
charjr committed Nov 6, 2024
1 parent 40b6a9a commit 61cb1d0
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 30 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"require": {
"php": "^8.1.0",
"cebe/php-openapi": "^1.7",
"membrane/openapi-reader": "^2.0.0",
"membrane/openapi-reader": "2.1.0",
"psr/http-message": "^1.0 || ^2.0",
"psr/log": "^3.0",
"symfony/console": "^6.0 || ^7.0"
Expand Down
53 changes: 39 additions & 14 deletions src/Console/Command/CacheOpenAPIRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

namespace Membrane\OpenAPIRouter\Console\Command;

use Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes as CacheOpenAPIRoutesService;
use Membrane\OpenAPIRouter\Console\Service;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -20,29 +22,52 @@ class CacheOpenAPIRoutes extends Command
{
protected function configure(): void
{
self::addArgument(
'openAPI',
InputArgument::REQUIRED,
'The absolute filepath to your OpenAPI'
);
self::addArgument(
'destination',
InputArgument::OPTIONAL,
'The filepath for the generated route collection',
getcwd() . '/cache/routes.php'
);
$this->setDefinition(new InputDefinition([
new InputArgument(
name: 'openAPI',
mode: InputArgument::REQUIRED,
description: 'The absolute filepath to your OpenAPI'
),
new InputArgument(
name: 'destination',
mode: InputArgument::OPTIONAL,
description: 'The filepath for the generated route collection',
default: getcwd() . '/cache/routes.php'
),
new InputOption(
name: 'ignore-servers',
description: 'ignore servers, only use the default "/" server',
mode: InputOption::VALUE_NONE,
),
// @todo add support for this in the reader first
// new InputOption(
// name: 'with-hostless-fallback',
// description: 'add the default "/" server, if not already specified',
// ),
]));
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$openAPIFilePath = $input->getArgument('openAPI');
assert(is_string($openAPIFilePath));

$destination = $input->getArgument('destination');
assert(is_string($destination));

$ignoreServers = $input->getOption('ignore-servers');
assert(is_bool($ignoreServers));
// @todo add support this in the reader first
// $hostlessFallback = $input->getOption('with-hostless-fallback');

$logger = new ConsoleLogger($output);
$service = new CacheOpenAPIRoutesService($logger);

return $service->cache($openAPIFilePath, $destination) ? Command::SUCCESS : Command::FAILURE;
return (new Service\CacheOpenAPIRoutes($logger))->cache(
$openAPIFilePath,
$destination,
$ignoreServers,
// @todo add support this in the reader first
// $hostlessFallback,
) ? Command::SUCCESS : Command::FAILURE;
}
}
18 changes: 16 additions & 2 deletions src/Console/Service/CacheOpenAPIRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ public function __construct(
) {
}

public function cache(string $openAPIFilePath, string $cacheDestination): bool
{
public function cache(
string $openAPIFilePath,
string $cacheDestination,
bool $ignoreServers = false,
//@todo add support for this in the reader first
// bool $hostlessFallback,
): bool {
$existingFilePath = $cacheDestination;
while (!file_exists($existingFilePath)) {
$existingFilePath = dirname($existingFilePath);
Expand All @@ -38,6 +43,15 @@ public function cache(string $openAPIFilePath, string $cacheDestination): bool
return false;
}

if ($ignoreServers) {
$openApi = $openApi->withoutServers();
}

//@todo add support for this in reader first
// if ($hostlessFallback) {
// $openApi = $openApi->withHostlessFallback();
// }

try {
$routeCollection = (new RouteCollector())->collect($openApi);
} catch (CannotCollectRoutes $e) {
Expand Down
115 changes: 102 additions & 13 deletions tests/Console/Service/CacheOpenAPIRoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,35 @@ public function cannotRouteFromRelativeFilePaths(): void
self::assertFalse($this->sut->cache($filePath, $this->root . '/cache/routes.php'));
}

public static function successfulExecutionProvider(): array
#[Test]
#[DataProvider('provideRoutes')]
public function itCachesRoutes(
string $openAPI,
string $destination,
RouteCollection $expectedRouteCollection
): void {
self::assertTrue($this->sut->cache($openAPI, $destination));

$actualRouteCollection = eval('?>' . file_get_contents($destination));

self::assertEquals($expectedRouteCollection, $actualRouteCollection);
}

#[Test]
#[DataProvider('provideRoutesWithoutServers')]
public function itCachesRoutesWithoutServers(
string $openAPI,
string $destination,
RouteCollection $expectedRouteCollection
): void {
self::assertTrue($this->sut->cache($openAPI, $destination, true));

$actualRouteCollection = eval('?>' . file_get_contents($destination));

self::assertEquals($expectedRouteCollection, $actualRouteCollection);
}

public static function provideRoutes(): array
{
$petStoreRoutes = new RouteCollection([
'hosted' => [
Expand Down Expand Up @@ -271,17 +299,78 @@ public static function successfulExecutionProvider(): array
];
}

#[Test]
#[DataProvider('successfulExecutionProvider')]
public function successfulExecutionTest(
string $openAPI,
string $destination,
RouteCollection $expectedRouteCollection
): void {
self::assertTrue($this->sut->cache($openAPI, $destination));

$actualRouteCollection = eval('?>' . file_get_contents($destination));

self::assertEquals($expectedRouteCollection, $actualRouteCollection);
public static function provideRoutesWithoutServers(): array
{
$petStoreRoutes = new RouteCollection([
'hosted' => ['static' => [], 'dynamic' => ['regex' => '#^(?|)#', 'servers' => []]],
'hostless' => [
'static' => ['' => [
'static' => [
'/pets' => [
'get' => 'findPets',
'post' => 'addPet',
],
],
'dynamic' => [
'regex' => '#^(?|/pets/([^/]+)(*MARK:/pets/{id}))$#',
'paths' => [
'/pets/{id}' => [
'get' => 'find pet by id',
'delete' => 'deletePet',
],
],
],
]],
'dynamic' => ['regex' => '#^(?|)#', 'servers' => []],
],
]);
$weirdAndWonderfulRoutes = new RouteCollection([
'hosted' => ['static' => [], 'dynamic' => ['regex' => '#^(?|)#', 'servers' => []]],
'hostless' => [
'static' => ['' => [
'static' => [
'/or' => [
'post' => 'post-or',
],
'/xor' => [
'delete' => 'delete-xor',
],
'/however' => [
'get' => 'get-however',
'put' => 'put-however',
'post' => 'post-however',
],
],
'dynamic' => [
'regex' => '#^(?|/and/([^/]+)(*MARK:/and/{name}))$#',
'paths' => [
'/and/{name}' => [
'get' => 'get-and',
'put' => 'put-and',
'post' => 'post-and',
],
]
],
]],
'dynamic' => ['regex' => '#^(?|)#', 'servers' => []],
],
]);
return [
'successfully routes petstore-expanded.json' => [
__DIR__ . '/../../fixtures/docs/petstore-expanded.json',
vfsStream::url('root/cache/routes.php'),
$petStoreRoutes
],
'successfully routes the WeirdAndWonderful.json' => [
__DIR__ . '/../../fixtures/WeirdAndWonderful.json',
vfsStream::url('root/cache/routes.php'),
$weirdAndWonderfulRoutes
],
'successfully routes the WeirdAndWonderful.json and caches in a nested directory' => [
__DIR__ . '/../../fixtures/WeirdAndWonderful.json',
vfsStream::url('root/cache/nested-cache/nester-cache/nestest-cache/routes.php'),
$weirdAndWonderfulRoutes
]
];
}
}

0 comments on commit 61cb1d0

Please sign in to comment.