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
|
2 |
|
public function __construct(SDKJsonRpcEndpoint $sdkEndpoint) |
23
|
|
|
{ |
24
|
2 |
|
$this->sdkEndpoint = $sdkEndpoint; |
25
|
2 |
|
$this->allowedMethodList = [Request::METHOD_POST, 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 httpPost(Request $request) : Response |
55
|
|
|
{ |
56
|
1 |
|
$response = new Response(); |
57
|
1 |
|
$response->headers->set('Content-Type', 'application/json'); |
58
|
|
|
|
59
|
1 |
|
$response->setContent( |
60
|
1 |
|
$this->sdkEndpoint->index( |
61
|
1 |
|
$request->getContent() |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
|
65
|
1 |
|
return $response; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.