2
2
3
3
namespace Rakutentech \LaravelRequestDocs \Commands ;
4
4
5
+ use ErrorException ;
6
+ use Exception ;
5
7
use Illuminate \Console \Command ;
8
+ use Illuminate \Support \Collection ;
6
9
use Rakutentech \LaravelRequestDocs \LaravelRequestDocs ;
7
10
use Rakutentech \LaravelRequestDocs \LaravelRequestDocsToOpenApi ;
8
11
@@ -15,7 +18,7 @@ public function __construct(LaravelRequestDocs $laravelRequestDoc, LaravelReques
15
18
{
16
19
parent ::__construct ();
17
20
18
- $ this ->laravelRequestDocs = $ laravelRequestDoc ;
21
+ $ this ->laravelRequestDocs = $ laravelRequestDoc ;
19
22
$ this ->laravelRequestDocsToOpenApi = $ laravelRequestDocsToOpenApi ;
20
23
}
21
24
@@ -42,49 +45,45 @@ public function __construct(LaravelRequestDocs $laravelRequestDoc, LaravelReques
42
45
*/
43
46
public function handle ()
44
47
{
45
- if ($ this ->confirmFilePath ()) {
46
- try {
47
- $ excludedMethods = config ('request-docs.open_api.exclude_http_methods ' , []);
48
-
49
- $ excludedMethods = array_map (fn ($ item ) => strtolower ($ item ), $ excludedMethods );
50
-
51
- $ showGet = !in_array ('get ' , $ excludedMethods );
52
- $ showPost = !in_array ('post ' , $ excludedMethods );
53
- $ showPut = !in_array ('put ' , $ excludedMethods );
54
- $ showPatch = !in_array ('patch ' , $ excludedMethods );
55
- $ showDelete = !in_array ('delete ' , $ excludedMethods );
56
- $ showHead = !in_array ('head ' , $ excludedMethods );
57
-
58
- // Get a list of Doc with route and rules information.
59
- // If user defined `Route::match(['get', 'post'], 'uri', ...)`,
60
- // only a single Doc will be generated.
61
- $ docs = $ this ->laravelRequestDocs ->getDocs (
62
- $ showGet ,
63
- $ showPost ,
64
- $ showPut ,
65
- $ showPatch ,
66
- $ showDelete ,
67
- $ showHead ,
68
- );
69
-
70
- // Loop and split Doc by the `methods` property.
71
- // `Route::match([...n], 'uri', ...)` will generate n number of Doc.
72
- $ docs = $ this ->laravelRequestDocs ->splitByMethods ($ docs );
73
- $ docs = $ this ->laravelRequestDocs ->sortDocs ($ docs , 'default ' );
74
- $ docs = $ this ->laravelRequestDocs ->groupDocs ($ docs , 'default ' );
75
-
76
- $ content = json_encode (
77
- $ this ->laravelRequestDocsToOpenApi ->openApi ($ docs ->all ())->toArray (),
78
- JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
79
- );
80
-
81
- if (!$ this ->writeFile ($ content )) {
82
- throw new \ErrorException ("Failed to write on [ {$ this ->exportFilePath }] file. " );
83
- }
84
- } catch (\Exception $ exception ) {
85
- $ this ->error ('Error : ' . $ exception ->getMessage ());
86
- return self ::FAILURE ;
48
+ if (!$ this ->confirmFilePathAvailability ()) {
49
+ //silently stop command
50
+ return self ::SUCCESS ;
51
+ }
52
+
53
+ try {
54
+ //get the excluded methods list from config
55
+ $ excludedMethods = config ('request-docs.open_api.exclude_http_methods ' , []);
56
+ $ excludedMethods = array_map (fn ($ item ) => strtolower ($ item ), $ excludedMethods );
57
+
58
+ //filter while method apis to export
59
+ $ showGet = !in_array ('get ' , $ excludedMethods );
60
+ $ showPost = !in_array ('post ' , $ excludedMethods );
61
+ $ showPut = !in_array ('put ' , $ excludedMethods );
62
+ $ showPatch = !in_array ('patch ' , $ excludedMethods );
63
+ $ showDelete = !in_array ('delete ' , $ excludedMethods );
64
+ $ showHead = !in_array ('head ' , $ excludedMethods );
65
+
66
+ // Get a list of Doc with route and rules information.
67
+ $ docs = $ this ->laravelRequestDocs ->getDocs (
68
+ $ showGet ,
69
+ $ showPost ,
70
+ $ showPut ,
71
+ $ showPatch ,
72
+ $ showDelete ,
73
+ $ showHead ,
74
+ );
75
+
76
+ // Loop and split Doc by the `methods` property.
77
+ $ docs = $ this ->laravelRequestDocs ->splitByMethods ($ docs );
78
+ $ docs = $ this ->laravelRequestDocs ->sortDocs ($ docs , 'default ' );
79
+ $ docs = $ this ->laravelRequestDocs ->groupDocs ($ docs , 'default ' );
80
+
81
+ if (!$ this ->writeApiDocsToFile ($ docs )) {
82
+ throw new ErrorException ("Failed to write on [ {$ this ->exportFilePath }] file. " );
87
83
}
84
+ } catch (Exception $ exception ) {
85
+ $ this ->error ('Error : ' . $ exception ->getMessage ());
86
+ return self ::FAILURE ;
88
87
}
89
88
90
89
return self ::SUCCESS ;
@@ -93,7 +92,7 @@ public function handle()
93
92
/**
94
93
* @return bool
95
94
*/
96
- private function confirmFilePath (): bool
95
+ private function confirmFilePathAvailability (): bool
97
96
{
98
97
$ path = $ this ->argument ('path ' );
99
98
@@ -118,17 +117,23 @@ private function confirmFilePath(): bool
118
117
}
119
118
120
119
/**
121
- * @param $content
120
+ * @param $docs
122
121
* @return false|int
123
122
*/
124
- private function writeFile ( $ content )
123
+ private function writeApiDocsToFile ( Collection $ docs ): bool
125
124
{
125
+ $ content = json_encode (
126
+ $ this ->laravelRequestDocsToOpenApi ->openApi ($ docs ->all ())->toArray (),
127
+ JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
128
+ );
129
+
126
130
$ targetDirectory = dirname ($ this ->exportFilePath );
127
131
132
+ //create parent directory if not exists
128
133
if (!is_dir ($ targetDirectory )) {
129
134
mkdir ($ targetDirectory , 0755 , true );
130
135
}
131
136
132
- return file_put_contents ($ this ->exportFilePath , $ content );
137
+ return ( bool ) file_put_contents ($ this ->exportFilePath , $ content );
133
138
}
134
139
}
0 commit comments