Skip to content

Commit

Permalink
Add ignoreServers option
Browse files Browse the repository at this point in the history
  • Loading branch information
charjr committed Oct 11, 2024
1 parent eef3f79 commit dd3a69b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
50 changes: 36 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,49 @@ 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',
),
// @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)
{
$openAPIFilePath = $input->getArgument('openAPI');
assert(is_string($openAPIFilePath));

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

$ignoreServers = $input->getOption('servers');
$hostlessFallback = $input->getOption('servers');

$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,

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.1, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.1, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.2, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.3, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.3, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.4, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.1, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.1, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.2, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.3, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.3, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.

Check failure on line 65 in src/Console/Command/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.4, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Parameter #3 $ignoreServers of method Membrane\OpenAPIRouter\Console\Service\CacheOpenAPIRoutes::cache() expects bool, mixed given.
// @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,
//@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();

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.1, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.1, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.2, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.3, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.3, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.4, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.1, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.1, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.2, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.3, lowest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.3, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\OpenAPI::withoutServers().

Check failure on line 47 in src/Console/Service/CacheOpenAPIRoutes.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPStan [8.4, latest], ubuntu-latest, laminas/laminas-continuous-integration-action@v1...

Call to an undefined method Membrane\OpenAPIReader\ValueObject\Valid\V30\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

0 comments on commit dd3a69b

Please sign in to comment.