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. /**
  58. * {@inheritdoc}
  59. */
  60. protected function configure()
  61. {
  62. $this
  63. ->setDescription('Dump the Swagger v2 documentation')
  64. ->addOption('yaml', 'y', InputOption::VALUE_NONE, 'Dump the documentation in YAML')
  65. ->addOption('spec-version', null, InputOption::VALUE_OPTIONAL, sprintf('OpenAPI version to use (%s)', implode(' or ', $this->swaggerVersions)), (string) ($this->swaggerVersions[0] ?? 2))
  66. ->addOption('output', 'o', InputOption::VALUE_OPTIONAL, 'Write output to file')
  67. ->addOption('api-gateway', null, InputOption::VALUE_NONE, 'API Gateway compatibility');
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function execute(InputInterface $input, OutputInterface $output): int
  73. {
  74. $io = new SymfonyStyle($input, $output);
  75. /** @var string $version */
  76. $version = $input->getOption('spec-version');
  77. if (!\in_array((int) $version, $this->swaggerVersions, true)) {
  78. throw new InvalidOptionException(sprintf('This tool only supports versions %s of the OpenAPI specification ("%s" given).', implode(', ', $this->swaggerVersions), $version));
  79. }
  80. if (3 === (int) $version) {
  81. @trigger_error('The command "api:swagger:export" is deprecated for the spec version 3 use "api:openapi:export".', \E_USER_DEPRECATED);
  82. }
  83. if (!$this->legacyMode) {
  84. @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);
  85. }
  86. $documentation = new Documentation($this->resourceNameCollectionFactory->create(), $this->apiTitle, $this->apiDescription, $this->apiVersion, $this->apiFormats);
  87. $data = $this->normalizer->normalize($documentation, DocumentationNormalizer::FORMAT, ['spec_version' => (int) $version, ApiGatewayNormalizer::API_GATEWAY => $input->getOption('api-gateway')]);
  88. $content = $input->getOption('yaml')
  89. ? Yaml::dump($data, 10, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK)
  90. : (json_encode($data, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES) ?: '');
  91. if (!empty($filename = $input->getOption('output')) && \is_string($filename)) {
  92. file_put_contents($filename, $content);
  93. $io->success(sprintf('Data written to %s (specification version %s).', $filename, $version));
  94. } else {
  95. $output->writeln($content);
  96. }
  97. return 0;
  98. }
  99. public static function getDefaultName(): string
  100. {
  101. return 'api:swagger:export';
  102. }
  103. }