Skip to content

Commit 1ea1664

Browse files
authored
Merge pull request #2024 from er1z/master
added an --output option to the api:swagger:export
2 parents c0de241 + 5f0873e commit 1ea1664

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Bridge/Symfony/Bundle/Command/SwaggerCommand.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ protected function configure()
5656
$this
5757
->setName('api:swagger:export')
5858
->setDescription('Dump the Swagger 2.0 (OpenAPI) documentation')
59-
->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML');
59+
->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML')
60+
->addOption('output', 'o', InputOption::VALUE_OPTIONAL, 'Write output to file');
6061
}
6162

6263
/**
@@ -67,6 +68,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
6768
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats);
6869
$data = $this->documentationNormalizer->normalize($documentation);
6970
$content = $input->getOption('yaml') ? Yaml::dump($data) : json_encode($data, JSON_PRETTY_PRINT);
70-
$output->writeln($content);
71+
72+
if (!empty($input->getOption('output'))) {
73+
file_put_contents($input->getOption('output'), $content);
74+
$output->writeln(
75+
sprintf('Data written to %s', $input->getOption('output'))
76+
);
77+
} else {
78+
$output->writeln($content);
79+
}
7180
}
7281
}

tests/Bridge/Symfony/Bundle/Command/SwaggerCommandTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ public function testExecuteWithYaml()
5454
$this->assertYaml($this->tester->getDisplay());
5555
}
5656

57+
public function testWriteToFile()
58+
{
59+
$tmpFile = tempnam(sys_get_temp_dir(), 'test_write_to_file');
60+
61+
$this->tester->run(['command' => 'api:swagger:export', '--output' => $tmpFile]);
62+
63+
$this->assertJson(file_get_contents($tmpFile));
64+
@unlink($tmpFile);
65+
}
66+
5767
/**
5868
* @param string $data
5969
*/

0 commit comments

Comments
 (0)