|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HarmBandstra\SwaggerUiBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
6
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
12
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
13
|
|
|
|
|
14
|
|
|
class DocsController extends Controller |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param Request $request |
|
18
|
|
|
* |
|
19
|
|
|
* @return Response |
|
20
|
|
|
*/ |
|
21
|
1 |
|
public function indexAction(Request $request) |
|
22
|
|
|
{ |
|
23
|
1 |
|
if (!$request->get('url')) { |
|
24
|
|
|
// if there is no ?url=... parameter, redirect to the default one |
|
25
|
1 |
|
$specFiles = $this->getParameter('hb_swagger_ui.files'); |
|
26
|
|
|
|
|
27
|
1 |
|
$defaultSpecFile = reset($specFiles); |
|
28
|
|
|
|
|
29
|
1 |
|
return $this->redirect($this->getRedirectUrlToSpec($defaultSpecFile)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$indexFilePath = $this->getParameter('kernel.root_dir') . '/../vendor/swagger-api/swagger-ui/dist/index.html'; |
|
33
|
|
|
|
|
34
|
|
|
return new Response(file_get_contents($indexFilePath)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Request $request |
|
39
|
|
|
* @param string $fileName |
|
40
|
|
|
* |
|
41
|
|
|
* @return RedirectResponse |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function redirectAction(Request $request, $fileName) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
1 |
|
$validFiles = $this->getParameter('hb_swagger_ui.files'); |
|
46
|
|
|
|
|
47
|
|
|
// redirect to swagger file if that's what we're looking for |
|
48
|
1 |
|
if (in_array($fileName, $validFiles, true)) { |
|
49
|
1 |
|
return $this->redirect($this->getRedirectUrlToSpec($fileName)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// redirect to the assets dir so that relative links work |
|
53
|
|
|
return $this->redirect('/bundles/hbswaggerui/' . $fileName); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $fileName |
|
58
|
|
|
* |
|
59
|
|
|
* @return JsonResponse |
|
60
|
|
|
*/ |
|
61
|
4 |
|
public function swaggerFileAction($fileName) |
|
62
|
|
|
{ |
|
63
|
|
|
try { |
|
64
|
4 |
|
$filePath = $this->getFilePath($fileName); |
|
65
|
4 |
|
} catch (\Exception $e) { |
|
66
|
2 |
|
return new JsonResponse($e->getMessage(), Response::HTTP_NOT_FOUND); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); |
|
70
|
2 |
|
if ($extension === 'yml' || $extension === 'yaml') { |
|
71
|
1 |
|
$fileContents = Yaml::parse(file_get_contents($filePath)); |
|
72
|
|
|
|
|
73
|
1 |
|
return new JsonResponse($fileContents); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
$fileContents = file_get_contents($filePath); |
|
77
|
|
|
|
|
78
|
1 |
|
return new JsonResponse($fileContents, Response::HTTP_OK, [], true); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $fileName |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
4 |
|
private function getFilePath($fileName = '') |
|
87
|
|
|
{ |
|
88
|
4 |
|
$validFiles = $this->getParameter('hb_swagger_ui.files'); |
|
89
|
|
|
|
|
90
|
4 |
|
if ($fileName !== '' && !in_array($fileName, $validFiles)) { |
|
91
|
1 |
|
throw new \RuntimeException( |
|
92
|
1 |
|
sprintf('File [%s] not defined under [hb_swagger_ui.files] in config.yml.', $fileName) |
|
93
|
1 |
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
3 |
|
$directory = $this->getParameter('hb_swagger_ui.directory'); |
|
97
|
|
|
|
|
98
|
3 |
|
if ($directory === '') { |
|
99
|
|
|
throw new \RuntimeException( |
|
100
|
|
|
'Directory [hb_swagger_ui.directory] not defined or empty in config.yml.' |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
$filePath = realpath($this->getParameter('hb_swagger_ui.directory') . DIRECTORY_SEPARATOR . $fileName); |
|
105
|
3 |
|
if (!is_file($filePath)) { |
|
106
|
1 |
|
throw new FileNotFoundException(sprintf('File [%s] not found.', $fileName)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
return $filePath; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $fileName |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
2 |
|
private function getRedirectUrlToSpec($fileName) |
|
118
|
|
|
{ |
|
119
|
2 |
|
if (strpos($fileName, '/') === 0 || preg_match('#http[s]?://#', $fileName)) { |
|
120
|
|
|
// if absolute path or URL use it raw |
|
121
|
|
|
$specUrl = $fileName; |
|
122
|
|
|
} else { |
|
123
|
2 |
|
$specUrl = $this->generateUrl( |
|
124
|
2 |
|
'hb_swagger_ui_swagger_file', |
|
125
|
2 |
|
['fileName' => $fileName], |
|
126
|
|
|
UrlGeneratorInterface::ABSOLUTE_PATH |
|
127
|
2 |
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
2 |
|
return $this->generateUrl('hb_swagger_ui_default', ['url' => $specUrl]); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.