1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\SymfonyJsonRpcHttpServer\EventListener; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\HttpFoundation\Request; |
5
|
|
|
use Symfony\Component\HttpFoundation\Response; |
6
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent; |
7
|
|
|
use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint as SDKJsonRpcEndpoint; |
8
|
|
|
|
9
|
|
|
class RequestListener |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
private $uri; |
13
|
|
|
/** @var SdkJsonRpcEndpoint */ |
14
|
|
|
private $sdkEndpoint; |
15
|
|
|
/** @var string[] */ |
16
|
|
|
private $allowedMethodList = []; |
17
|
|
|
|
18
|
6 |
|
public function __construct(SDKJsonRpcEndpoint $sdkEndpoint, $uri) |
19
|
|
|
{ |
20
|
6 |
|
$this->uri = $uri; |
21
|
6 |
|
$this->sdkEndpoint = $sdkEndpoint; |
22
|
6 |
|
$this->allowedMethodList = [Request::METHOD_POST, Request::METHOD_OPTIONS]; |
23
|
|
|
} |
24
|
|
|
|
25
|
6 |
|
public function onKernelRequest(RequestEvent $event) |
26
|
|
|
{ |
27
|
6 |
|
if (!$event->isMasterRequest()) { |
|
|
|
|
28
|
|
|
// Don't do anything if it's not the master request ! |
29
|
1 |
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
5 |
|
$request = $event->getRequest(); |
33
|
5 |
|
if ($this->uri === $request->getRequestUri()) { |
34
|
4 |
|
switch ($request->getMethod()) { |
35
|
3 |
|
case Request::METHOD_POST: |
36
|
2 |
|
$event->setResponse($this->httpPost($request)); |
37
|
2 |
|
break; |
38
|
2 |
|
case Request::METHOD_OPTIONS: |
39
|
1 |
|
$event->setResponse($this->httpOptions()); |
40
|
1 |
|
break; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
protected function httpOptions() : Response |
46
|
|
|
{ |
47
|
1 |
|
$response = new Response(); |
48
|
1 |
|
$response->headers->set('Content-Type', 'application/json'); |
49
|
|
|
|
50
|
|
|
// Set allowed http methods |
51
|
1 |
|
$allowedMethodListString = implode(', ', $this->allowedMethodList); |
52
|
1 |
|
$response->headers->set('Allow', $allowedMethodListString); |
53
|
1 |
|
$response->headers->set('Access-Control-Request-Method', $allowedMethodListString); |
54
|
|
|
|
55
|
|
|
// Set allowed content type |
56
|
1 |
|
$response->headers->set('Accept', 'application/json'); |
57
|
1 |
|
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type'); |
58
|
|
|
|
59
|
1 |
|
return $response; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
protected function httpPost(Request $request) : Response |
63
|
|
|
{ |
64
|
2 |
|
$response = new Response(); |
65
|
2 |
|
$response->headers->set('Content-Type', 'application/json'); |
66
|
|
|
|
67
|
2 |
|
$response->setContent( |
68
|
2 |
|
$this->sdkEndpoint->index( |
69
|
2 |
|
$request->getContent() |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
|
73
|
2 |
|
return $response; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.