1
1
<?php
2
2
3
- namespace AndreasElia \PostmanGenerator ;
3
+ namespace AndreasElia \PostmanGenerator \ Commands ;
4
4
5
5
use Closure ;
6
6
use Illuminate \Console \Command ;
7
7
use Illuminate \Contracts \Config \Repository ;
8
8
use Illuminate \Foundation \Http \FormRequest ;
9
9
use Illuminate \Routing \Router ;
10
10
use Illuminate \Support \Facades \Storage ;
11
+ use Illuminate \Support \Str ;
11
12
use ReflectionClass ;
12
13
use ReflectionFunction ;
13
14
14
- class ExportPostman extends Command
15
+ class ExportPostmanCommand extends Command
15
16
{
16
17
/** @var string */
17
18
protected $ signature = 'export:postman {--bearer= : The bearer token to use on your endpoints} ' ;
@@ -28,34 +29,35 @@ class ExportPostman extends Command
28
29
/** @var array */
29
30
protected $ config ;
30
31
32
+ /** @var null */
33
+ protected $ filename ;
34
+
31
35
public function __construct (Router $ router , Repository $ config )
32
36
{
33
37
parent ::__construct ();
34
38
35
39
$ this ->router = $ router ;
36
40
$ this ->config = $ config ['api-postman ' ];
41
+ $ this ->filename = $ this ->formatFilename ();
37
42
}
38
43
39
44
public function handle (): void
40
45
{
41
- $ bearer = $ this ->option ('bearer ' ) ?? false ;
42
-
43
- $ filename = date ('Y_m_d_His ' ).'_postman ' ;
44
-
45
- $ this ->initStructure ($ filename );
46
+ $ this ->initStructure ();
46
47
47
- if ($ bearer ) {
48
+ if ($ bearer = $ this -> option ( ' bearer ' ) ?? false ) {
48
49
$ this ->structure ['variable ' ][] = [
49
50
'key ' => 'token ' ,
50
51
'value ' => $ bearer ,
51
52
];
52
53
}
53
54
54
55
foreach ($ this ->router ->getRoutes () as $ route ) {
55
- $ middleware = $ route ->middleware ();
56
+ $ methods = collect ($ route ->methods ())->reject (fn ($ method ) => $ method == 'HEAD ' );
57
+ $ middleware = $ route ->gatherMiddleware ();
56
58
57
- foreach ($ route -> methods as $ method ) {
58
- if ($ method == ' HEAD ' || empty ($ middleware ) || $ middleware [ 0 ] !== 'api ' ) {
59
+ foreach ($ methods as $ method ) {
60
+ if (empty ($ middleware ) || ! in_array ( 'api ' , $ middleware ) ) {
59
61
continue ;
60
62
}
61
63
@@ -95,9 +97,9 @@ public function handle(): void
95
97
];
96
98
}
97
99
98
- $ request = $ this ->makeItem ($ route , $ method , $ routeHeaders , $ requestRules );
100
+ $ request = $ this ->makeRequest ($ route , $ method , $ routeHeaders , $ requestRules );
99
101
100
- if ($ this ->config [ ' structured ' ] ) {
102
+ if ($ this ->isStructured () ) {
101
103
$ routeNames = $ route ->action ['as ' ] ?? null ;
102
104
103
105
if (! $ routeNames ) {
@@ -114,23 +116,22 @@ public function handle(): void
114
116
return ! is_null ($ value ) && $ value !== '' ;
115
117
});
116
118
117
- $ destination = end ($ routeNames );
118
-
119
- $ this ->ensurePath ($ this ->structure , $ routeNames , $ request , $ destination );
119
+ $ this ->buildTree ($ this ->structure , $ routeNames , $ request );
120
120
} else {
121
121
$ this ->structure ['item ' ][] = $ request ;
122
122
}
123
123
}
124
124
}
125
125
126
- Storage::put ($ exportName = "$ filename.json " , json_encode ($ this ->structure ));
126
+ Storage::put ($ exportName = "postman/ $ this -> filename " , json_encode ($ this ->structure ));
127
127
128
128
$ this ->info ("Postman Collection Exported: $ exportName " );
129
129
}
130
130
131
- protected function ensurePath (array &$ routes , array $ segments , array $ request, string $ destination ): void
131
+ protected function buildTree (array &$ routes , array $ segments , array $ request ): void
132
132
{
133
133
$ parent = &$ routes ;
134
+ $ destination = end ($ segments );
134
135
135
136
foreach ($ segments as $ segment ) {
136
137
$ matched = false ;
@@ -165,7 +166,7 @@ protected function ensurePath(array &$routes, array $segments, array $request, s
165
166
}
166
167
}
167
168
168
- public function makeItem ($ route , $ method , $ routeHeaders , $ requestRules )
169
+ public function makeRequest ($ route , $ method , $ routeHeaders , $ requestRules )
169
170
{
170
171
$ data = [
171
172
'name ' => $ route ->uri (),
@@ -199,7 +200,7 @@ public function makeItem($route, $method, $routeHeaders, $requestRules)
199
200
return $ data ;
200
201
}
201
202
202
- protected function initStructure (string $ filename ): void
203
+ protected function initStructure (): void
203
204
{
204
205
$ this ->structure = [
205
206
'variable ' => [
@@ -209,10 +210,24 @@ protected function initStructure(string $filename): void
209
210
],
210
211
],
211
212
'info ' => [
212
- 'name ' => $ filename ,
213
+ 'name ' => $ this -> filename ,
213
214
'schema ' => 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json ' ,
214
215
],
215
216
'item ' => [],
216
217
];
217
218
}
219
+
220
+ protected function formatFilename ()
221
+ {
222
+ return str_replace (
223
+ ['{timestamp} ' , '{app} ' ],
224
+ [date ('Y_m_d_His ' ), Str::snake (config ('app.name ' ))],
225
+ $ this ->config ['filename ' ]
226
+ );
227
+ }
228
+
229
+ protected function isStructured ()
230
+ {
231
+ return $ this ->config ['structured ' ];
232
+ }
218
233
}
0 commit comments