Skip to content

Commit

Permalink
Merge pull request #2024 from er1z/master
Browse files Browse the repository at this point in the history
added an --output option to the api:swagger:export
  • Loading branch information
dunglas committed Jun 18, 2018
2 parents c0de241 + 5f0873e commit 1ea1664
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Bridge/Symfony/Bundle/Command/SwaggerCommand.php
Expand Up @@ -56,7 +56,8 @@ protected function configure()
$this
->setName('api:swagger:export')
->setDescription('Dump the Swagger 2.0 (OpenAPI) documentation')
->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML');
->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML')
->addOption('output', 'o', InputOption::VALUE_OPTIONAL, 'Write output to file');
}

/**
Expand All @@ -67,6 +68,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats);
$data = $this->documentationNormalizer->normalize($documentation);
$content = $input->getOption('yaml') ? Yaml::dump($data) : json_encode($data, JSON_PRETTY_PRINT);
$output->writeln($content);

if (!empty($input->getOption('output'))) {
file_put_contents($input->getOption('output'), $content);
$output->writeln(
sprintf('Data written to %s', $input->getOption('output'))
);
} else {
$output->writeln($content);
}
}
}
10 changes: 10 additions & 0 deletions tests/Bridge/Symfony/Bundle/Command/SwaggerCommandTest.php
Expand Up @@ -54,6 +54,16 @@ public function testExecuteWithYaml()
$this->assertYaml($this->tester->getDisplay());
}

public function testWriteToFile()
{
$tmpFile = tempnam(sys_get_temp_dir(), 'test_write_to_file');

$this->tester->run(['command' => 'api:swagger:export', '--output' => $tmpFile]);

$this->assertJson(file_get_contents($tmpFile));
@unlink($tmpFile);
}

/**
* @param string $data
*/
Expand Down

0 comments on commit 1ea1664

Please sign in to comment.