Skip to content

Commit ec1f49d

Browse files
authored
Improve (#13)
1 parent a9856ff commit ec1f49d

File tree

5 files changed

+83
-7
lines changed

5 files changed

+83
-7
lines changed

features/demo_app/default_config_with_bundle/config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ framework:
44
json_rpc_http_server:
55
methods_mapping:
66
bundledMethodA:
7-
service: 'jsonrpc.method.a'
7+
service: '@jsonrpc.method.a'
88
aliases: 'bundledMethodAAlias'
9-
bundledMethodB: 'jsonrpc.method.b'
10-
bundledGetDummy: 'jsonrpc.method.c'
11-
bundledGetAnotherDummy: 'jsonrpc.method.d'
9+
bundledMethodB: '@jsonrpc.method.b'
10+
bundledGetDummy: '@jsonrpc.method.c'
11+
bundledGetAnotherDummy: '@jsonrpc.method.d'

src/DependencyInjection/JsonRpcHttpServerExtension.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ private function loadJsonRpcMethods(ContainerBuilder $container)
279279
// Check if methods have been defined by tags
280280
$methodServiceList = $container->findTaggedServiceIds(self::JSONRPC_METHOD_TAG);
281281

282-
foreach ($methodServiceList as $serviceId => $tagAttributeList) {
282+
foreach ($methodServiceList as $externalServiceIdString => $tagAttributeList) {
283+
$serviceId = $this->cleanExternalServiceIdString($externalServiceIdString);
283284
$this->checkJsonRpcMethodService($container, $serviceId);
284285
$methodNameList = $this->extractMethodNameList($tagAttributeList, $serviceId);
285286
foreach ($methodNameList as $methodName) {
@@ -289,7 +290,7 @@ private function loadJsonRpcMethods(ContainerBuilder $container)
289290

290291
if ($container->hasParameter(self::METHODS_MAPPING_CONTAINER_PARAM)) {
291292
foreach ($container->getParameter(self::METHODS_MAPPING_CONTAINER_PARAM) as $methodName => $mappingConfig) {
292-
$serviceId = $mappingConfig['service'];
293+
$serviceId = $this->cleanExternalServiceIdString($mappingConfig['service']);
293294
$this->checkJsonRpcMethodService($container, $serviceId);
294295
$this->injectMethodMappingToServiceNameResolver($methodName, $serviceId, $container);
295296
foreach ($mappingConfig['aliases'] as $methodAlias) {
@@ -348,7 +349,10 @@ private function compileAndProcessConfigurations(array $configs, ContainerBuilde
348349
$config = (new Processor())->processConfiguration($configuration, $configs);
349350

350351
if (array_key_exists('method_resolver', $config) && $config['method_resolver']) {
351-
$container->setParameter(self::CUSTOM_METHOD_RESOLVER_CONTAINER_PARAM, $config['method_resolver']);
352+
$container->setParameter(
353+
self::CUSTOM_METHOD_RESOLVER_CONTAINER_PARAM,
354+
$this->cleanExternalServiceIdString($config['method_resolver'])
355+
);
352356
}
353357
if (array_key_exists('methods_mapping', $config) && is_array($config['methods_mapping'])) {
354358
$container->setParameter(self::METHODS_MAPPING_CONTAINER_PARAM, $config['methods_mapping']);
@@ -369,4 +373,13 @@ private function injectMethodMappingToServiceNameResolver(
369373
$container->getDefinition(self::SERVICE_NAME_RESOLVER_SERVICE_NAME)
370374
->addMethodCall('addMethodMapping', [$methodName, $serviceId]);
371375
}
376+
377+
private function cleanExternalServiceIdString(string $externalServiceIdString)
378+
{
379+
if ('@' === $externalServiceIdString[0]) {
380+
return substr($externalServiceIdString, 1);
381+
}
382+
383+
return $externalServiceIdString;
384+
}
372385
}

tests/Common/DependencyInjection/AbstractTestClass.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ protected function assertEndpointIsUsable()
4747
$this->assertNotNull($this->container->get(self::EXPECTED_ENDPOINT_SERVICE_ID));
4848
}
4949

50+
/**
51+
* @param $jsonRpcMethodServiceId
52+
*/
53+
protected function assertJsonRpcMethodServiceIsAvailable($jsonRpcMethodServiceId)
54+
{
55+
$this->assertNotNull($this->container->get($jsonRpcMethodServiceId));
56+
}
57+
5058
/**
5159
* @return Definition
5260
*/

tests/Functional/DependencyInjection/JsonRpcHttpServerExtensionWithConfigParsedTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public function testShouldManageMethodsMapping()
101101
[$methodCName, $serviceC]
102102
);
103103

104+
$this->assertJsonRpcMethodServiceIsAvailable($serviceA);
105+
$this->assertJsonRpcMethodServiceIsAvailable($serviceB);
106+
$this->assertJsonRpcMethodServiceIsAvailable($serviceC);
107+
104108
$this->assertEndpointIsUsable();
105109
}
106110

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace Tests\Technical\DependencyInjection;
3+
4+
use Symfony\Component\DependencyInjection\Exception\LogicException;
5+
use Tests\Common\DependencyInjection\AbstractTestClass;
6+
use Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection\JsonRpcHttpServerExtension;
7+
8+
/**
9+
* @covers \Yoanm\SymfonyJsonRpcHttpServer\DependencyInjection\JsonRpcHttpServerExtension
10+
*/
11+
class JsonRpcHttpServerExtensionWithConfigurationParsedTest extends AbstractTestClass
12+
{
13+
/**
14+
* {@inheritdoc}
15+
*/
16+
protected function getContainerExtensions()
17+
{
18+
return [
19+
new JsonRpcHttpServerExtension(true)
20+
];
21+
}
22+
23+
public function testShouldNormalizeExternalServiceIdStringPassedForMethodResolver()
24+
{
25+
$myCustomResolverServiceId = 'my-custom-resolver';
26+
$this->setDefinition($myCustomResolverServiceId, $this->createCustomMethodResolverDefinition());
27+
28+
$this->load(['method_resolver' => '@'.$myCustomResolverServiceId]);
29+
30+
$this->assertEndpointIsUsable();
31+
}
32+
33+
public function testShouldNormalizeExternalServiceIdStringPassedForMethodMapping()
34+
{
35+
$jsonRpcMethodServiceId = uniqid();
36+
37+
$methodService = $this->createJsonRpcMethodDefinition();
38+
$this->setDefinition($jsonRpcMethodServiceId, $methodService);
39+
40+
41+
$this->load([
42+
'methods_mapping' => [
43+
'a-method' => '@'.$jsonRpcMethodServiceId
44+
]
45+
]);
46+
47+
$this->assertJsonRpcMethodServiceIsAvailable($jsonRpcMethodServiceId);
48+
49+
$this->assertEndpointIsUsable();
50+
}
51+
}

0 commit comments

Comments
 (0)