NelmioApiDocBundle/Command/DumpCommand.php

104 lines
3.4 KiB
PHP
Raw Normal View History

<?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;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
2024-06-18 12:30:31 +03:00
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
#[AsCommand(
name: 'api:doc:dump',
description: 'Dumps API documentation in various formats',
)]
class DumpCommand extends Command
{
/**
* @var array
*/
2012-04-12 17:48:21 +02:00
protected $availableFormats = array('markdown', 'json', 'html');
2024-06-18 12:30:31 +03:00
/**
* @param TranslatorInterface&LocaleAwareInterface $translator
*/
public function __construct(
private ContainerInterface $container,
2024-06-18 12:30:31 +03:00
private TranslatorInterface $translator,
string $name = null
) {
parent::__construct($name);
}
protected function configure()
{
$this
->addOption(
'format', '', InputOption::VALUE_REQUIRED,
'Output format like: ' . implode(', ', $this->availableFormats),
$this->availableFormats[0]
)
->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')
->addOption('view', '', InputOption::VALUE_OPTIONAL, '', ApiDoc::DEFAULT_VIEW)
->addOption('no-sandbox', '', InputOption::VALUE_NONE)
;
}
2024-06-18 20:01:11 +03:00
protected function execute(InputInterface $input, OutputInterface $output): int
{
$format = $input->getOption('format');
$view = $input->getOption('view');
2024-06-18 12:30:31 +03:00
if ($format === 'json') {
$formatter = $this->container->get('nelmio_api_doc.formatter.simple_formatter');
} else {
2012-04-12 17:48:21 +02:00
if (!in_array($format, $this->availableFormats)) {
throw new \RuntimeException(sprintf('Format "%s" not supported.', $format));
}
$formatter = $this->container->get(sprintf('nelmio_api_doc.formatter.%s_formatter', $format));
}
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'));
}
if ($input->getOption('no-sandbox') && 'html' === $format) {
$formatter->setEnableSandbox(false);
}
$extractor = $this->container->get('nelmio_api_doc.extractor.api_doc_extractor');
$extractedDoc = $input->hasOption('api-version') ?
$extractor->allForVersion($input->getOption('api-version'), $view) :
$extractor->all($view);
2012-04-12 17:48:21 +02:00
$formattedDoc = $formatter->format($extractedDoc);
if ('json' === $format) {
2012-04-12 17:48:21 +02:00
$output->writeln(json_encode($formattedDoc));
} else {
$output->writeln($formattedDoc, OutputInterface::OUTPUT_RAW);
}
return 0;
}
}