1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Rpc\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
6
|
|
|
use Symfony\Component\Config\Definition\Builder\ScalarNodeDefinition; |
7
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
8
|
|
|
use Symfony\Component\Config\Definition\Builder\VariableNodeDefinition; |
9
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
10
|
|
|
|
11
|
|
|
final class Configuration implements ConfigurationInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Generates the configuration tree builder. |
16
|
|
|
* |
17
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder The tree builder |
18
|
|
|
*/ |
19
|
1 |
|
public function getConfigTreeBuilder() |
20
|
|
|
{ |
21
|
1 |
|
$builder = new TreeBuilder(); |
22
|
1 |
|
$root = $builder->root('rpc_server'); |
23
|
1 |
|
$this->configureRouter($root->children()->arrayNode('router')); |
24
|
|
|
|
25
|
1 |
|
return $builder; |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
private function configureRouter(ArrayNodeDefinition $root) |
29
|
|
|
{ |
30
|
2 |
|
$root->addDefaultsIfNotSet(); |
31
|
1 |
|
$root->treatNullLike(['endpoints' => []]); |
32
|
1 |
|
/** @var ArrayNodeDefinition $proto */ |
33
|
2 |
|
$endpoints = $root->children()->arrayNode('endpoints'); |
34
|
|
|
|
35
|
1 |
|
$proto = $endpoints->prototype('array'); |
36
|
1 |
|
|
37
|
2 |
|
$proto->append( |
38
|
3 |
|
(new ScalarNodeDefinition('path')) |
39
|
2 |
|
->isRequired() |
40
|
3 |
|
->example('/') |
41
|
3 |
|
->cannotBeEmpty() |
42
|
2 |
|
->info('Endpoint URI') |
43
|
2 |
|
); |
44
|
2 |
|
$proto->append( |
45
|
2 |
|
(new ArrayNodeDefinition('resources')) |
46
|
2 |
|
->beforeNormalization() |
47
|
2 |
|
->ifNull() |
48
|
3 |
|
->then( |
49
|
1 |
|
function () { |
50
|
2 |
|
return []; |
51
|
2 |
|
} |
52
|
2 |
|
) |
53
|
2 |
|
->ifString() |
54
|
3 |
|
->then( |
55
|
1 |
|
function ($v) { |
56
|
1 |
|
return [$v]; |
57
|
1 |
|
} |
58
|
2 |
|
) |
59
|
2 |
|
->end() |
60
|
3 |
|
->prototype('scalar')->end() |
61
|
3 |
|
->example('rpc.yml') |
62
|
3 |
|
->info('Route definitions') |
63
|
3 |
|
); |
64
|
3 |
|
$proto->append( |
65
|
3 |
|
(new VariableNodeDefinition('defaults')) |
66
|
3 |
|
); |
67
|
3 |
|
$proto->append( |
68
|
3 |
|
(new VariableNodeDefinition('context')) |
69
|
3 |
|
->beforeNormalization() |
70
|
2 |
|
->ifString() |
71
|
2 |
|
->then( |
72
|
2 |
|
function ($v) { |
73
|
2 |
|
return [$v]; |
74
|
2 |
|
} |
75
|
2 |
|
) |
76
|
3 |
|
->end() |
77
|
3 |
|
->defaultValue(['Default']) |
78
|
3 |
|
->info('Endpoint-wide context') |
79
|
2 |
|
->example(['Default']) |
80
|
2 |
|
); |
81
|
1 |
|
|
82
|
2 |
|
$endpoints->useAttributeAsKey('name'); |
83
|
2 |
|
$proto->addDefaultsIfNotSet(); |
84
|
1 |
|
} |
85
|
|
|
} |
86
|
|
|
|