Skip to content

Commit 980b0e9

Browse files
authored
Add configuration (#5)
1 parent 47c396c commit 980b0e9

File tree

14 files changed

+216
-62
lines changed

14 files changed

+216
-62
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"squizlabs/php_codesniffer": "3.*",
4343
"phpunit/phpunit": "^6.0 || ^7.0",
4444
"matthiasnoback/symfony-dependency-injection-test": "^2.0 || ^3.0",
45+
"matthiasnoback/symfony-config-test": "^3.0 || ^4.0",
4546
"symfony/framework-bundle": "^3.4",
4647
"symfony/http-kernel": "^3.4",
4748
"symfony/routing": "^3.4"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
framework:
22
secret: '%env(APP_SECRET)%'
3+
4+
json_rpc_http_server: ~
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
framework:
22
secret: '%env(APP_SECRET)%'
3+
4+
json_rpc_http_server:
5+
method_resolver: 'jsonrpc.custom_method_resolver'

features/demo_app/default_config_with_bundle_and_resolver/services.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ services:
1515
jsonrpc.method.d:
1616
class: DemoApp\Method\MethodD
1717

18-
##### Configure your resolver and add the required tag
18+
##### Configure your resolver (do not add the tag, service will be injected thanks to bundle configuration)
1919
jsonrpc.custom_method_resolver:
20-
class: Tests\Common\Infra\Symfony\DependencyInjection\CustomMethodResolverClass
21-
tags: ['yoanm.jsonrpc_http_server.method_resolver']
22-
20+
class: DemoApp\Resolver\JsonRpcMethodResolver
2321
# Inject your JSON-RPC methods (They cannot be automatically injected as resolver implementation is on your own)
2422
calls:
2523
- method: 'addMethod'

features/demo_app/default_config_with_resolver/services.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ services:
1818

1919
##### Configure your resolver and add the required tag
2020
jsonrpc.custom_method_resolver:
21-
class: Tests\Common\Infra\Symfony\DependencyInjection\CustomMethodResolverClass
21+
class: DemoApp\Resolver\JsonRpcMethodResolver
2222
tags: ['yoanm.jsonrpc_http_server.method_resolver']
2323

2424
# Inject your JSON-RPC methods (They cannot be automatically injected as resolver implementation is on your own)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace Yoanm\SymfonyJsonRpcHttpServer\Infra\Symfony\DependencyInjection;
3+
4+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
5+
use Symfony\Component\Config\Definition\ConfigurationInterface;
6+
7+
class Configuration implements ConfigurationInterface
8+
{
9+
public function getConfigTreeBuilder()
10+
{
11+
$treeBuilder = new TreeBuilder();
12+
13+
$rootNode = $treeBuilder->root(JsonRpcHttpServerExtension::EXTENSION_IDENTIFIER);
14+
15+
$rootNode
16+
->children()
17+
->variableNode('method_resolver')
18+
->info('Your custom method resolver service')
19+
->treatNullLike(false)
20+
->defaultFalse()
21+
->end()
22+
->end()
23+
;
24+
25+
return $treeBuilder;
26+
}
27+
}

src/Infra/Symfony/DependencyInjection/JsonRpcHttpServerExtension.php

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Yoanm\SymfonyJsonRpcHttpServer\Infra\Symfony\DependencyInjection;
33

4+
use Symfony\Component\Config\Definition\Processor; // <= Must stay optional !
45
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
56
use Symfony\Component\DependencyInjection\ContainerBuilder;
67
use Symfony\Component\DependencyInjection\Definition;
@@ -30,16 +31,21 @@ class JsonRpcHttpServerExtension implements ExtensionInterface, CompilerPassInte
3031
{
3132
// Use this service to inject string request
3233
const ENDPOINT_SERVICE_NAME = 'yoanm.jsonrpc_http_server.endpoint';
34+
3335
// Use this tag to inject your own resolver
3436
const METHOD_RESOLVER_TAG = 'yoanm.jsonrpc_http_server.method_resolver';
37+
3538
// Use this tag to inject your JSON-RPC methods into the default method resolver
3639
const JSONRPC_METHOD_TAG = 'yoanm.jsonrpc_http_server.jsonrpc_method';
40+
3741
// In case you want to add mapping for a method, use the following service
3842
const SERVICE_NAME_RESOLVER_SERVICE_NAME = 'yoanm.jsonrpc_http_server.resolver.service_name';
39-
40-
43+
// And add an attribute with following key
4144
const JSONRPC_METHOD_TAG_METHOD_NAME_KEY = 'method';
4245

46+
// Extension identifier (used in configuration for instance)
47+
const EXTENSION_IDENTIFIER = 'json_rpc_http_server';
48+
4349

4450
private $sdkAppResponseCreatorServiceId = 'sdk.app.creator.response';
4551
private $sdkAppCustomExceptionCreatorServiceId = 'sdk.app.creator.custom_exception';
@@ -55,12 +61,26 @@ class JsonRpcHttpServerExtension implements ExtensionInterface, CompilerPassInte
5561
private $psr11InfraMethodResolverServiceId = 'psr11.infra.resolver.method';
5662

5763
private $methodResolverStubServiceId = 'infra.resolver.method';
64+
private $customResolverContainerParameter = self::EXTENSION_IDENTIFIER.'.custom_method_resolver';
65+
66+
/** @var bool */
67+
private $parseConfig = false;
68+
69+
/**
70+
* @param bool|false $parseConfig If true, Config component is required
71+
*/
72+
public function __construct(bool $parseConfig = false)
73+
{
74+
$this->parseConfig = $parseConfig;
75+
}
5876

5977
/**
6078
* {@inheritdoc}
6179
*/
6280
public function load(array $configs, ContainerBuilder $container)
6381
{
82+
$this->compileAndProcessConfigurations($configs, $container);
83+
6484
// Use only references to avoid class instantiation
6585
// And don't use file configuration in order to not add Symfony\Component\Config as dependency
6686
$this->createPublicServiceDefinitions($container);
@@ -89,7 +109,7 @@ public function getXsdValidationBasePath()
89109
*/
90110
public function getAlias()
91111
{
92-
return 'json_rpc_http_server';
112+
return self::EXTENSION_IDENTIFIER;
93113
}
94114

95115
/**
@@ -235,23 +255,27 @@ protected function createPublicServiceDefinitions(ContainerBuilder $container)
235255
private function aliasMethodResolver(ContainerBuilder $container)
236256
{
237257
$isContainerResolver = false;
238-
$serviceIdList = array_keys($container->findTaggedServiceIds(self::METHOD_RESOLVER_TAG));
239-
$serviceCount = count($serviceIdList);
240-
if ($serviceCount > 0) {
241-
if ($serviceCount > 1) {
242-
throw new LogicException(
243-
sprintf(
244-
'Only one method resolver could be defined, found following services : %s',
245-
implode(', ', $serviceIdList)
246-
)
247-
);
248-
}
249-
// Use the first result
250-
$resolverServiceId = array_shift($serviceIdList);
258+
if ($container->hasParameter($this->customResolverContainerParameter)) {
259+
$resolverServiceId = $container->getParameter($this->customResolverContainerParameter);
251260
} else {
252-
// Use ArrayMethodResolver as default resolver
253-
$resolverServiceId = $this->prependServiceName($this->psr11InfraMethodResolverServiceId);
254-
$isContainerResolver = true;
261+
$serviceIdList = array_keys($container->findTaggedServiceIds(self::METHOD_RESOLVER_TAG));
262+
$serviceCount = count($serviceIdList);
263+
if ($serviceCount > 0) {
264+
if ($serviceCount > 1) {
265+
throw new LogicException(
266+
sprintf(
267+
'Only one method resolver could be defined, found following services : %s',
268+
implode(', ', $serviceIdList)
269+
)
270+
);
271+
}
272+
// Use the first result
273+
$resolverServiceId = array_shift($serviceIdList);
274+
} else {
275+
// Use ArrayMethodResolver as default resolver
276+
$resolverServiceId = $this->prependServiceName($this->psr11InfraMethodResolverServiceId);
277+
$isContainerResolver = true;
278+
}
255279
}
256280

257281
$container->setAlias($this->prependServiceName($this->methodResolverStubServiceId), $resolverServiceId);
@@ -324,4 +348,20 @@ private function checkJsonRpcMethodService(ContainerBuilder $container, string $
324348
));
325349
}
326350
}
351+
352+
/**
353+
* @param array $configs
354+
* @param ContainerBuilder $container
355+
*/
356+
private function compileAndProcessConfigurations(array $configs, ContainerBuilder $container)
357+
{
358+
if (true === $this->parseConfig) {
359+
$configuration = new Configuration();
360+
$config = (new Processor())->processConfiguration($configuration, $configs);
361+
362+
if (array_key_exists('method_resolver', $config) && $config['method_resolver']) {
363+
$container->setParameter($this->customResolverContainerParameter, $config['method_resolver']);
364+
}
365+
}
366+
}
327367
}

src/Infra/Symfony/JsonRpcHttpServerBundle.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
namespace Yoanm\SymfonyJsonRpcHttpServer\Infra\Symfony;
33

44
use Symfony\Component\HttpKernel\Bundle\Bundle;
5+
use Yoanm\SymfonyJsonRpcHttpServer\Infra\Symfony\DependencyInjection\JsonRpcHttpServerExtension;
56

67
/**
78
* Class JsonRpcHttpServerBundle
89
*/
910
class JsonRpcHttpServerBundle extends Bundle
1011
{
11-
12+
public function getContainerExtension()
13+
{
14+
return new JsonRpcHttpServerExtension(true);
15+
}
1216
}

tests/Common/Infra/Symfony/DependencyInjection/AbstractTestClass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
55
use Symfony\Component\DependencyInjection\Definition;
6+
use Yoanm\JsonRpcServer\Domain\Model\MethodResolverInterface;
67
use Yoanm\SymfonyJsonRpcHttpServer\Infra\Symfony\DependencyInjection\JsonRpcHttpServerExtension;
78

89
abstract class AbstractTestClass extends AbstractExtensionTestCase
@@ -68,7 +69,7 @@ protected function createJsonRpcMethodDefinition($methodName)
6869
*/
6970
protected function createCustomMethodResolverDefinition()
7071
{
71-
$customResolverService = new Definition(CustomMethodResolverClass::class);
72+
$customResolverService = new Definition($this->prophesize(MethodResolverInterface::class)->reveal());
7273
$customResolverService->addTag(self::EXPECTED_METHOD_RESOLVER_TAG);
7374

7475
return $customResolverService;

tests/Common/Infra/Symfony/DependencyInjection/CustomMethodResolverClass.php

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

0 commit comments

Comments
 (0)