1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\SymfonyJsonRpcHttpServerDoc\Endpoint; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\HttpFoundation\Request; |
5
|
|
|
use Symfony\Component\HttpFoundation\Response; |
6
|
|
|
use Yoanm\SymfonyJsonRpcHttpServerDoc\Finder\NormalizedDocFinder; |
7
|
|
|
use Yoanm\SymfonyJsonRpcHttpServerDoc\Provider\RawDocProvider; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DocumentationEndpoint |
11
|
|
|
*/ |
12
|
|
|
class DocumentationEndpoint |
13
|
|
|
{ |
14
|
|
|
/** @var NormalizedDocFinder */ |
15
|
|
|
private $normalizedDocFinder; |
16
|
|
|
|
17
|
|
|
/** @var string[] */ |
18
|
|
|
private $allowedMethodList = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param NormalizedDocFinder $normalizedDocFinder |
22
|
|
|
*/ |
23
|
6 |
|
public function __construct(NormalizedDocFinder $normalizedDocFinder) |
24
|
|
|
{ |
25
|
6 |
|
$this->normalizedDocFinder = $normalizedDocFinder; |
26
|
6 |
|
$this->allowedMethodList = [Request::METHOD_GET, Request::METHOD_OPTIONS]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return Response |
31
|
|
|
*/ |
32
|
1 |
|
public function httpOptions() : Response |
33
|
|
|
{ |
34
|
1 |
|
$response = new Response(); |
35
|
1 |
|
$response->headers->set('Content-Type', 'application/json'); |
36
|
|
|
|
37
|
|
|
// Set allowed http methods |
38
|
1 |
|
$allowedMethodListString = implode(', ', $this->allowedMethodList); |
39
|
1 |
|
$response->headers->set('Allow', $allowedMethodListString); |
40
|
1 |
|
$response->headers->set('Access-Control-Request-Method', $allowedMethodListString); |
41
|
|
|
|
42
|
|
|
// Set allowed content type |
43
|
1 |
|
$response->headers->set('Accept', 'application/json'); |
44
|
1 |
|
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type'); |
45
|
|
|
|
46
|
1 |
|
return $response; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Request $request |
51
|
|
|
* |
52
|
|
|
* @return Response |
53
|
|
|
* |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
5 |
|
public function httpGet(Request $request) : Response |
57
|
|
|
{ |
58
|
|
|
// Use Raw doc by default if not provided |
59
|
5 |
|
$filename = $request->get('filename') ?? RawDocProvider::SUPPORTED_FILENAME; |
60
|
5 |
|
$response = new Response(); |
61
|
5 |
|
$response->headers->set('Content-Type', 'application/json'); |
62
|
|
|
|
63
|
5 |
|
$doc = $this->normalizedDocFinder->findFor($filename, $request->getHttpHost()); |
64
|
|
|
|
65
|
5 |
|
$response->setContent(json_encode($doc)); |
66
|
|
|
|
67
|
5 |
|
return $response; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|