2012-04-12 01:42:52 +02:00
|
|
|
<?php
|
|
|
|
|
2012-04-13 11:03:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the NelmioApiDocBundle.
|
|
|
|
*
|
|
|
|
* (c) Nelmio <hello@nelm.io>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-04-12 18:37:42 +02:00
|
|
|
namespace Nelmio\ApiDocBundle\Command;
|
2012-04-12 01:42:52 +02:00
|
|
|
|
2015-05-26 18:09:29 +07:00
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
2024-07-02 16:24:26 +03:00
|
|
|
use Nelmio\ApiDocBundle\Extractor\ApiDocExtractor;
|
|
|
|
use Nelmio\ApiDocBundle\Formatter\HtmlFormatter;
|
|
|
|
use Nelmio\ApiDocBundle\Formatter\MarkdownFormatter;
|
|
|
|
use Nelmio\ApiDocBundle\Formatter\SimpleFormatter;
|
2024-06-18 00:19:53 +03:00
|
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
2022-10-07 15:32:38 +03:00
|
|
|
use Symfony\Component\Console\Command\Command;
|
2012-04-12 01:42:52 +02:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2024-06-18 12:30:31 +03:00
|
|
|
use Symfony\Contracts\Translation\LocaleAwareInterface;
|
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2012-04-12 01:42:52 +02:00
|
|
|
|
2024-06-18 00:19:53 +03:00
|
|
|
#[AsCommand(
|
|
|
|
name: 'api:doc:dump',
|
|
|
|
description: 'Dumps API documentation in various formats',
|
|
|
|
)]
|
|
|
|
class DumpCommand extends Command
|
2012-04-12 01:42:52 +02:00
|
|
|
{
|
2024-07-02 16:24:26 +03:00
|
|
|
private const AVAILABLE_FORMATS = ['markdown', 'json', 'html'];
|
2012-04-12 01:42:52 +02:00
|
|
|
|
2024-06-18 12:30:31 +03:00
|
|
|
/**
|
|
|
|
* @param TranslatorInterface&LocaleAwareInterface $translator
|
|
|
|
*/
|
2024-06-18 00:19:53 +03:00
|
|
|
public function __construct(
|
2024-07-02 16:24:26 +03:00
|
|
|
private readonly SimpleFormatter $simpleFormatter,
|
|
|
|
private readonly MarkdownFormatter $markdownFormatter,
|
|
|
|
private readonly HtmlFormatter $htmlFormatter,
|
|
|
|
private readonly ApiDocExtractor $apiDocExtractor,
|
|
|
|
private readonly TranslatorInterface $translator
|
2024-06-18 00:19:53 +03:00
|
|
|
) {
|
2024-07-02 16:24:26 +03:00
|
|
|
parent::__construct();
|
2024-06-18 00:19:53 +03:00
|
|
|
}
|
|
|
|
|
2024-07-02 16:24:26 +03:00
|
|
|
protected function configure(): void
|
2012-04-12 01:42:52 +02:00
|
|
|
{
|
|
|
|
$this
|
|
|
|
->addOption(
|
|
|
|
'format', '', InputOption::VALUE_REQUIRED,
|
2024-07-02 16:24:26 +03:00
|
|
|
'Output format like: ' . implode(', ', self::AVAILABLE_FORMATS),
|
|
|
|
self::AVAILABLE_FORMATS[0]
|
2012-04-12 01:42:52 +02:00
|
|
|
)
|
2019-04-24 12:55:08 +03:00
|
|
|
->addOption('api-version', null, InputOption::VALUE_REQUIRED, 'The API version')
|
2019-04-24 13:06:08 +03:00
|
|
|
->addOption('locale', null, InputOption::VALUE_REQUIRED, 'Locale for translation')
|
2015-05-26 18:09:29 +07:00
|
|
|
->addOption('view', '', InputOption::VALUE_OPTIONAL, '', ApiDoc::DEFAULT_VIEW)
|
2012-07-18 16:51:53 +02:00
|
|
|
->addOption('no-sandbox', '', InputOption::VALUE_NONE)
|
2024-06-18 00:19:53 +03:00
|
|
|
;
|
2012-04-12 01:42:52 +02:00
|
|
|
}
|
|
|
|
|
2024-06-18 20:01:11 +03:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
2012-04-12 01:42:52 +02:00
|
|
|
{
|
|
|
|
$format = $input->getOption('format');
|
2015-05-26 18:09:29 +07:00
|
|
|
$view = $input->getOption('view');
|
|
|
|
|
2024-07-02 16:24:26 +03:00
|
|
|
$formatter = match ($format) {
|
|
|
|
'json' => $this->simpleFormatter,
|
|
|
|
'markdown' => $this->markdownFormatter,
|
|
|
|
'html' => $this->htmlFormatter,
|
|
|
|
default => throw new \RuntimeException(sprintf('Format "%s" not supported.', $format)),
|
|
|
|
};
|
2012-04-12 01:42:52 +02:00
|
|
|
|
2019-04-24 13:06:08 +03:00
|
|
|
if ($input->hasOption('locale')) {
|
2024-06-18 12:30:31 +03:00
|
|
|
$this->translator->setLocale($input->getOption('locale') ?? '');
|
2019-04-24 13:06:08 +03:00
|
|
|
}
|
|
|
|
|
2019-04-24 13:12:35 +03:00
|
|
|
if ($input->hasOption('api-version')) {
|
|
|
|
$formatter->setVersion($input->getOption('api-version'));
|
|
|
|
}
|
|
|
|
|
2024-07-02 16:24:26 +03:00
|
|
|
if ($formatter instanceof HtmlFormatter && $input->getOption('no-sandbox')) {
|
2012-07-18 16:51:53 +02:00
|
|
|
$formatter->setEnableSandbox(false);
|
|
|
|
}
|
|
|
|
|
2019-04-24 12:55:08 +03:00
|
|
|
$extractedDoc = $input->hasOption('api-version') ?
|
2024-07-02 16:24:26 +03:00
|
|
|
$this->apiDocExtractor->allForVersion($input->getOption('api-version'), $view) :
|
|
|
|
$this->apiDocExtractor->all($view);
|
2019-04-24 12:55:08 +03:00
|
|
|
|
2012-04-12 17:48:21 +02:00
|
|
|
$formattedDoc = $formatter->format($extractedDoc);
|
2012-04-12 01:42:52 +02:00
|
|
|
|
|
|
|
if ('json' === $format) {
|
2024-07-02 16:24:26 +03:00
|
|
|
$output->writeln(json_encode($formattedDoc, JSON_THROW_ON_ERROR));
|
2012-04-12 01:42:52 +02:00
|
|
|
} else {
|
2016-06-10 10:47:43 +02:00
|
|
|
$output->writeln($formattedDoc, OutputInterface::OUTPUT_RAW);
|
2012-04-12 01:42:52 +02:00
|
|
|
}
|
2022-10-07 15:32:38 +03:00
|
|
|
|
|
|
|
return 0;
|
2012-04-12 01:42:52 +02:00
|
|
|
}
|
|
|
|
}
|