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($request, $defaultSpecFile)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
try { |
33
|
|
|
// check if public/index.html exists and get its path if it does |
34
|
|
|
$indexFilePath = $this->get('file_locator')->locate('@HBSwaggerUiBundle/Resources/public/index.html'); |
35
|
|
|
} catch (\InvalidArgumentException $exception) { |
36
|
|
|
// index.html doesn't exist, let's update public/ with swagger-ui files |
37
|
|
|
$publicDir = $this->get('file_locator')->locate('@HBSwaggerUiBundle/Resources/public/'); |
38
|
|
|
|
39
|
|
|
$swaggerDistDir = $this->getParameter('kernel.root_dir').'/../vendor/swagger-api/swagger-ui/dist'; |
40
|
|
|
|
41
|
|
|
// update public dir |
42
|
|
|
$this->get('filesystem')->mirror($swaggerDistDir, $publicDir); |
43
|
|
|
|
44
|
|
|
// the public/index.html file should exists now, let's try to get its path again |
45
|
|
|
$indexFilePath = $this->get('file_locator')->locate('@HBSwaggerUiBundle/Resources/public/index.html'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return new Response(file_get_contents($indexFilePath)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Request $request |
53
|
|
|
* @param string $fileName |
54
|
|
|
* |
55
|
|
|
* @return RedirectResponse |
56
|
|
|
*/ |
57
|
1 |
|
public function redirectAction(Request $request, $fileName) |
58
|
|
|
{ |
59
|
1 |
|
$validFiles = $this->getParameter('hb_swagger_ui.files'); |
60
|
|
|
|
61
|
|
|
// redirect to swagger file if that's what we're looking for |
62
|
1 |
|
if (in_array($fileName, $validFiles, true)) { |
63
|
1 |
|
return $this->redirect($this->getRedirectUrlToSpec($request, $fileName)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// redirect to the assets dir so that relative links work |
67
|
|
|
return $this->redirect('/bundles/hbswaggerui/'.$fileName); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $fileName |
72
|
|
|
* |
73
|
|
|
* @return JsonResponse |
74
|
|
|
*/ |
75
|
4 |
|
public function swaggerFileAction($fileName) |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
4 |
|
$filePath = $this->getFilePath($fileName); |
79
|
4 |
|
} catch (\Exception $e) { |
80
|
2 |
|
return new JsonResponse($e->getMessage(), Response::HTTP_NOT_FOUND); |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); |
84
|
2 |
|
if ($extension === 'yml' || $extension === 'yaml') { |
85
|
1 |
|
$fileContents = Yaml::parse(file_get_contents($filePath)); |
86
|
|
|
|
87
|
1 |
|
return new JsonResponse($fileContents); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$fileContents = file_get_contents($filePath); |
91
|
|
|
|
92
|
1 |
|
return new JsonResponse($fileContents, Response::HTTP_OK, [], true); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $fileName |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
4 |
|
private function getFilePath($fileName = '') |
101
|
|
|
{ |
102
|
4 |
|
$validFiles = $this->getParameter('hb_swagger_ui.files'); |
103
|
|
|
|
104
|
4 |
|
if ($fileName !== '' && !in_array($fileName, $validFiles)) { |
105
|
1 |
|
throw new \RuntimeException( |
106
|
1 |
|
sprintf('File [%s] not defined under [hb_swagger_ui.files] in config.yml.', $fileName) |
107
|
1 |
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
$directory = $this->getParameter('hb_swagger_ui.directory'); |
111
|
|
|
|
112
|
3 |
|
if ($directory === '') { |
113
|
|
|
throw new \RuntimeException( |
114
|
|
|
'Directory [hb_swagger_ui.directory] not defined or empty in config.yml.' |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
3 |
|
$filePath = realpath($this->getParameter('hb_swagger_ui.directory') . DIRECTORY_SEPARATOR . $fileName); |
119
|
3 |
|
if (!is_file($filePath)) { |
120
|
1 |
|
throw new FileNotFoundException(sprintf('File [%s] not found.', $fileName)); |
121
|
|
|
} |
122
|
|
|
|
123
|
2 |
|
return $filePath; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param Request $request |
128
|
|
|
* @param $fileName |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
2 |
|
private function getRedirectUrlToSpec(Request $request, $fileName) { |
|
|
|
|
133
|
2 |
|
if (strpos($fileName, '/') === 0 || preg_match('#http[s]?://#', $fileName)) { |
134
|
|
|
// if absolute path or URL use it raw |
135
|
|
|
$specUrl = $fileName; |
136
|
|
|
} else { |
137
|
2 |
|
$specUrl = $this->generateUrl('hb_swagger_ui_swagger_file', ['fileName' => $fileName], UrlGeneratorInterface::ABSOLUTE_PATH); |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
return $this->generateUrl('hb_swagger_ui_default', ['url' => $specUrl]); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.