Skip to content

Commit

Permalink
Add possibility to choose the destination of the file
Browse files Browse the repository at this point in the history
  • Loading branch information
Th3Mouk committed Sep 27, 2018
1 parent e81fd69 commit 6b7c633
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Th3Mouk\OpenAPIGenerator\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
Expand All @@ -15,26 +16,27 @@ protected function configure()
{
$this
->setName('generate')
->setDescription('Generate the swagger.json')
->setDescription('Generate the openapi.json')
->addArgument('path', InputArgument::OPTIONAL, 'The path where generate the openapi.json file', '')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->generateJson();
$this->generateJson($input);
echo 'generated'.PHP_EOL;
}

private function generateJson()
private function generateJson(InputInterface $input)
{
$templateFile = (new Finder())
->in(getRootPath().PathHelper::ROOT)
->files()
->name('swagger.yaml')
->name('openapi.yaml')
;

if (empty($templateFile)) {
echo 'no swagger.yaml file found'.PHP_EOL;
echo 'no openapi.yaml file found'.PHP_EOL;
return;
}

Expand All @@ -58,8 +60,12 @@ private function generateJson()
$template['definitions'] = (object) $definitions;
$template['paths'] = (object) $paths;

if (!$file = fopen(getRootPath().PathHelper::ROOT.'/swagger.json', 'w')) {
echo 'error generating json file'.PHP_EOL;
$argPath = $input->getArgument('path');
$path = '/' !== substr($argPath, 0, 1) ? '/'.$argPath : $argPath ;

$openapiFilePath = getRootPath().$path.'/openapi.json';
if (!$file = fopen($openapiFilePath, 'w')) {
echo 'error generating openapi.json file'.PHP_EOL;
return;
}

Expand Down

0 comments on commit 6b7c633

Please sign in to comment.