Skip to content

Commit ef282e7

Browse files
committed
Request listener instead of controller
1 parent 72c5008 commit ef282e7

File tree

5 files changed

+84
-27
lines changed

5 files changed

+84
-27
lines changed

features/demo_app/default_config/routes.yaml

Lines changed: 0 additions & 3 deletions
This file was deleted.

features/demo_app/mapping_collector_config/routes.yaml

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/EventListener/RequestListener.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
public function __construct(SDKJsonRpcEndpoint $sdkEndpoint, $uri)
19+
{
20+
var_dump("CONSTRUCT");
21+
$this->uri = $uri;
22+
$this->sdkEndpoint = $sdkEndpoint;
23+
$this->allowedMethodList = [Request::METHOD_POST, Request::METHOD_OPTIONS];
24+
}
25+
26+
public function onKernelRequest(RequestEvent $event)
27+
{
28+
if (!$event->isMasterRequest()) {
29+
// Don't do anything if it's not the master request !
30+
return;
31+
}
32+
33+
$request = $event->getRequest();
34+
if ($this->uri === $request->getRequestUri()) {
35+
switch ($request->getMethod()) {
36+
case Request::METHOD_POST:
37+
$event->setResponse($this->httpPost($request));
38+
break;
39+
case Request::METHOD_OPTIONS:
40+
$event->setResponse($this->httpOptions());
41+
break;
42+
}
43+
}
44+
}
45+
46+
protected function httpOptions() : Response
47+
{
48+
$response = new Response();
49+
$response->headers->set('Content-Type', 'application/json');
50+
51+
// Set allowed http methods
52+
$allowedMethodListString = implode(', ', $this->allowedMethodList);
53+
$response->headers->set('Allow', $allowedMethodListString);
54+
$response->headers->set('Access-Control-Request-Method', $allowedMethodListString);
55+
56+
// Set allowed content type
57+
$response->headers->set('Accept', 'application/json');
58+
$response->headers->set('Access-Control-Allow-Headers', 'Content-Type');
59+
60+
return $response;
61+
}
62+
63+
protected function httpPost(Request $request) : Response
64+
{
65+
$response = new Response();
66+
$response->headers->set('Content-Type', 'application/json');
67+
68+
$response->setContent(
69+
$this->sdkEndpoint->index(
70+
$request->getContent()
71+
)
72+
);
73+
74+
return $response;
75+
}
76+
}

src/Resources/config/routing/endpoint.xml

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Resources/config/services.public.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ services:
77
arguments:
88
- '@json_rpc_server_sdk.infra.endpoint'
99

10+
json_rpc_http_server.request_listener:
11+
class: Yoanm\SymfonyJsonRpcHttpServer\EventListener\RequestListener
12+
arguments:
13+
- '@json_rpc_server_sdk.infra.endpoint'
14+
- '%json_rpc_http_server.http_endpoint_path%'
15+
tags:
16+
- { name: kernel.event_listener, event: kernel.request, method: 'onKernelRequest', priority: 9999 }
17+
1018
json_rpc_http_server.dispatcher.server:
1119
class: Yoanm\SymfonyJsonRpcHttpServer\Dispatcher\SymfonyJsonRpcServerDispatcher
1220
arguments:

0 commit comments

Comments
 (0)