-
Notifications
You must be signed in to change notification settings - Fork 946
Migrate bin/openapi to Symfony Console
#1977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ce85d8e
Implement Symfony Console integration for OpenAPI command generation
yceruto baccd77
Refactor GenerateCommand to use attributes for command configuration
yceruto f18eaf2
Enhance GenerateCommand to utilize ConsoleLogger for improved logging
yceruto d7ceb1e
Refactor GenerateCommand and GenerateInput to improve bootstrap handl…
yceruto 734d780
Refactor GenerateCommand and GenerateInput to use GenerateFormat enum…
yceruto 644fa4d
Update composer.json to allow symfony/console version 7.4 alongside 8.0
yceruto c83b434
Add license declaration and enforce strict types in GenerateFormat.php
yceruto b4f97be
Refactor GenerateCommand and GenerateInput to use OpenApi annotations…
yceruto 06aebb8
Remove ErrorListener
yceruto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @license Apache 2.0 | ||
| */ | ||
|
|
||
| namespace OpenApi\Console; | ||
|
|
||
| use OpenApi\Annotations as OA; | ||
| use OpenApi\Generator; | ||
| use OpenApi\SourceFinder; | ||
| use Symfony\Component\Console\Attribute\AsCommand; | ||
| use Symfony\Component\Console\Attribute\MapInput; | ||
| use Symfony\Component\Console\Logger\ConsoleLogger; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
|
||
| #[AsCommand( | ||
| name: 'openapi', | ||
| description: 'Generate OpenAPI documentation', | ||
| )] | ||
| class GenerateCommand | ||
| { | ||
| public function __construct( | ||
| private ConsoleLogger $logger, | ||
| ) { | ||
| } | ||
|
|
||
| public function __invoke(#[MapInput] GenerateInput $input, SymfonyStyle $io): int | ||
| { | ||
| $io->setVerbosity($input->debug ? OutputInterface::VERBOSITY_DEBUG : $io->getVerbosity()); | ||
|
|
||
| foreach ($input->getBootstrapFilenames() as $filename) { | ||
| if ($io->isVerbose()) { | ||
| $io->info('Bootstrapping: ' . $filename); | ||
| } | ||
|
|
||
| require_once($filename); | ||
| } | ||
|
|
||
| if ($input->defaults) { | ||
| $io->title('Default config'); | ||
| $io->writeln(json_encode((new Generator())->getDefaultConfig(), JSON_PRETTY_PRINT)); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| $openapi = $this->generate($input); | ||
|
|
||
| if (!$input->output) { | ||
| if ($input->format->isJson()) { | ||
| echo $openapi->toJson(); | ||
| } else { | ||
| echo $openapi->toYaml(); | ||
| } | ||
| echo "\n"; | ||
| } else { | ||
| $outputPath = $input->output; | ||
| if (is_dir($outputPath)) { | ||
| $outputPath .= '/openapi.yaml'; | ||
| } | ||
| $openapi->saveAs($outputPath, $input->format->value); | ||
| } | ||
|
|
||
| return $this->logger->hasErrored() ? 1 : 0; | ||
| } | ||
|
|
||
| private function generate(GenerateInput $input): OA\OpenApi | ||
| { | ||
| $generator = new Generator($this->logger); | ||
|
|
||
| foreach ($input->addProcessor as $processor) { | ||
| $class = '\OpenApi\Processors\\' . ucfirst((string) $processor); | ||
| if (class_exists($class)) { | ||
| $processor = new $class(); | ||
| } elseif (class_exists($processor)) { | ||
| $processor = new $processor(); | ||
| } | ||
| $generator->getProcessorPipeline()->add($processor); | ||
| } | ||
|
|
||
| foreach ($input->removeProcessor as $processor) { | ||
| $class = class_exists($processor) | ||
| ? $processor | ||
| : '\OpenApi\Processors\\' . ucfirst((string) $processor); | ||
| $generator->getProcessorPipeline()->remove($class); | ||
| } | ||
|
|
||
| return $generator | ||
| ->setVersion($input->version) | ||
| ->setConfig($input->config) | ||
| ->generate(new SourceFinder($input->paths, $input->exclude, $input->pattern)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.