|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Config\Definition\Processor; |
|
5
|
|
|
use Symfony\Component\Config\FileLocator; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Exception\LogicException; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
12
|
|
|
use Yoanm\JsonRpcServer\App\Dispatcher\JsonRpcServerDispatcherAwareTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class JsonRpcHttpServerExtension |
|
16
|
|
|
*/ |
|
17
|
|
|
class JsonRpcHttpServerExtension implements ExtensionInterface, CompilerPassInterface |
|
18
|
|
|
{ |
|
19
|
|
|
// Extension identifier (used in configuration for instance) |
|
20
|
|
|
const EXTENSION_IDENTIFIER = 'json_rpc_http_server'; |
|
21
|
|
|
|
|
22
|
|
|
/** Tags */ |
|
23
|
|
|
// Server dispatcher - Use this tag and server dispatcher will be injected |
|
24
|
|
|
const JSONRPC_SERVER_DISPATCHER_AWARE_TAG = 'json_rpc_http_server.server_dispatcher_aware'; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** Method resolver */ |
|
28
|
|
|
const METHOD_RESOLVER_ALIAS = 'json_rpc_http_server.alias.method_resolver'; |
|
29
|
|
|
/** Params validator */ |
|
30
|
|
|
const PARAMS_VALIDATOR_ALIAS = 'json_rpc_http_server.alias.params_validator'; |
|
31
|
|
|
|
|
32
|
|
|
const REQUEST_HANDLER_SERVICE_ID = 'json_rpc_server_sdk.app.handler.jsonrpc_request'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
7 |
|
public function load(array $configs, ContainerBuilder $container) |
|
38
|
|
|
{ |
|
39
|
7 |
|
$this->compileAndProcessConfigurations($configs, $container); |
|
40
|
|
|
|
|
41
|
7 |
|
$loader = new YamlFileLoader( |
|
42
|
7 |
|
$container, |
|
43
|
7 |
|
new FileLocator(__DIR__.'/../Resources/config') |
|
44
|
|
|
); |
|
45
|
7 |
|
$loader->load('sdk.services.app.yaml'); |
|
46
|
7 |
|
$loader->load('sdk.services.infra.yaml'); |
|
47
|
7 |
|
$loader->load('services.public.yaml'); |
|
48
|
7 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
7 |
|
public function process(ContainerBuilder $container) |
|
54
|
|
|
{ |
|
55
|
7 |
|
$this->bindJsonRpcServerDispatcher($container); |
|
56
|
6 |
|
$this->bindValidatorIfDefined($container); |
|
57
|
6 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
8 |
|
public function getNamespace() |
|
63
|
|
|
{ |
|
64
|
8 |
|
return 'http://example.org/schema/dic/'.$this->getAlias(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getXsdValidationBasePath() |
|
71
|
|
|
{ |
|
72
|
1 |
|
return ''; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
8 |
|
public function getAlias() |
|
79
|
|
|
{ |
|
80
|
8 |
|
return self::EXTENSION_IDENTIFIER; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param ContainerBuilder $container |
|
85
|
|
|
* |
|
86
|
|
|
* @return Reference|null Null in case no dispatcher found |
|
87
|
|
|
*/ |
|
88
|
7 |
|
private function bindJsonRpcServerDispatcher(ContainerBuilder $container) |
|
89
|
|
|
{ |
|
90
|
7 |
|
$dispatcherRef = new Reference('json_rpc_http_server.dispatcher.server'); |
|
91
|
7 |
|
$dispatcherAwareServiceList = $container->findTaggedServiceIds(self::JSONRPC_SERVER_DISPATCHER_AWARE_TAG); |
|
92
|
7 |
|
foreach ($dispatcherAwareServiceList as $serviceId => $tagAttributeList) { |
|
93
|
7 |
|
$definition = $container->getDefinition($serviceId); |
|
94
|
|
|
|
|
95
|
7 |
|
if (!in_array(JsonRpcServerDispatcherAwareTrait::class, class_uses($definition->getClass()))) { |
|
96
|
1 |
|
throw new LogicException( |
|
97
|
1 |
|
sprintf( |
|
98
|
1 |
|
'Service "%s" is taggued with "%s" but does not use "%s"', |
|
99
|
1 |
|
$serviceId, |
|
100
|
1 |
|
self::JSONRPC_SERVER_DISPATCHER_AWARE_TAG, |
|
101
|
1 |
|
JsonRpcServerDispatcherAwareTrait::class |
|
102
|
|
|
) |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
6 |
|
$definition->addMethodCall('setJsonRpcServerDispatcher', [$dispatcherRef]); |
|
107
|
|
|
} |
|
108
|
6 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param array $configs |
|
112
|
|
|
* @param ContainerBuilder $container |
|
113
|
|
|
*/ |
|
114
|
7 |
|
private function compileAndProcessConfigurations(array $configs, ContainerBuilder $container) |
|
115
|
|
|
{ |
|
116
|
7 |
|
$configuration = new Configuration(); |
|
117
|
7 |
|
$config = (new Processor())->processConfiguration($configuration, $configs); |
|
118
|
|
|
|
|
119
|
7 |
|
$httpEndpointPath = $config['endpoint']; |
|
120
|
|
|
|
|
121
|
7 |
|
$container->setParameter(self::EXTENSION_IDENTIFIER.'.http_endpoint_path', $httpEndpointPath); |
|
122
|
7 |
|
} |
|
123
|
|
|
|
|
124
|
6 |
|
private function bindValidatorIfDefined(ContainerBuilder $container) |
|
125
|
|
|
{ |
|
126
|
6 |
|
if ($container->hasAlias(self::PARAMS_VALIDATOR_ALIAS)) { |
|
127
|
1 |
|
$container->getDefinition(self::REQUEST_HANDLER_SERVICE_ID) |
|
128
|
1 |
|
->addMethodCall( |
|
129
|
1 |
|
'setMethodParamsValidator', |
|
130
|
|
|
[ |
|
131
|
1 |
|
new Reference(self::PARAMS_VALIDATOR_ALIAS) |
|
132
|
|
|
] |
|
133
|
|
|
) |
|
134
|
|
|
; |
|
135
|
|
|
} |
|
136
|
6 |
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|