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 8d19acb
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 453 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
Loading

0 comments on commit 8d19acb

Please sign in to comment.