Skip to content

Commit b16d753

Browse files
Use an array of API definition in a Swagger UI view (#636)
* Use an array of API definition in a Swagger UI view * Use an array of API definition in a Swagger UI view * Refactor to use response helper
1 parent 4cf2b3f commit b16d753

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

resources/views/index.blade.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset="UTF-8">
5-
<title>{{config('l5-swagger.documentations.'.$documentation.'.api.title')}}</title>
5+
<title>{{ $documentationTitle }}</title>
66
<link rel="stylesheet" type="text/css" href="{{ l5_swagger_asset($documentation, 'swagger-ui.css') }}">
77
<link rel="icon" type="image/png" href="{{ l5_swagger_asset($documentation, 'favicon-32x32.png') }}" sizes="32x32"/>
88
<link rel="icon" type="image/png" href="{{ l5_swagger_asset($documentation, 'favicon-16x16.png') }}" sizes="16x16"/>
@@ -123,10 +123,17 @@
123123
<script src="{{ l5_swagger_asset($documentation, 'swagger-ui-standalone-preset.js') }}"></script>
124124
<script>
125125
window.onload = function() {
126+
const urls = [];
127+
128+
@foreach($urlsToDocs as $title => $url)
129+
urls.push({name: "{{ $title }}", url: "{{ $url }}"});
130+
@endforeach
131+
126132
// Build a system
127133
const ui = SwaggerUIBundle({
128134
dom_id: '#swagger-ui',
129-
url: "{!! $urlToDocs !!}",
135+
urls: urls,
136+
"urls.primaryName": "{{ $documentationTitle }}",
130137
operationsSorter: {!! isset($operationsSorter) ? '"' . $operationsSorter . '"' : 'null' !!},
131138
configUrl: {!! isset($configUrl) ? '"' . $configUrl . '"' : 'null' !!},
132139
validatorUrl: {!! isset($validatorUrl) ? '"' . $validatorUrl . '"' : 'null' !!},

src/Http/Controllers/SwaggerController.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Illuminate\Routing\Controller as BaseController;
1010
use Illuminate\Support\Facades\Log;
1111
use Illuminate\Support\Facades\Request as RequestFacade;
12-
use Illuminate\Support\Facades\Response as ResponseFacade;
12+
use L5Swagger\ConfigFactory;
1313
use L5Swagger\Exceptions\L5SwaggerException;
1414
use L5Swagger\GeneratorFactory;
1515

@@ -20,9 +20,17 @@ class SwaggerController extends BaseController
2020
*/
2121
protected $generatorFactory;
2222

23-
public function __construct(GeneratorFactory $generatorFactory)
24-
{
23+
/**
24+
* @var ConfigFactory
25+
*/
26+
protected $configFactory;
27+
28+
public function __construct(
29+
GeneratorFactory $generatorFactory,
30+
ConfigFactory $configFactory
31+
) {
2532
$this->generatorFactory = $generatorFactory;
33+
$this->configFactory = $configFactory;
2634
}
2735

2836
/**
@@ -76,13 +84,13 @@ public function docs(Request $request)
7684
$content = $fileSystem->get($filePath);
7785

7886
if ($yamlFormat) {
79-
return ResponseFacade::make($content, 200, [
87+
return response($content, 200, [
8088
'Content-Type' => 'application/yaml',
8189
'Content-Disposition' => 'inline',
8290
]);
8391
}
8492

85-
return ResponseFacade::make($content, 200, [
93+
return response($content, 200, [
8694
'Content-Type' => 'application/json',
8795
]);
8896
}
@@ -97,8 +105,9 @@ public function api(Request $request)
97105
{
98106
$documentation = $request->offsetGet('documentation');
99107
$config = $request->offsetGet('config');
108+
$proxy = $config['proxy'];
100109

101-
if ($proxy = $config['proxy']) {
110+
if ($proxy) {
102111
if (! is_array($proxy)) {
103112
$proxy = [$proxy];
104113
}
@@ -113,14 +122,17 @@ public function api(Request $request)
113122
}
114123

115124
$urlToDocs = $this->generateDocumentationFileURL($documentation, $config);
125+
$urlsToDocs = $this->getAllDocumentationUrls();
116126
$useAbsolutePath = config('l5-swagger.documentations.'.$documentation.'.paths.use_absolute_path', true);
117127

118128
// Need the / at the end to avoid CORS errors on Homestead systems.
119-
return ResponseFacade::make(
129+
return response(
120130
view('l5-swagger::index', [
121131
'documentation' => $documentation,
132+
'documentationTitle' => $config['api']['title'] ?? $documentation,
122133
'secure' => RequestFacade::secure(),
123-
'urlToDocs' => $urlToDocs,
134+
'urlToDocs' => $urlToDocs, // Is not used in the view, but still passed for backwards compatibility
135+
'urlsToDocs' => $urlsToDocs,
124136
'operationsSorter' => $config['operations_sort'],
125137
'configUrl' => $config['additional_config_url'],
126138
'validatorUrl' => $config['validator_url'],
@@ -173,4 +185,23 @@ protected function generateDocumentationFileURL(string $documentation, array $co
173185
$useAbsolutePath
174186
);
175187
}
188+
189+
/**
190+
* @return array<string, string> [title => url]
191+
*/
192+
protected function getAllDocumentationUrls(): array
193+
{
194+
$documentations = array_keys(config('l5-swagger.documentations', []));
195+
196+
$urlsToDocs = [];
197+
198+
foreach ($documentations as $documentationName) {
199+
$config = $this->configFactory->documentationConfig($documentationName);
200+
$title = $config['api']['title'] ?? $documentationName;
201+
202+
$urlsToDocs[$title] = $this->generateDocumentationFileURL($documentationName, $config);
203+
}
204+
205+
return $urlsToDocs;
206+
}
176207
}

0 commit comments

Comments
 (0)