File tree Expand file tree Collapse file tree 7 files changed +105
-2
lines changed Expand file tree Collapse file tree 7 files changed +105
-2
lines changed Original file line number Diff line number Diff line change 16
16
"Mtrajano\\ LaravelSwagger\\ " : " src/"
17
17
}
18
18
},
19
+ "suggest" : {
20
+ "ext-yaml" : " Required to use the YAML output option"
21
+ },
19
22
"extra" : {
20
23
"laravel" : {
21
24
"providers" : [
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Mtrajano \LaravelSwagger ;
4
+
5
+ class FormatterManager
6
+ {
7
+ private $ docs ;
8
+
9
+ private $ formatter ;
10
+
11
+ public function __construct ($ docs )
12
+ {
13
+ $ this ->docs = $ docs ;
14
+
15
+ $ this ->formatter = new Formatters \JsonFormatter ($ docs );
16
+ }
17
+
18
+ public function setFormat ($ format )
19
+ {
20
+ $ format = strtolower ($ format );
21
+
22
+ $ this ->formatter = $ this ->getFormatter ($ format );
23
+
24
+ return $ this ;
25
+ }
26
+
27
+ protected function getFormatter ($ format )
28
+ {
29
+ switch ($ format ) {
30
+ case 'json ' :
31
+ return new Formatters \JsonFormatter ($ this ->docs );
32
+ case 'yaml ' :
33
+ return new Formatters \YamlFormatter ($ this ->docs );
34
+ default :
35
+ throw new LaravelSwaggerException ('Invalid format passed ' );
36
+ }
37
+ }
38
+
39
+ public function format ()
40
+ {
41
+ return $ this ->formatter ->format ();
42
+ }
43
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Mtrajano \LaravelSwagger \Formatters ;
4
+
5
+ abstract class Formatter
6
+ {
7
+ protected $ docs ;
8
+
9
+ public function __construct ($ docs )
10
+ {
11
+ $ this ->docs = $ docs ;
12
+ }
13
+
14
+ abstract public function format ();
15
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Mtrajano \LaravelSwagger \Formatters ;
4
+
5
+ class JsonFormatter extends Formatter
6
+ {
7
+ public function format ()
8
+ {
9
+ if (!extension_loaded ('json ' )) {
10
+ throw new \Exception ('JSON extension must be loaded to use the json output format ' );
11
+ }
12
+
13
+ return json_encode ($ this ->docs , JSON_PRETTY_PRINT );
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Mtrajano \LaravelSwagger \Formatters ;
4
+
5
+ class YamlFormatter extends Formatter
6
+ {
7
+ public function format ()
8
+ {
9
+ if (!extension_loaded ('yaml ' )) {
10
+ throw new \Exception ('YAML extension must be loaded to use the yaml output format ' );
11
+ }
12
+
13
+ return yaml_emit ($ this ->docs );
14
+ }
15
+ }
Original file line number Diff line number Diff line change @@ -11,7 +11,8 @@ class GenerateSwaggerDoc extends Command
11
11
*
12
12
* @var string
13
13
*/
14
- protected $ signature = 'laravel-swagger:generate ' ;
14
+ protected $ signature = 'laravel-swagger:generate
15
+ {--format=json : The format of the output, current options are json and yaml} ' ;
15
16
16
17
/**
17
18
* The console command description.
@@ -31,6 +32,10 @@ public function handle()
31
32
32
33
$ docs = (new Generator ($ config ))->generate ();
33
34
34
- echo json_encode ($ docs , JSON_PRETTY_PRINT ) . "\n" ;
35
+ $ formattedDocs = (new FormatterManager ($ docs ))
36
+ ->setFormat ($ this ->option ('format ' ))
37
+ ->format ();
38
+
39
+ $ this ->line ($ formattedDocs );
35
40
}
36
41
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Mtrajano \LaravelSwagger ;
4
+
5
+ class LaravelSwaggerException extends \Exception
6
+ {
7
+ }
You can’t perform that action at this time.
0 commit comments