1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\SymfonyJsonRpcHttpServer\Infra\Symfony\DependencyInjection; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
5
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
6
|
|
|
use Symfony\Component\DependencyInjection\Exception\LogicException; |
7
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
9
|
|
|
use Yoanm\JsonRpcServer\App\Creator\CustomExceptionCreator; |
10
|
|
|
use Yoanm\JsonRpcServer\App\Creator\ResponseCreator; |
11
|
|
|
use Yoanm\JsonRpcServer\App\Manager\MethodManager; |
12
|
|
|
use Yoanm\JsonRpcServer\App\RequestHandler; |
13
|
|
|
use Yoanm\JsonRpcServer\App\Serialization\RequestDenormalizer; |
14
|
|
|
use Yoanm\JsonRpcServer\App\Serialization\ResponseNormalizer; |
15
|
|
|
use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint; |
16
|
|
|
use Yoanm\JsonRpcServer\Infra\Serialization\RawRequestSerializer; |
17
|
|
|
use Yoanm\JsonRpcServer\Infra\Serialization\RawResponseSerializer; |
18
|
|
|
use Yoanm\JsonRpcServerPsr11Resolver\Infra\Resolver\ContainerMethodResolver; |
19
|
|
|
use Yoanm\SymfonyJsonRpcHttpServer\Infra\Endpoint\JsonRpcHttpEndpoint; |
20
|
|
|
use Yoanm\SymfonyJsonRpcHttpServer\Infra\Resolver\ServiceNameResolver; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class JsonRpcHttpServerExtension |
24
|
|
|
* |
25
|
|
|
* /!\ In case you the default resolver (yoanm/jsonrpc-server-sdk-psr11-resolver), |
26
|
|
|
* your JSON-RPC method services must be public in order to retrieve it later from container |
27
|
|
|
*/ |
28
|
|
|
class JsonRpcHttpServerExtension extends Extension |
29
|
|
|
{ |
30
|
|
|
// Use this service to inject string request |
31
|
|
|
const ENDPOINT_SERVICE_NAME = 'yoanm.jsonrpc_http_server.endpoint'; |
32
|
|
|
// Use this tag to inject your own resolver |
33
|
|
|
const METHOD_RESOLVER_TAG = 'yoanm.jsonrpc_http_server.method_resolver'; |
34
|
|
|
// Use this tag to inject your JSON-RPC methods into the default method resolver |
35
|
|
|
const JSONRPC_METHOD_TAG = 'yoanm.jsonrpc_http_server.jsonrpc_method'; |
36
|
|
|
// In case you want to add mapping for a method, use the following service |
37
|
|
|
const SERVICE_NAME_RESOLVER_SERVICE_NAME = 'yoanm.jsonrpc_http_server.resolver.service_name'; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
const JSONRPC_METHOD_TAG_METHOD_NAME_KEY = 'method'; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
private $sdkAppResponseCreatorServiceId = 'sdk.app.creator.response'; |
44
|
|
|
private $sdkAppCustomExceptionCreatorServiceId = 'sdk.app.creator.custom_exception'; |
45
|
|
|
private $sdkAppRequestDenormalizerServiceId = 'sdk.app.serialization.request_denormalizer'; |
46
|
|
|
private $sdkAppResponseNormalizerServiceId = 'sdk.app.serialization.response_normalizer'; |
47
|
|
|
private $sdkAppMethodManagerServiceId = 'sdk.app.manager.method'; |
48
|
|
|
private $sdkAppRequestHandlerServiceId = 'sdk.app.handler.request'; |
49
|
|
|
|
50
|
|
|
private $sdkInfraEndpointServiceId = 'sdk.infra.endpoint'; |
51
|
|
|
private $sdkInfraRawReqSerializerServiceId = 'sdk.infra.serialization.raw_request_serializer'; |
52
|
|
|
private $sdkInfraRawRespSerializerServiceId = 'sdk.infra.serialization.raw_response_serializer'; |
53
|
|
|
|
54
|
|
|
private $psr11InfraMethodResolverServiceId = 'psr11.infra.resolver.method'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function load(array $configs, ContainerBuilder $container) |
60
|
|
|
{ |
61
|
|
|
// Use only references to avoid class instantiation |
62
|
|
|
// And don't use file configuration in order to not add Symfony\Component\Config as dependency |
63
|
|
|
$this->createPublicServiceDefinitions($container); |
64
|
|
|
$this->createInfraServiceDefinitions($container); |
65
|
|
|
$this->createAppServiceDefinitions($container); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param ContainerBuilder $container |
70
|
|
|
*/ |
71
|
|
|
protected function createAppServiceDefinitions(ContainerBuilder $container) |
72
|
|
|
{ |
73
|
|
|
// RequestDenormalizer |
74
|
|
|
$container->setDefinition( |
75
|
|
|
$this->prependServiceName($this->sdkAppRequestDenormalizerServiceId), |
76
|
|
|
new Definition(RequestDenormalizer::class) |
77
|
|
|
); |
78
|
|
|
// ResponseNormalizer |
79
|
|
|
$container->setDefinition( |
80
|
|
|
$this->prependServiceName($this->sdkAppResponseNormalizerServiceId), |
81
|
|
|
new Definition(ResponseNormalizer::class) |
82
|
|
|
); |
83
|
|
|
// ResponseCreator |
84
|
|
|
$container->setDefinition( |
85
|
|
|
$this->prependServiceName($this->sdkAppResponseCreatorServiceId), |
86
|
|
|
new Definition(ResponseCreator::class) |
87
|
|
|
); |
88
|
|
|
// CustomExceptionCreator |
89
|
|
|
$container->setDefinition( |
90
|
|
|
$this->prependServiceName($this->sdkAppCustomExceptionCreatorServiceId), |
91
|
|
|
new Definition(CustomExceptionCreator::class) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
// MethodManager |
95
|
|
|
$container->setDefinition( |
96
|
|
|
$this->prependServiceName($this->sdkAppMethodManagerServiceId), |
97
|
|
|
new Definition( |
98
|
|
|
MethodManager::class, |
99
|
|
|
[ |
100
|
|
|
new Reference($this->getMethodResolverServiceId($container)), |
101
|
|
|
new Reference($this->prependServiceName($this->sdkAppCustomExceptionCreatorServiceId)) |
102
|
|
|
] |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
// RequestHandler |
106
|
|
|
$container->setDefinition( |
107
|
|
|
$this->prependServiceName($this->sdkAppRequestHandlerServiceId), |
108
|
|
|
new Definition( |
109
|
|
|
RequestHandler::class, |
110
|
|
|
[ |
111
|
|
|
new Reference($this->prependServiceName($this->sdkAppMethodManagerServiceId)), |
112
|
|
|
new Reference($this->prependServiceName($this->sdkAppResponseCreatorServiceId)) |
113
|
|
|
] |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param ContainerBuilder $container |
120
|
|
|
*/ |
121
|
|
|
protected function createInfraServiceDefinitions(ContainerBuilder $container) |
122
|
|
|
{ |
123
|
|
|
// RawRequestSerializer |
124
|
|
|
$container->setDefinition( |
125
|
|
|
$this->prependServiceName($this->sdkInfraRawReqSerializerServiceId), |
126
|
|
|
new Definition( |
127
|
|
|
RawRequestSerializer::class, |
128
|
|
|
[new Reference($this->prependServiceName($this->sdkAppRequestDenormalizerServiceId))] |
129
|
|
|
) |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
// RawResponseSerializer |
133
|
|
|
$container->setDefinition( |
134
|
|
|
$this->prependServiceName($this->sdkInfraRawRespSerializerServiceId), |
135
|
|
|
new Definition( |
136
|
|
|
RawResponseSerializer::class, |
137
|
|
|
[new Reference($this->prependServiceName($this->sdkAppResponseNormalizerServiceId))] |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
// JsonRpcEndpoint |
141
|
|
|
$container->setDefinition( |
142
|
|
|
$this->prependServiceName($this->sdkInfraEndpointServiceId), |
143
|
|
|
new Definition( |
144
|
|
|
JsonRpcEndpoint::class, |
145
|
|
|
[ |
146
|
|
|
new Reference($this->prependServiceName($this->sdkInfraRawReqSerializerServiceId)), |
147
|
|
|
new Reference($this->prependServiceName($this->sdkAppRequestHandlerServiceId)), |
148
|
|
|
new Reference($this->prependServiceName($this->sdkInfraRawRespSerializerServiceId)), |
149
|
|
|
new Reference($this->prependServiceName($this->sdkAppResponseCreatorServiceId)) |
150
|
|
|
] |
151
|
|
|
) |
152
|
|
|
); |
153
|
|
|
// ContainerMethodResolver |
154
|
|
|
$container->setDefinition( |
155
|
|
|
$this->prependServiceName($this->psr11InfraMethodResolverServiceId), |
156
|
|
|
(new Definition( |
157
|
|
|
ContainerMethodResolver::class, |
158
|
|
|
[ |
159
|
|
|
new Reference('container') |
160
|
|
|
] |
161
|
|
|
))->addMethodCall( |
162
|
|
|
'setServiceNameResolver', |
163
|
|
|
[ |
164
|
|
|
new Reference(self::SERVICE_NAME_RESOLVER_SERVICE_NAME) |
165
|
|
|
] |
166
|
|
|
) |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param ContainerBuilder $container |
172
|
|
|
*/ |
173
|
|
|
protected function createPublicServiceDefinitions(ContainerBuilder $container) |
174
|
|
|
{ |
175
|
|
|
// JsonRpcHttpEndpoint |
176
|
|
|
$container->setDefinition( |
177
|
|
|
self::ENDPOINT_SERVICE_NAME, |
178
|
|
|
(new Definition( |
179
|
|
|
JsonRpcHttpEndpoint::class, |
180
|
|
|
[ |
181
|
|
|
new Reference($this->prependServiceName($this->sdkInfraEndpointServiceId)) |
182
|
|
|
] |
183
|
|
|
))->setPrivate(false) |
184
|
|
|
); |
185
|
|
|
// ServiceNameResolver |
186
|
|
|
$container->setDefinition( |
187
|
|
|
self::SERVICE_NAME_RESOLVER_SERVICE_NAME, |
188
|
|
|
(new Definition(ServiceNameResolver::class))->setPrivate(false) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param ContainerBuilder $container |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
private function getMethodResolverServiceId(ContainerBuilder $container) |
198
|
|
|
{ |
199
|
|
|
$serviceIdList = array_keys($container->findTaggedServiceIds(self::METHOD_RESOLVER_TAG)); |
200
|
|
|
$serviceCount = count($serviceIdList); |
201
|
|
|
if ($serviceCount > 0) { |
202
|
|
|
if ($serviceCount > 1) { |
203
|
|
|
throw new LogicException( |
204
|
|
|
sprintf( |
205
|
|
|
'Only one method resolver could be defined, found following services : %s', |
206
|
|
|
implode(', ', $serviceIdList) |
207
|
|
|
) |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
// Use the first result |
211
|
|
|
$resolverServiceId = array_shift($serviceIdList); |
212
|
|
|
} else { |
213
|
|
|
// Use ArrayMethodResolver as default resolver |
214
|
|
|
$resolverServiceId = $this->prependServiceName($this->psr11InfraMethodResolverServiceId); |
215
|
|
|
$this->loadJsonRpcMethodsFromTag($container); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $resolverServiceId; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param ContainerBuilder $container |
223
|
|
|
*/ |
224
|
|
|
private function loadJsonRpcMethodsFromTag(ContainerBuilder $container) |
225
|
|
|
{ |
226
|
|
|
// Check if methods have been defined by tags |
227
|
|
|
$methodServiceList = $container->findTaggedServiceIds(self::JSONRPC_METHOD_TAG); |
228
|
|
|
$defaultResolverDefinition = $container->getDefinition(self::SERVICE_NAME_RESOLVER_SERVICE_NAME); |
229
|
|
|
|
230
|
|
|
foreach ($methodServiceList as $serviceId => $tags) { |
231
|
|
|
$firstTag = array_shift($tags); |
232
|
|
|
if (!is_array($firstTag) || !array_key_exists(self::JSONRPC_METHOD_TAG_METHOD_NAME_KEY, $firstTag)) { |
233
|
|
|
throw new LogicException(sprintf( |
234
|
|
|
'Service %s is taggued as JSON-RPC method but does not have' |
235
|
|
|
.'method name defined under "%s" tag attribute key', |
236
|
|
|
$serviceId, |
237
|
|
|
self::JSONRPC_METHOD_TAG_METHOD_NAME_KEY |
238
|
|
|
)); |
239
|
|
|
} |
240
|
|
|
// Check if given service is public => must be public in order to get it from container later |
241
|
|
|
if ($container->getDefinition($serviceId)->isPrivate()) { |
242
|
|
|
throw new LogicException(sprintf( |
243
|
|
|
'Service %s is taggued as JSON-RPC method but is not public. Service must be public in order' |
244
|
|
|
.' to retrieve it later', |
245
|
|
|
$serviceId |
246
|
|
|
)); |
247
|
|
|
} |
248
|
|
|
$defaultResolverDefinition->addMethodCall( |
249
|
|
|
'addMethodMapping', |
250
|
|
|
[ |
251
|
|
|
$firstTag[self::JSONRPC_METHOD_TAG_METHOD_NAME_KEY], |
252
|
|
|
$serviceId |
253
|
|
|
] |
254
|
|
|
); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param string $serviceName |
260
|
|
|
* |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
|
|
private function prependServiceName(string $serviceName) : string |
264
|
|
|
{ |
265
|
|
|
return sprintf('yoanm.jsonrpc_http_server.%s', $serviceName); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|