diff --git a/Command/DumpCommand.php b/Command/DumpCommand.php new file mode 100644 index 0000000..8b86b23 --- /dev/null +++ b/Command/DumpCommand.php @@ -0,0 +1,78 @@ +generatorLocator = $generatorLocator; + + parent::__construct(); + } + + /** + * Configures the dump command. + */ + protected function configure() + { + $this + ->setDescription('Dumps documentation in OpenAPI JSON format') + ->addOption('area', '', InputOption::VALUE_OPTIONAL, '', 'default') + ->addOption('no-pretty', '', InputOption::VALUE_NONE, 'Do not pretty format output') + ; + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * + * @throws InvalidArgumentException If the area to dump is not valid + * + * @return int|void + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $area = $input->getOption('area'); + + if (!$this->generatorLocator->has($area)) { + throw new InvalidArgumentException(sprintf('Area "%s" is not supported.', $area)); + } + + $spec = $this->generatorLocator->get($area)->generate(); + + if ($input->hasParameterOption(['--no-pretty'])) { + $output->writeln(json_encode($spec)); + } else { + $output->writeln(json_encode($spec, JSON_PRETTY_PRINT)); + } + + return 0; + } +} diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 91cda00..67a499d 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -4,6 +4,12 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + + + + + diff --git a/Tests/Command/DumpCommandTest.php b/Tests/Command/DumpCommandTest.php new file mode 100644 index 0000000..2ccb15e --- /dev/null +++ b/Tests/Command/DumpCommandTest.php @@ -0,0 +1,36 @@ +find('nelmio:apidoc:dump'); + $commandTester = new CommandTester($command); + $commandTester->execute([ + '--area' => 'test', + '--no-pretty' => '', + ]); + + // the output of the command in the console + $output = $commandTester->getDisplay(); + $this->assertEquals(json_encode($this->getOpenApiDefinition('test'))."\n", $output); + } +}