1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HarmBandstra\SwaggerUiBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
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 AbstractController |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
private $projectDir; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
private $swaggerFiles; |
21
|
|
|
|
22
|
|
|
/** @var string */ |
23
|
|
|
private $directory; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
private $assetUrlPath; |
27
|
|
|
|
28
|
6 |
|
public function __construct($projectDir, $swaggerFiles, $directory, $assetUrlPath) |
29
|
|
|
{ |
30
|
6 |
|
$this->projectDir = $projectDir; |
31
|
6 |
|
$this->swaggerFiles = $swaggerFiles; |
32
|
6 |
|
$this->directory = $directory; |
33
|
6 |
|
$this->assetUrlPath = $assetUrlPath; |
34
|
6 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Request $request |
38
|
|
|
* |
39
|
|
|
* @return Response |
40
|
|
|
*/ |
41
|
1 |
|
public function indexAction(Request $request) |
42
|
|
|
{ |
43
|
1 |
|
if (!$request->get('url')) { |
44
|
|
|
// if there is no ?url=... parameter, redirect to the default one |
45
|
1 |
|
$defaultSpecFile = reset($this->swaggerFiles); |
46
|
|
|
|
47
|
1 |
|
return $this->redirect($this->getRedirectUrlToSpec($defaultSpecFile)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$contents = @file_get_contents(__DIR__ . '/../Resources/public/index.html'); |
51
|
|
|
if ($contents === false) { |
52
|
|
|
throw new \RuntimeException('Unable to load [Resources/public/index.html]. Did [ScriptHandler::linkAssets] run correctly?'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return new Response($contents); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $fileName |
60
|
|
|
* |
61
|
|
|
* @return RedirectResponse |
62
|
|
|
*/ |
63
|
1 |
|
public function redirectAction($fileName) |
64
|
|
|
{ |
65
|
|
|
// redirect to swagger file if that's what we're looking for |
66
|
1 |
|
if (in_array($fileName, $this->swaggerFiles, true)) { |
67
|
1 |
|
return $this->redirect($this->getRedirectUrlToSpec($fileName)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// redirect to the assets dir so that relative links work |
71
|
|
|
return $this->redirect($this->assetUrlPath . $fileName); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $fileName |
76
|
|
|
* |
77
|
|
|
* @return JsonResponse|Response |
78
|
|
|
*/ |
79
|
4 |
|
public function swaggerFileAction($fileName) |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
4 |
|
$filePath = $this->getFilePath($fileName); |
83
|
2 |
|
} catch (\Exception $e) { |
84
|
2 |
|
return new JsonResponse($e->getMessage(), Response::HTTP_NOT_FOUND); |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); |
88
|
2 |
|
if ($extension === 'yml' || $extension === 'yaml') { |
89
|
1 |
|
$fileContents = Yaml::parse(file_get_contents($filePath)); |
90
|
|
|
|
91
|
1 |
|
return new JsonResponse($fileContents); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$fileContents = file_get_contents($filePath); |
95
|
|
|
|
96
|
1 |
|
return new Response( |
97
|
1 |
|
$fileContents, |
98
|
1 |
|
Response::HTTP_OK, |
99
|
1 |
|
['Content-Type' => 'application/json'] |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $fileName |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
4 |
|
private function getFilePath($fileName = '') |
109
|
|
|
{ |
110
|
4 |
|
if ($fileName !== '' && !in_array($fileName, $this->swaggerFiles)) { |
111
|
1 |
|
throw new \RuntimeException( |
112
|
1 |
|
sprintf('File [%s] not defined under [hb_swagger_ui.files] in config.yml.', $fileName) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
if ($this->directory === '') { |
117
|
|
|
throw new \RuntimeException( |
118
|
|
|
'Directory [hb_swagger_ui.directory] not defined or empty in config.yml.' |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
3 |
|
$filePath = realpath($this->directory . DIRECTORY_SEPARATOR . $fileName); |
123
|
3 |
|
if (!is_file($filePath)) { |
124
|
1 |
|
throw new FileNotFoundException(sprintf('File [%s] not found.', $fileName)); |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
return $filePath; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $fileName |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
2 |
|
private function getRedirectUrlToSpec($fileName) |
136
|
|
|
{ |
137
|
2 |
|
if (strpos($fileName, '/') === 0 || preg_match('#http[s]?://#', $fileName)) { |
138
|
|
|
// if absolute path or URL use it raw |
139
|
|
|
$specUrl = $fileName; |
140
|
|
|
} else { |
141
|
2 |
|
$specUrl = $this->generateUrl( |
142
|
2 |
|
'hb_swagger_ui_swagger_file', |
143
|
2 |
|
['fileName' => $fileName], |
144
|
2 |
|
UrlGeneratorInterface::ABSOLUTE_PATH |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
return $this->generateUrl('hb_swagger_ui_default', ['url' => $specUrl]); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|