-
-
Notifications
You must be signed in to change notification settings - Fork 838
/
SwaggerCommand.php
108 lines (93 loc) · 4.4 KB
/
SwaggerCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Command;
use ApiPlatform\Core\Documentation\Documentation;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Core\Swagger\Serializer\ApiGatewayNormalizer;
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Yaml\Yaml;
/**
* Console command to dump Swagger API documentations.
*
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
*/
final class SwaggerCommand extends Command
{
private $normalizer;
private $resourceNameCollectionFactory;
private $apiTitle;
private $apiDescription;
private $apiVersion;
private $apiFormats;
private $swaggerVersions;
/**
* @param int[] $swaggerVersions
*/
public function __construct(NormalizerInterface $normalizer, ResourceNameCollectionFactoryInterface $resourceNameCollection, string $apiTitle, string $apiDescription, string $apiVersion, array $apiFormats = null, array $swaggerVersions = [2, 3])
{
$this->normalizer = $normalizer;
$this->resourceNameCollectionFactory = $resourceNameCollection;
$this->apiTitle = $apiTitle;
$this->apiDescription = $apiDescription;
$this->apiVersion = $apiVersion;
$this->apiFormats = $apiFormats;
$this->swaggerVersions = $swaggerVersions;
if (null !== $apiFormats) {
@trigger_error(sprintf('Passing a 6th parameter to the constructor of "%s" is deprecated since API Platform 2.5', __CLASS__), E_USER_DEPRECATED);
}
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('api:openapi:export')
->setAliases(['api:swagger:export'])
->setDescription('Dump the OpenAPI documentation')
->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML')
->addOption('spec-version', null, InputOption::VALUE_OPTIONAL, sprintf('OpenAPI version to use (%s)', implode(' or ', $this->swaggerVersions)), $this->swaggerVersions[0] ?? 2)
->addOption('output', 'o', InputOption::VALUE_OPTIONAL, 'Write output to file')
->addOption('api-gateway', null, InputOption::VALUE_NONE, 'API Gateway compatibility');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
/** @var string $version */
$version = $input->getOption('spec-version');
if (!\in_array((int) $version, $this->swaggerVersions, true)) {
throw new InvalidOptionException(sprintf('This tool only supports versions %s of the OpenAPI specification ("%s" given).', implode(', ', $this->swaggerVersions), $version));
}
$documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats);
$data = $this->normalizer->normalize($documentation, DocumentationNormalizer::FORMAT, ['spec_version' => (int) $version, ApiGatewayNormalizer::API_GATEWAY => $input->getOption('api-gateway')]);
$content = $input->getOption('yaml')
? Yaml::dump($data, 10, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)
: (json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) ?: '');
if (!empty($filename = $input->getOption('output')) && \is_string($filename)) {
file_put_contents($filename, $content);
$io->success(sprintf('Data written to %s (specification version %s).', $filename, $version));
} else {
$output->writeln($content);
}
return 0;
}
}