|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\SymfonyJsonRpcHttpServer\Endpoint; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
5
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
6
|
|
|
use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint as SDKJsonRpcEndpoint; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class JsonRpcHttpEndpoint |
|
10
|
|
|
*/ |
|
11
|
|
|
class JsonRpcHttpEndpoint |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var SdkJsonRpcEndpoint */ |
|
14
|
|
|
private $sdkEndpoint; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string[] */ |
|
17
|
|
|
private $allowedMethodList = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param SDKJsonRpcEndpoint $sdkEndpoint |
|
21
|
|
|
*/ |
|
22
|
3 |
|
public function __construct(SDKJsonRpcEndpoint $sdkEndpoint) |
|
23
|
|
|
{ |
|
24
|
3 |
|
$this->sdkEndpoint = $sdkEndpoint; |
|
25
|
3 |
|
$this->allowedMethodList = [Request::METHOD_POST, Request::METHOD_OPTIONS]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return Response |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function httpOptions() : Response |
|
32
|
|
|
{ |
|
33
|
1 |
|
$response = new Response(); |
|
34
|
1 |
|
$response->headers->set('Content-Type', 'application/json'); |
|
35
|
|
|
|
|
36
|
|
|
// Set allowed http methods |
|
37
|
1 |
|
$allowedMethodListString = implode(', ', $this->allowedMethodList); |
|
38
|
1 |
|
$response->headers->set('Allow', $allowedMethodListString); |
|
39
|
1 |
|
$response->headers->set('Access-Control-Request-Method', $allowedMethodListString); |
|
40
|
|
|
|
|
41
|
|
|
// Set allowed content type |
|
42
|
1 |
|
$response->headers->set('Accept', 'application/json'); |
|
43
|
1 |
|
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type'); |
|
44
|
|
|
|
|
45
|
1 |
|
return $response; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Request $request |
|
50
|
|
|
* |
|
51
|
|
|
* @return Response |
|
52
|
|
|
*/ |
|
53
|
2 |
|
public function httpPost(Request $request) : Response |
|
54
|
|
|
{ |
|
55
|
2 |
|
$response = new Response(); |
|
56
|
2 |
|
$response->headers->set('Content-Type', 'application/json'); |
|
57
|
|
|
|
|
58
|
2 |
|
$response->setContent( |
|
59
|
2 |
|
$this->sdkEndpoint->index( |
|
60
|
2 |
|
$request->getContent() |
|
61
|
2 |
|
) |
|
62
|
2 |
|
); |
|
63
|
|
|
|
|
64
|
2 |
|
return $response; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|