-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathJsonRpcHttpServerExtension.php
226 lines (194 loc) · 8.1 KB
/
JsonRpcHttpServerExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Yoanm\JsonRpcServer\App\Dispatcher\JsonRpcServerDispatcherAwareTrait;
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodAwareInterface;
/**
* Class JsonRpcHttpServerExtension
*/
class JsonRpcHttpServerExtension implements ExtensionInterface, CompilerPassInterface
{
// Extension identifier (used in configuration for instance)
public const EXTENSION_IDENTIFIER = 'json_rpc_http_server';
public const ENDPOINT_PATH_CONTAINER_PARAM_ID = self::EXTENSION_IDENTIFIER.'.http_endpoint_path';
/** Tags */
/**** Methods tags **/
// Use this tag to inject your JSON-RPC methods into the default method resolver
public const JSONRPC_METHOD_TAG = 'json_rpc_http_server.jsonrpc_method';
// And add an attribute with following key
public const JSONRPC_METHOD_TAG_METHOD_NAME_KEY = 'method';
/**** END - Methods tags **/
// Server dispatcher - Use this tag and server dispatcher will be injected
public const JSONRPC_SERVER_DISPATCHER_AWARE_TAG = 'json_rpc_http_server.server_dispatcher_aware';
// JSON-RPC Methods mapping - Use this tag and all JSON-RPC method instance will be injected
// Useful for documentation for instance
public const JSONRPC_METHOD_AWARE_TAG = 'json_rpc_http_server.method_aware';
private const PARAMS_VALIDATOR_ALIAS = 'json_rpc_http_server.alias.params_validator';
private const REQUEST_HANDLER_SERVICE_ID = 'json_rpc_server_sdk.app.handler.jsonrpc_request';
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$this->compileAndProcessConfigurations($configs, $container);
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('sdk.services.app.yaml');
$loader->load('sdk.services.infra.yaml');
$loader->load('services.private.yaml');
$loader->load('services.public.yaml');
}
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$this->bindJsonRpcServerDispatcher($container);
$this->bindValidatorIfDefined($container);
$this->bindJsonRpcMethods($container);
}
/**
* {@inheritdoc}
*/
public function getNamespace()
{
return 'http://example.org/schema/dic/'.$this->getAlias();
}
/**
* {@inheritdoc}
*/
public function getXsdValidationBasePath()
{
return '';
}
/**
* {@inheritdoc}
*/
public function getAlias()
{
return self::EXTENSION_IDENTIFIER;
}
/**
* @param array $configs
* @param ContainerBuilder $container
*/
private function compileAndProcessConfigurations(array $configs, ContainerBuilder $container) : void
{
$configuration = new Configuration();
$config = (new Processor())->processConfiguration($configuration, $configs);
$httpEndpointPath = $config['endpoint'];
$container->setParameter(self::ENDPOINT_PATH_CONTAINER_PARAM_ID, $httpEndpointPath);
}
/**
* @param ContainerBuilder $container
*/
private function bindJsonRpcServerDispatcher(ContainerBuilder $container) : void
{
$dispatcherRef = new Reference('json_rpc_http_server.dispatcher.server');
$dispatcherAwareServiceList = $container->findTaggedServiceIds(self::JSONRPC_SERVER_DISPATCHER_AWARE_TAG);
foreach ($dispatcherAwareServiceList as $serviceId => $tagAttributeList) {
$definition = $container->getDefinition($serviceId);
if (!in_array(JsonRpcServerDispatcherAwareTrait::class, class_uses($definition->getClass()))) {
throw new LogicException(
sprintf(
'Service "%s" is taggued with "%s" but does not use "%s"',
$serviceId,
self::JSONRPC_SERVER_DISPATCHER_AWARE_TAG,
JsonRpcServerDispatcherAwareTrait::class
)
);
}
$definition->addMethodCall('setJsonRpcServerDispatcher', [$dispatcherRef]);
}
}
private function bindValidatorIfDefined(ContainerBuilder $container) : void
{
if ($container->hasAlias(self::PARAMS_VALIDATOR_ALIAS)) {
$container->getDefinition(self::REQUEST_HANDLER_SERVICE_ID)
->addMethodCall(
'setMethodParamsValidator',
[
new Reference(self::PARAMS_VALIDATOR_ALIAS)
]
)
;
}
}
/**
* @param ContainerBuilder $container
*/
private function bindJsonRpcMethods(ContainerBuilder $container) : void
{
$mappingAwareServiceDefinitionList = $this->findAndValidateMappingAwareDefinitionList($container);
$jsonRpcMethodDefinitionList = (new JsonRpcMethodDefinitionHelper($container))
->findAndValidateJsonRpcMethodDefinition($container);
$methodMappingList = [];
foreach ($jsonRpcMethodDefinitionList as $jsonRpcMethodServiceId => $methodNameList) {
foreach ($methodNameList as $methodName) {
$methodMappingList[$methodName] = new Reference($jsonRpcMethodServiceId);
$this->bindJsonRpcMethod($methodName, $jsonRpcMethodServiceId, $mappingAwareServiceDefinitionList);
}
}
// Service locator for method resolver
// => first argument is an array of wanted service with keys as alias for internal use
$container->getDefinition('json_rpc_http_server.service_locator.method_resolver')
->setArgument(0, $methodMappingList);
}
/**
* @param string $methodName
* @param string $jsonRpcMethodServiceId
* @param Definition[] $mappingAwareServiceDefinitionList
*/
private function bindJsonRpcMethod(
string $methodName,
string $jsonRpcMethodServiceId,
array $mappingAwareServiceDefinitionList
) : void {
foreach ($mappingAwareServiceDefinitionList as $methodAwareServiceDefinition) {
$methodAwareServiceDefinition->addMethodCall(
'addJsonRpcMethod',
[$methodName, new Reference($jsonRpcMethodServiceId)]
);
}
}
/**
* @param ContainerBuilder $container
*
* @return array
*/
private function findAndValidateMappingAwareDefinitionList(ContainerBuilder $container): array
{
$mappingAwareServiceDefinitionList = [];
$methodAwareServiceIdList = array_keys($container->findTaggedServiceIds(self::JSONRPC_METHOD_AWARE_TAG));
foreach ($methodAwareServiceIdList as $serviceId) {
$definition = $container->getDefinition($serviceId);
$this->checkMethodAwareServiceIdList($definition, $serviceId, $container);
$mappingAwareServiceDefinitionList[$serviceId] = $definition;
}
return $mappingAwareServiceDefinitionList;
}
private function checkMethodAwareServiceIdList(
Definition $definition,
string $serviceId,
ContainerBuilder $container
) : void {
$class = $container->getReflectionClass($definition->getClass());
if (null !== $class && !$class->implementsInterface(JsonRpcMethodAwareInterface::class)) {
throw new LogicException(sprintf(
'Service "%s" is taggued as JSON-RPC method aware but does not implement %s',
$serviceId,
JsonRpcMethodAwareInterface::class
));
}
}
}