16
16
use ApiPlatform \Core \Documentation \Documentation ;
17
17
use ApiPlatform \Core \Metadata \Resource \Factory \ResourceNameCollectionFactoryInterface ;
18
18
use Symfony \Component \Console \Command \Command ;
19
+ use Symfony \Component \Console \Exception \InvalidOptionException ;
19
20
use Symfony \Component \Console \Input \InputInterface ;
20
21
use Symfony \Component \Console \Input \InputOption ;
21
22
use Symfony \Component \Console \Output \OutputInterface ;
29
30
*/
30
31
final class SwaggerCommand extends Command
31
32
{
32
- private $ documentationNormalizer ;
33
+ private $ v2documentationNormalizer ;
34
+ private $ v3documentationNormalizer ;
33
35
private $ resourceNameCollectionFactory ;
34
36
private $ apiTitle ;
35
37
private $ apiDescription ;
36
38
private $ apiVersion ;
37
39
private $ apiFormats ;
38
40
39
- public function __construct (NormalizerInterface $ documentationNormalizer , ResourceNameCollectionFactoryInterface $ resourceNameCollection , string $ apiTitle , string $ apiDescription , string $ apiVersion , array $ apiFormats )
41
+ public function __construct (NormalizerInterface $ v2documentationNormalizer , NormalizerInterface $ v3documentationNormalizer , ResourceNameCollectionFactoryInterface $ resourceNameCollection , string $ apiTitle , string $ apiDescription , string $ apiVersion , array $ apiFormats )
40
42
{
41
43
parent ::__construct ();
42
44
43
- $ this ->documentationNormalizer = $ documentationNormalizer ;
45
+ $ this ->v2documentationNormalizer = $ v2documentationNormalizer ;
46
+ $ this ->v3documentationNormalizer = $ v3documentationNormalizer ;
44
47
$ this ->resourceNameCollectionFactory = $ resourceNameCollection ;
45
48
$ this ->apiTitle = $ apiTitle ;
46
49
$ this ->apiDescription = $ apiDescription ;
@@ -54,9 +57,11 @@ public function __construct(NormalizerInterface $documentationNormalizer, Resour
54
57
protected function configure ()
55
58
{
56
59
$ this
57
- ->setName ('api:swagger:export ' )
58
- ->setDescription ('Dump the Swagger 2.0 (OpenAPI) documentation ' )
60
+ ->setName ('api:openapi:export ' )
61
+ ->setAliases (['api:swagger:export ' ])
62
+ ->setDescription ('Dump the OpenAPI documentation ' )
59
63
->addOption ('yaml ' , 'y ' , InputOption::VALUE_NONE , 'Dump the documentation in YAML ' )
64
+ ->addOption ('spec-version ' , null , InputOption::VALUE_OPTIONAL , 'OpenAPI version to use ("2" or "3") ' , '2 ' )
60
65
->addOption ('output ' , 'o ' , InputOption::VALUE_OPTIONAL , 'Write output to file ' );
61
66
}
62
67
@@ -65,9 +70,15 @@ protected function configure()
65
70
*/
66
71
protected function execute (InputInterface $ input , OutputInterface $ output )
67
72
{
73
+ /** @var string $version */
74
+ $ version = $ input ->getOption ('spec-version ' );
75
+ if (!\in_array ($ version , ['2 ' , '3 ' ], true )) {
76
+ throw new InvalidOptionException (sprintf ('This tool only support version 2 and 3 of the OpenAPI specification ("%s" given). ' , $ version ));
77
+ }
78
+ $ documentationNormalizer = sprintf ('v%ddocumentationNormalizer ' , $ version );
68
79
$ documentation = new Documentation ($ this ->resourceNameCollectionFactory ->create (), $ this ->apiTitle , $ this ->apiDescription , $ this ->apiVersion , $ this ->apiFormats );
69
- $ data = $ this ->documentationNormalizer ->normalize ($ documentation );
70
- $ content = $ input ->getOption ('yaml ' ) ? Yaml::dump ($ data , 6 , 4 , Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE ) : (json_encode ($ data , JSON_PRETTY_PRINT ) ?: '' );
80
+ $ data = $ this ->$ documentationNormalizer ->normalize ($ documentation );
81
+ $ content = $ input ->getOption ('yaml ' ) ? Yaml::dump ($ data , 10 , 2 , Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE ) : (json_encode ($ data , JSON_PRETTY_PRINT ) ?: '' );
71
82
if (!empty ($ filename = $ input ->getOption ('output ' )) && \is_string ($ filename )) {
72
83
file_put_contents ($ filename , $ content );
73
84
$ output ->writeln (
0 commit comments