vendor/api-platform/core/src/Core/Bridge/Symfony/Bundle/Command/SwaggerCommand.php line 48

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