1 | <?php |
||
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 | 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($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) |
|
94 | |||
95 | /** |
||
96 | * @param string $fileName |
||
97 | * |
||
98 | * @return string |
||
99 | */ |
||
100 | 4 | private function getFilePath($fileName = '') |
|
125 | |||
126 | /** |
||
127 | * @param string $fileName |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | 2 | private function getRedirectUrlToSpec($fileName) |
|
146 | } |
||
147 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.