NelmioApiDocBundle/Command/SwaggerDumpCommand.php

168 lines
5.4 KiB
PHP
Raw Normal View History

<?php
/*
* 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.
*/
namespace Nelmio\ApiDocBundle\Command;
use Nelmio\ApiDocBundle\Extractor\ApiDocExtractor;
2014-07-31 12:27:40 -07:00
use Nelmio\ApiDocBundle\Formatter\SwaggerFormatter;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
2014-07-31 12:27:40 -07:00
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
/**
2014-07-31 12:27:40 -07:00
* Console command to dump Swagger-compliant API definitions.
*
* @author Bez Hermoso <bez@activelamp.com>
*/
#[AsCommand(
name: 'api:swagger:dump',
description: 'Dumps Swagger-compliant API definitions.',
)]
class SwaggerDumpCommand extends Command
{
private Filesystem $filesystem;
2014-07-31 12:00:56 -07:00
public function __construct(
private readonly ApiDocExtractor $extractor,
private readonly SwaggerFormatter $formatter
) {
parent::__construct();
}
protected function configure(): void
{
2014-07-31 12:00:56 -07:00
$this->filesystem = new Filesystem();
$this
2014-07-31 12:27:40 -07:00
->addOption('resource', 'r', InputOption::VALUE_OPTIONAL, 'A specific resource API declaration to dump.')
->addOption('list-only', 'l', InputOption::VALUE_NONE, 'Dump resource list only.')
->addOption('pretty', 'p', InputOption::VALUE_NONE, 'Dump as prettified JSON.')
->addArgument('destination', InputArgument::OPTIONAL, 'Directory to dump JSON files in.', null)
;
}
2024-06-18 20:01:11 +03:00
protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($input->getOption('list-only') && $input->getOption('resource')) {
throw new \RuntimeException('Cannot selectively dump a resource with the --list-only flag.');
}
$apiDocs = $this->extractor->all();
if ($input->getOption('list-only')) {
$data = $this->getResourceList($apiDocs);
2014-07-31 12:00:56 -07:00
$this->dump($data, null, $input, $output);
2015-03-06 11:19:08 +01:00
2024-06-18 20:01:11 +03:00
return 0;
}
if (false !== ($resource = $input->getOption('resource'))) {
$data = $this->getApiDeclaration($apiDocs, $resource);
2014-07-31 12:00:56 -07:00
if (count($data['apis']) === 0) {
throw new \InvalidArgumentException(sprintf('Resource "%s" does not exist.', $resource));
}
$this->dump($data, $resource, $input, $output);
2015-03-06 11:19:08 +01:00
2024-06-18 20:01:11 +03:00
return 0;
2014-07-31 12:00:56 -07:00
}
2014-07-31 12:27:40 -07:00
/*
* If --list-only and --resource is not specified, dump everything.
*/
2014-07-31 12:00:56 -07:00
$data = $this->getResourceList($apiDocs);
2014-07-31 12:27:40 -07:00
if (!$input->getArguments('destination')) {
2014-07-31 12:00:56 -07:00
$output->writeln('');
$output->writeln('<comment>Resource list: </comment>');
}
2014-07-31 12:00:56 -07:00
$this->dump($data, null, $input, $output, false);
foreach ($data['apis'] as $api) {
$resource = substr($api['path'], 1);
2014-07-31 12:27:40 -07:00
if (!$input->getArgument('destination')) {
2014-07-31 12:00:56 -07:00
$output->writeln('');
$output->writeln(sprintf('<comment>API declaration for <info>"%s"</info> resource: </comment>', $resource));
}
2014-07-31 12:00:56 -07:00
$data = $this->getApiDeclaration($apiDocs, $resource, $output);
$this->dump($data, $resource, $input, $output, false);
}
2024-06-18 20:01:11 +03:00
return 0;
}
protected function dump(array $data, $resource, InputInterface $input, OutputInterface $output, $treatAsFile = true): void
{
2014-07-31 12:27:40 -07:00
$destination = $input->getArgument('destination');
2014-07-31 12:27:40 -07:00
$content = json_encode($data, $input->getOption('pretty') ? JSON_PRETTY_PRINT : 0);
2014-07-31 12:27:40 -07:00
if (!$destination) {
2014-07-31 12:00:56 -07:00
$output->writeln($content);
2015-03-06 11:19:08 +01:00
2014-07-31 12:00:56 -07:00
return;
}
2014-07-31 12:27:40 -07:00
if ($treatAsFile === false) {
if (!$this->filesystem->exists($destination)) {
$this->filesystem->mkdir($destination);
}
}
if (!$resource) {
if (!$treatAsFile) {
$destination = sprintf('%s/api-docs.json', rtrim($destination, '\\/'));
2014-07-31 12:00:56 -07:00
}
2014-07-31 12:27:40 -07:00
$message = sprintf('<comment>Dumping resource list to %s: </comment>', $destination);
$this->writeToFile($content, $destination, $output, $message);
2014-07-31 12:00:56 -07:00
return;
}
2014-07-31 12:00:56 -07:00
if ($treatAsFile === false) {
2014-07-31 12:27:40 -07:00
$destination = sprintf('%s/%s.json', rtrim($destination, '\\/'), $resource);
2014-07-31 12:00:56 -07:00
}
2014-07-31 12:27:40 -07:00
$message = sprintf('<comment>Dump API declaration to %s: </comment>', $destination);
$this->writeToFile($content, $destination, $output, $message);
2014-07-31 12:00:56 -07:00
}
protected function writeToFile($content, $file, OutputInterface $output, $message): void
2014-07-31 12:00:56 -07:00
{
try {
2014-07-31 12:00:56 -07:00
$this->filesystem->dumpFile($file, $content);
2014-07-31 12:27:40 -07:00
$message .= ' <info>OK</info>';
} catch (IOException $e) {
2014-07-31 12:27:40 -07:00
$message .= sprintf(' <error>NOT OK - %s</error>', $e->getMessage());
}
2014-07-31 12:27:40 -07:00
$output->writeln($message);
}
2014-07-31 12:00:56 -07:00
protected function getResourceList(array $data)
{
2014-07-31 12:27:40 -07:00
return $this->formatter->format($data);
2014-07-31 12:00:56 -07:00
}
2014-07-31 12:00:56 -07:00
protected function getApiDeclaration(array $data, $resource)
{
2014-07-31 12:27:40 -07:00
return $this->formatter->format($data, '/' . $resource);
}
2014-07-31 12:27:40 -07:00
}