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