9
9
use Illuminate \Contracts \Validation \ValidationRule ;
10
10
use Illuminate \Foundation \Http \FormRequest ;
11
11
use Illuminate \Routing \Router ;
12
+ use Illuminate \Support \Facades \Log ;
12
13
use Illuminate \Support \Facades \Storage ;
13
14
use Illuminate \Support \Facades \Validator ;
14
15
use Illuminate \Support \Str ;
@@ -27,7 +28,8 @@ class ExportPostmanCommand extends Command
27
28
/** @var string */
28
29
protected $ signature = 'export:postman
29
30
{--bearer= : The bearer token to use on your endpoints}
30
- {--basic= : The basic auth to use on your endpoints} ' ;
31
+ {--basic= : The basic auth to use on your endpoints}
32
+ {--continue-on-errors : Continues even if an error occurs in any route, middleware or method (boolean)} ' ;
31
33
32
34
/** @var string */
33
35
protected $ description = 'Automatically generate a Postman collection for your API routes ' ;
@@ -62,6 +64,9 @@ class ExportPostmanCommand extends Command
62
64
'basic ' ,
63
65
];
64
66
67
+ /** @var bool */
68
+ private $ stopOnErrors = true ;
69
+
65
70
/** @var \Illuminate\Validation\Validator */
66
71
private $ validator ;
67
72
@@ -77,12 +82,14 @@ public function handle(): void
77
82
{
78
83
$ this ->setFilename ();
79
84
$ this ->setAuthToken ();
85
+ $ this ->setOptions ();
80
86
$ this ->initializeStructure ();
81
87
$ this ->initializePhpDocParser ();
82
88
83
89
foreach ($ this ->router ->getRoutes () as $ route ) {
84
90
$ methods = array_filter ($ route ->methods (), fn ($ value ) => $ value !== 'HEAD ' );
85
- $ middlewares = $ route ->gatherMiddleware ();
91
+
92
+ $ middlewares = $ this ->handleCallable (fn () => $ route ->gatherMiddleware ());
86
93
87
94
foreach ($ methods as $ method ) {
88
95
$ includedMiddleware = false ;
@@ -103,7 +110,7 @@ public function handle(): void
103
110
104
111
$ routeAction = $ route ->getAction ();
105
112
106
- $ reflectionMethod = $ this ->getReflectionMethod ($ routeAction );
113
+ $ reflectionMethod = $ this ->handleCallable ( fn () => $ this -> getReflectionMethod ($ routeAction) );
107
114
108
115
if (! $ reflectionMethod ) {
109
116
continue ;
@@ -223,6 +230,18 @@ public function handle(): void
223
230
$ this ->info ('Postman Collection Exported: ' .storage_path ('app/ ' .$ exportName ));
224
231
}
225
232
233
+ protected function handleCallable ($ callable )
234
+ {
235
+ try {
236
+ return $ callable ();
237
+ } catch (\Throwable $ th ) {
238
+ if ($ this ->stopOnErrors ) {
239
+ throw $ th ;
240
+ }
241
+ Log::error ($ th ->getMessage ()."\n[stacktrace] \n" .$ th ->getTraceAsString ()."\n" );
242
+ }
243
+ }
244
+
226
245
protected function getReflectionMethod (array $ routeAction ): ?object
227
246
{
228
247
// Hydrates the closure if it is an instance of Opis\Closure\SerializableClosure
@@ -489,6 +508,13 @@ protected function setAuthToken()
489
508
}
490
509
}
491
510
511
+ protected function setOptions ()
512
+ {
513
+ if ($ this ->option ('continue-on-errors ' ) === true ) {
514
+ $ this ->stopOnErrors = false ;
515
+ }
516
+ }
517
+
492
518
protected function isStructured ()
493
519
{
494
520
return $ this ->config ['structured ' ];
0 commit comments