-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathSwaggerLumeController.php
More file actions
104 lines (90 loc) · 3.01 KB
/
Copy pathSwaggerLumeController.php
File metadata and controls
104 lines (90 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace SwaggerLume\Http\Controllers;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Request as RequestFacade;
use Laravel\Lumen\Routing\Controller as BaseController;
use SwaggerLume\Generator;
class SwaggerLumeController extends BaseController
{
/**
* Dump api-docs.json content endpoint.
*
* @param null $docsFile
* @return \Illuminate\Http\Response
*/
public function docs()
{
$filePath = sprintf(
'%s/%s',
config('swagger-lume.paths.docs'),
config('swagger-lume.paths.format_to_use_for_docs') === 'json' ? config('swagger-lume.paths.docs_json') : config('swagger-lume.paths.docs_yaml')
);
$yaml = false;
$parts = explode('.', $filePath);
if (! empty($parts)) {
$extension = array_pop($parts);
$yaml = strtolower($extension) === 'yaml';
}
if (config('swagger-lume.generate_always') && ! File::exists($filePath)) {
try {
Generator::generateDocs();
} catch (\Exception $e) {
Log::error($e);
abort(
404,
sprintf(
'Unable to generate documentation file to: "%s". Please make sure directory is writable. Error: %s',
$filePath,
$e->getMessage()
)
);
}
}
if (! File::exists($filePath)) {
abort(404, 'Cannot find '.$filePath);
}
$content = File::get($filePath);
if ($yaml) {
return Response($content, 200, [
'Content-Type' => 'application/yaml',
'Content-Disposition' => 'inline',
'filename' => config('swagger-lume.paths.docs_yaml'),
]);
}
return new Response($content, 200, ['Content-Type' => 'application/json']);
}
/**
* Display Swagger API page.
*
* @return \Illuminate\Http\Response
*/
public function api()
{
if (config('swagger-lume.generate_always')) {
Generator::generateDocs();
}
//need the / at the end to avoid CORS errors on Homestead systems.
return new Response(
view('swagger-lume::index', [
'secure' => RequestFacade::secure(),
'urlToDocs' => route('swagger-lume.docs', [], null),
'operationsSorter' => config('swagger-lume.operations_sort'),
'configUrl' => config('swagger-lume.additional_config_url'),
'validatorUrl' => config('swagger-lume.validator_url'),
]),
200,
['Content-Type' => 'text/html']
);
}
/**
* Display Oauth2 callback pages.
*
* @return string
*/
public function oauth2Callback()
{
return File::get(swagger_ui_dist_path('oauth2-redirect.html'));
}
}