1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the distributed-configuration-bundle package |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016 Guillaume Cavana |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* Feel free to edit as you please, and have fun. |
12
|
|
|
* |
13
|
|
|
* @author Guillaume Cavana <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Maikuro\DistributedConfigurationBundle\DependencyInjection; |
17
|
|
|
|
18
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
19
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
20
|
|
|
|
21
|
|
|
class Configuration implements ConfigurationInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function getConfigTreeBuilder() |
27
|
|
|
{ |
28
|
|
|
$self = $this; |
29
|
|
|
$treeBuilder = new TreeBuilder(); |
30
|
|
|
$rootNode = $treeBuilder->root('maikuro_distributed_configuration'); |
31
|
|
|
|
32
|
|
|
$supportedStores = ['dbal', 'predis', 'redis', 'json']; |
33
|
|
|
|
34
|
|
|
$normalization = function ($conf) use ($self) { |
35
|
|
|
|
36
|
|
|
$conf['type'] = $self->resolveNodeType($conf); |
37
|
|
|
|
38
|
|
|
return $conf; |
39
|
|
|
}; |
40
|
|
|
|
41
|
|
|
$rootNode |
42
|
|
|
->children() |
43
|
|
|
->arrayNode('store') |
44
|
|
|
->beforeNormalization() |
45
|
|
|
->ifTrue(function ($v) { |
46
|
|
|
return !isset($v['type']); |
47
|
|
|
}) |
48
|
|
|
->then($normalization) |
49
|
|
|
->end() |
50
|
|
|
->children() |
51
|
|
|
->scalarNode('type') |
52
|
|
|
->validate() |
53
|
|
|
->ifNotInArray($supportedStores) |
54
|
|
|
->thenInvalid('The store %s is not supported. Please choose one of '.json_encode($supportedStores)) |
55
|
|
|
->end() |
56
|
|
|
->end() |
57
|
|
|
->append($this->addRedisNode()) |
58
|
|
|
->append($this->addPredisNode()) |
59
|
|
|
->append($this->addDbalNode()) |
60
|
|
|
->append($this->addJsonFileNode()) |
61
|
|
|
->end() |
62
|
|
|
->end() |
63
|
|
|
->arrayNode('cache') |
64
|
|
|
->addDefaultsIfNotSet() |
65
|
|
|
->children() |
66
|
|
|
->scalarNode('enabled')->defaultFalse()->end() |
67
|
|
|
->scalarNode('service_id')->defaultNull()->end() |
68
|
|
|
->scalarNode('default_ttl')->defaultValue('3600')->end() |
69
|
|
|
->end() |
70
|
|
|
->end() |
71
|
|
|
->end() |
72
|
|
|
; |
73
|
|
|
|
74
|
|
|
return $treeBuilder; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $parameters |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function resolveNodeType(array $parameters) |
83
|
|
|
{ |
84
|
|
|
if (isset($parameters['type'])) { |
85
|
|
|
unset($parameters['type']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$type = key($parameters); |
89
|
|
|
|
90
|
|
|
return $type; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Build Predis node configuration definition. |
95
|
|
|
* |
96
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder |
97
|
|
|
*/ |
98
|
|
|
private function addPredisNode() |
99
|
|
|
{ |
100
|
|
|
$builder = new TreeBuilder(); |
101
|
|
|
$node = $builder->root('predis'); |
102
|
|
|
$node |
103
|
|
|
->addDefaultsIfNotSet() |
104
|
|
|
->children() |
105
|
|
|
->scalarNode('client_id')->defaultNull()->end() |
106
|
|
|
->scalarNode('scheme')->defaultValue('tcp')->end() |
107
|
|
|
->scalarNode('host')->defaultValue('localhost')->end() |
108
|
|
|
->scalarNode('port')->defaultValue('6379')->end() |
109
|
|
|
->scalarNode('password')->defaultNull()->end() |
110
|
|
|
->scalarNode('timeout')->defaultNull()->end() |
111
|
|
|
->scalarNode('database')->defaultNull()->end() |
112
|
|
|
->arrayNode('options') |
113
|
|
|
->useAttributeAsKey('name') |
114
|
|
|
->prototype('scalar')->end() |
115
|
|
|
->end() |
116
|
|
|
->end() |
117
|
|
|
; |
118
|
|
|
|
119
|
|
|
return $node; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Build JsonFile node configuration definition. |
124
|
|
|
* |
125
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder |
126
|
|
|
*/ |
127
|
|
|
private function addJsonFileNode() |
128
|
|
|
{ |
129
|
|
|
$builder = new TreeBuilder(); |
130
|
|
|
$node = $builder->root('json'); |
131
|
|
|
$node |
132
|
|
|
->addDefaultsIfNotSet() |
133
|
|
|
->children() |
134
|
|
|
->scalarNode('path')->defaultValue('%kernel.root_dir%/configuration.json')->end() |
135
|
|
|
->end() |
136
|
|
|
; |
137
|
|
|
|
138
|
|
|
return $node; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Build dbal node configuration definition. |
143
|
|
|
* |
144
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder |
145
|
|
|
*/ |
146
|
|
|
private function addDbalNode() |
147
|
|
|
{ |
148
|
|
|
$builder = new TreeBuilder(); |
149
|
|
|
$node = $builder->root('dbal'); |
150
|
|
|
$node |
151
|
|
|
->addDefaultsIfNotSet() |
152
|
|
|
->children() |
153
|
|
|
->scalarNode('connection_id')->defaultNull()->end() |
154
|
|
|
->scalarNode('table_name')->defaultValue('configuration')->end() |
155
|
|
|
->end() |
156
|
|
|
; |
157
|
|
|
|
158
|
|
|
return $node; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Build Redis node configuration definition. |
163
|
|
|
* |
164
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\TreeBuilder |
165
|
|
|
*/ |
166
|
|
|
private function addRedisNode() |
167
|
|
|
{ |
168
|
|
|
$builder = new TreeBuilder(); |
169
|
|
|
$node = $builder->root('redis'); |
170
|
|
|
$node |
171
|
|
|
->addDefaultsIfNotSet() |
172
|
|
|
->children() |
173
|
|
|
->scalarNode('connection_id')->defaultNull()->end() |
174
|
|
|
->scalarNode('host')->defaultValue('localhost')->end() |
175
|
|
|
->scalarNode('port')->defaultValue('6379')->end() |
176
|
|
|
->scalarNode('password')->defaultNull()->end() |
177
|
|
|
->scalarNode('timeout')->defaultNull()->end() |
178
|
|
|
->scalarNode('database')->defaultNull()->end() |
179
|
|
|
->booleanNode('persistent')->defaultFalse()->end() |
180
|
|
|
->end() |
181
|
|
|
; |
182
|
|
|
|
183
|
|
|
return $node; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|