|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Writing; |
|
4
|
|
|
|
|
5
|
|
|
use Ramsey\Uuid\Uuid; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Illuminate\Support\Facades\URL; |
|
9
|
|
|
|
|
10
|
|
|
class PostmanCollectionWriter |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Collection |
|
14
|
|
|
*/ |
|
15
|
|
|
private $routeGroups; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $baseUrl; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* CollectionWriter constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Collection $routeGroups |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct(Collection $routeGroups, $baseUrl) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->routeGroups = $routeGroups; |
|
30
|
|
|
$this->baseUrl = $baseUrl; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function getCollection() |
|
34
|
|
|
{ |
|
35
|
|
|
URL::forceRootUrl($this->baseUrl); |
|
36
|
|
|
if (Str::startsWith($this->baseUrl, 'https://')) { |
|
37
|
|
|
URL::forceScheme('https'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$collection = [ |
|
41
|
|
|
'variables' => [], |
|
42
|
|
|
'info' => [ |
|
43
|
|
|
'name' => config('apidoc.postman.name') ?: config('app.name').' API', |
|
44
|
|
|
'_postman_id' => Uuid::uuid4()->toString(), |
|
45
|
|
|
'description' => config('apidoc.postman.description') ?: '', |
|
46
|
|
|
'schema' => 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json', |
|
47
|
|
|
], |
|
48
|
|
|
'item' => $this->routeGroups->map(function ($routes, $groupName) { |
|
49
|
|
|
return [ |
|
50
|
|
|
'name' => $groupName, |
|
51
|
|
|
'description' => '', |
|
52
|
|
|
'item' => $routes->map(function ($route) { |
|
53
|
|
|
$mode = $route['methods'][0] === 'PUT' ? 'urlencoded' : 'formdata'; |
|
54
|
|
|
|
|
55
|
|
|
return [ |
|
56
|
|
|
'name' => $route['metadata']['title'] != '' ? $route['metadata']['title'] : url($route['uri']), |
|
57
|
|
|
'request' => [ |
|
58
|
|
|
'url' => url($route['uri']).(collect($route['queryParameters'])->isEmpty() |
|
59
|
|
|
? '' |
|
60
|
|
|
: ('?'.implode('&', collect($route['queryParameters'])->map(function ($parameter, $key) { |
|
61
|
|
|
return urlencode($key).'='.urlencode($parameter['value'] ?? ''); |
|
62
|
|
|
})->all()))), |
|
63
|
|
|
'method' => $route['methods'][0], |
|
64
|
|
|
'header' => collect($route['headers']) |
|
65
|
|
|
->union([ |
|
66
|
|
|
'Accept' => 'application/json', |
|
67
|
|
|
]) |
|
68
|
|
|
->map(function ($value, $header) { |
|
69
|
|
|
return [ |
|
70
|
|
|
'key' => $header, |
|
71
|
|
|
'value' => $value, |
|
72
|
|
|
]; |
|
73
|
|
|
}) |
|
74
|
|
|
->values()->all(), |
|
75
|
|
|
'body' => [ |
|
76
|
|
|
'mode' => $mode, |
|
77
|
|
|
$mode => collect($route['bodyParameters'])->map(function ($parameter, $key) { |
|
78
|
|
|
return [ |
|
79
|
|
|
'key' => $key, |
|
80
|
|
|
'value' => $parameter['value'] ?? '', |
|
81
|
|
|
'type' => 'text', |
|
82
|
|
|
'enabled' => true, |
|
83
|
|
|
]; |
|
84
|
|
|
})->values()->toArray(), |
|
85
|
|
|
], |
|
86
|
|
|
'description' => $route['metadata']['description'], |
|
87
|
|
|
'response' => [], |
|
88
|
|
|
], |
|
89
|
|
|
]; |
|
90
|
|
|
})->toArray(), |
|
91
|
|
|
]; |
|
92
|
|
|
})->values()->toArray(), |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
return json_encode($collection); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|