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
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class DocumentationEndpoint |
10
|
|
|
*/ |
11
|
|
|
class DocumentationEndpoint |
12
|
|
|
{ |
13
|
|
|
/** @var NormalizedDocFinder */ |
14
|
|
|
private $normalizedDocFinder; |
15
|
|
|
|
16
|
|
|
/** @var string[] */ |
17
|
|
|
private $allowedMethodList = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param NormalizedDocFinder $normalizedDocFinder |
21
|
|
|
*/ |
22
|
2 |
|
public function __construct(NormalizedDocFinder $normalizedDocFinder) |
23
|
|
|
{ |
24
|
2 |
|
$this->normalizedDocFinder = $normalizedDocFinder; |
25
|
2 |
|
$this->allowedMethodList = [Request::METHOD_GET, Request::METHOD_OPTIONS]; |
26
|
2 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Request $request |
30
|
|
|
* |
31
|
|
|
* @return Response |
32
|
|
|
*/ |
33
|
1 |
|
public function httpOptions(Request $request) : Response |
|
|
|
|
34
|
|
|
{ |
35
|
1 |
|
$response = new Response(); |
36
|
1 |
|
$response->headers->set('Content-Type', 'application/json'); |
37
|
|
|
|
38
|
|
|
// Set allowed http methods |
39
|
1 |
|
$response->headers->set('Allow', $this->allowedMethodList); |
40
|
1 |
|
$response->headers->set('Access-Control-Request-Method', $this->allowedMethodList); |
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
|
1 |
|
public function httpGet(Request $request) : Response |
55
|
|
|
{ |
56
|
1 |
|
$filename = $request->get('filename'); |
57
|
1 |
|
$response = new Response(); |
58
|
1 |
|
$response->headers->set('Content-Type', 'application/json'); |
59
|
|
|
|
60
|
1 |
|
$doc = $this->normalizedDocFinder->findFor($filename, $request->getHttpHost()); |
|
|
|
|
61
|
|
|
|
62
|
1 |
|
$response->setContent(json_encode($doc)); |
63
|
|
|
|
64
|
1 |
|
return $response; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.