1 | <?php |
||
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 | /** @var string */ |
||
29 | private $configFile; |
||
30 | |||
31 | 6 | public function __construct($projectDir, $swaggerFiles, $directory, $assetUrlPath, $configFile) |
|
32 | { |
||
33 | 6 | $this->projectDir = $projectDir; |
|
34 | 6 | $this->swaggerFiles = $swaggerFiles; |
|
35 | 6 | $this->directory = $directory; |
|
36 | 6 | $this->assetUrlPath = $assetUrlPath; |
|
37 | 6 | $this->configFile = $configFile; |
|
38 | 6 | } |
|
39 | |||
40 | /** |
||
41 | * @param Request $request |
||
42 | * |
||
43 | * @return Response |
||
44 | */ |
||
45 | 1 | public function indexAction(Request $request) |
|
46 | { |
||
47 | 1 | if (!$request->get('url') || (!$request->get('configUrl') && $this->configFile)) { |
|
48 | // if there is no ?url=... parameter, redirect to the default one |
||
49 | 1 | $specFile = reset($this->swaggerFiles); |
|
50 | |||
51 | 1 | return $this->redirect($this->getRedirectUrlToSpec($specFile, $request->get('url'))); |
|
52 | } |
||
53 | $contents = @file_get_contents(__DIR__ . '/../Resources/public/index.html'); |
||
54 | if ($contents === false) { |
||
55 | throw new \RuntimeException('Unable to load [Resources/public/index.html]. Did [ScriptHandler::linkAssets] run correctly?'); |
||
56 | } |
||
57 | |||
58 | return new Response($contents); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param string $fileName |
||
63 | * |
||
64 | * @return RedirectResponse |
||
65 | */ |
||
66 | 1 | public function redirectAction($fileName) |
|
67 | { |
||
68 | // redirect to swagger file if that's what we're looking for |
||
69 | 1 | if (in_array($fileName, $this->swaggerFiles, true)) { |
|
70 | 1 | return $this->redirect($this->getRedirectUrlToSpec($fileName)); |
|
71 | } |
||
72 | |||
73 | // redirect to the assets dir so that relative links work |
||
74 | return $this->redirect($this->assetUrlPath . $fileName); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param string $fileName |
||
79 | * |
||
80 | * @return JsonResponse|Response |
||
81 | */ |
||
82 | 4 | public function swaggerFileAction($fileName) |
|
105 | |||
106 | /** |
||
107 | * @param string $fileName |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | 4 | private function getFilePath($fileName = '') |
|
132 | |||
133 | /** |
||
134 | * @param string $fileName |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | 2 | private function getRedirectUrlToSpec($fileName, $url = null) |
|
163 | } |
||
164 |