2020-05-31 10:39:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the NelmioApiDocBundle package.
|
|
|
|
*
|
|
|
|
* (c) Nelmio
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Nelmio\ApiDocBundle\Command;
|
|
|
|
|
2021-06-27 09:24:35 +02:00
|
|
|
use Nelmio\ApiDocBundle\Render\Html\AssetsMode;
|
2021-06-27 08:41:33 +02:00
|
|
|
use Nelmio\ApiDocBundle\Render\RenderOpenApi;
|
2020-05-31 10:39:50 +01:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
class DumpCommand extends Command
|
|
|
|
{
|
|
|
|
/**
|
2021-06-27 08:41:33 +02:00
|
|
|
* @var RenderOpenApi
|
2020-05-31 10:39:50 +01:00
|
|
|
*/
|
2021-06-27 08:41:33 +02:00
|
|
|
private $renderOpenApi;
|
2020-05-31 10:39:50 +01:00
|
|
|
|
2021-06-27 09:24:35 +02:00
|
|
|
/**
|
|
|
|
* @var mixed[]
|
|
|
|
*/
|
|
|
|
private $defaultHtmlConfig = [
|
|
|
|
'assets_mode' => AssetsMode::CDN,
|
|
|
|
'swagger_ui_config' => [],
|
|
|
|
'server_url' => null,
|
|
|
|
];
|
|
|
|
|
2021-06-27 08:41:33 +02:00
|
|
|
public function __construct(RenderOpenApi $renderOpenApi)
|
2020-05-31 10:39:50 +01:00
|
|
|
{
|
2021-06-27 08:41:33 +02:00
|
|
|
$this->renderOpenApi = $renderOpenApi;
|
2020-05-31 10:39:50 +01:00
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configures the dump command.
|
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
2021-06-27 09:24:35 +02:00
|
|
|
$availableFormats = $this->renderOpenApi->getAvailableFormats();
|
2020-05-31 10:39:50 +01:00
|
|
|
$this
|
2021-07-29 11:57:00 +02:00
|
|
|
->setDescription('Dumps documentation in OpenAPI format to: '.implode(', ', $availableFormats))
|
2020-05-31 10:39:50 +01:00
|
|
|
->addOption('area', '', InputOption::VALUE_OPTIONAL, '', 'default')
|
2021-06-27 09:24:35 +02:00
|
|
|
->addOption(
|
|
|
|
'format',
|
|
|
|
'',
|
|
|
|
InputOption::VALUE_REQUIRED,
|
|
|
|
'Output format like: '.implode(', ', $availableFormats),
|
|
|
|
RenderOpenApi::JSON
|
|
|
|
)
|
|
|
|
->addOption('html-config', '', InputOption::VALUE_REQUIRED, '', json_encode($this->defaultHtmlConfig))
|
2020-05-31 10:39:50 +01:00
|
|
|
->addOption('no-pretty', '', InputOption::VALUE_NONE, 'Do not pretty format output')
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int|void
|
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$area = $input->getOption('area');
|
2021-06-27 09:24:35 +02:00
|
|
|
$format = $input->getOption('format');
|
|
|
|
|
|
|
|
$options = [];
|
|
|
|
if (RenderOpenApi::HTML === $format) {
|
|
|
|
$rawHtmlConfig = json_decode($input->getOption('html-config'), true);
|
|
|
|
$options = is_array($rawHtmlConfig) ? $rawHtmlConfig : $this->defaultHtmlConfig;
|
|
|
|
} elseif (RenderOpenApi::JSON === $format) {
|
|
|
|
$options = [
|
|
|
|
'no-pretty' => $input->hasParameterOption(['--no-pretty']),
|
|
|
|
];
|
|
|
|
}
|
2020-05-31 10:39:50 +01:00
|
|
|
|
2021-06-27 09:24:35 +02:00
|
|
|
$docs = $this->renderOpenApi->render($format, $area, $options);
|
2021-06-27 08:41:33 +02:00
|
|
|
$output->writeln($docs, OutputInterface::OUTPUT_RAW);
|
2020-05-31 10:39:50 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|