22
33namespace Rakutentech \LaravelRequestDocs \Commands ;
44
5+ use ErrorException ;
6+ use Exception ;
57use Illuminate \Console \Command ;
8+ use Illuminate \Support \Collection ;
69use Rakutentech \LaravelRequestDocs \LaravelRequestDocs ;
710use Rakutentech \LaravelRequestDocs \LaravelRequestDocsToOpenApi ;
811
@@ -15,7 +18,7 @@ public function __construct(LaravelRequestDocs $laravelRequestDoc, LaravelReques
1518 {
1619 parent ::__construct ();
1720
18- $ this ->laravelRequestDocs = $ laravelRequestDoc ;
21+ $ this ->laravelRequestDocs = $ laravelRequestDoc ;
1922 $ this ->laravelRequestDocsToOpenApi = $ laravelRequestDocsToOpenApi ;
2023 }
2124
@@ -42,49 +45,45 @@ public function __construct(LaravelRequestDocs $laravelRequestDoc, LaravelReques
4245 */
4346 public function handle ()
4447 {
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. " );
8783 }
84+ } catch (Exception $ exception ) {
85+ $ this ->error ('Error : ' . $ exception ->getMessage ());
86+ return self ::FAILURE ;
8887 }
8988
9089 return self ::SUCCESS ;
@@ -93,7 +92,7 @@ public function handle()
9392 /**
9493 * @return bool
9594 */
96- private function confirmFilePath (): bool
95+ private function confirmFilePathAvailability (): bool
9796 {
9897 $ path = $ this ->argument ('path ' );
9998
@@ -118,17 +117,23 @@ private function confirmFilePath(): bool
118117 }
119118
120119 /**
121- * @param $content
120+ * @param $docs
122121 * @return false|int
123122 */
124- private function writeFile ( $ content )
123+ private function writeApiDocsToFile ( Collection $ docs ): bool
125124 {
125+ $ content = json_encode (
126+ $ this ->laravelRequestDocsToOpenApi ->openApi ($ docs ->all ())->toArray (),
127+ JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
128+ );
129+
126130 $ targetDirectory = dirname ($ this ->exportFilePath );
127131
132+ //create parent directory if not exists
128133 if (!is_dir ($ targetDirectory )) {
129134 mkdir ($ targetDirectory , 0755 , true );
130135 }
131136
132- return file_put_contents ($ this ->exportFilePath , $ content );
137+ return ( bool ) file_put_contents ($ this ->exportFilePath , $ content );
133138 }
134139}
0 commit comments