-
Notifications
You must be signed in to change notification settings - Fork 14
/
Configuration.php
executable file
·338 lines (313 loc) · 11.1 KB
/
Configuration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/**
* @author Aaron Scherer <aequasi@gmail.com>
* @date 2013
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache License, Version 2.0
*/
namespace Aequasi\Bundle\CacheBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* Class Configuration
*
* @author Aaron Scherer <aequasi@gmail.com>
*/
class Configuration implements ConfigurationInterface
{
/**
* @var bool
*/
private $debug;
/**
* Constructor
*
* @param Boolean $debug Whether to use the debug mode
*/
public function __construct($debug)
{
$this->debug = (Boolean)$debug;
}
/**
* Generates the configuration tree builder.
*
* @return TreeBuilder The tree builder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('cache');
$rootNode->children()
->append($this->getClustersNode())
->append($this->addSessionSupportSection())
->append($this->addDoctrineSection())
->append($this->addRouterSection())
->end();
return $treeBuilder;
}
/**
* @return ArrayNodeDefinition
*/
private function getClustersNode()
{
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('instances');
$node
->requiresAtLeastOneElement()
->addDefaultChildrenIfNoneSet('default')
->useAttributeAsKey('name')
->prototype('array')
->children()
->enumNode('type')
->values(array('redis', 'php_file', 'file_system', 'array', 'memcached', 'apc'))
->end()
->scalarNode('id')
->defaultNull()
->end()
->scalarNode('namespace')
->defaultNull()
->info("Namespace for doctrine keys.")
->end()
->integerNode('database')
->defaultNull()
->info("For Redis: Specify what database you want.")
->end()
->scalarNode('persistent')
->defaultNull()
->beforeNormalization()
->ifTrue(
function ($v) {
return $v === 'true' || $v === 'false';
}
)
->then(
function ($v) {
return (bool) $v;
}
)
->end()
->info("For Redis and Memcached: Specify the persistent id if you want persistent connections.")
->end()
->scalarNode('auth_password')
->info("For Redis: Authorization info.")
->end()
->scalarNode('directory')
->info("For File System and PHP File: Directory to store cache.")
->defaultNull()
->end()
->scalarNode('extension')
->info("For File System and PHP File: Extension to use.")
->defaultNull()
->end()
->arrayNode('options')
->info("Options for Redis and Memcached.")
->children()
->append($this->getMemcachedOptions())
->end()
->end()
->arrayNode('hosts')
->prototype('array')
->children()
->scalarNode('host')
->defaultNull()
->end()
->scalarNode('port')
->defaultNull()
->validate()
->ifTrue(
function ($v) {
return !is_null($v) && !is_numeric($v);
}
)
->thenInvalid("Host port must be numeric")
->end()
->end()
->scalarNode('weight')
->info("For Memcached: Weight for given host.")
->defaultNull()
->validate()
->ifTrue(
function ($v) {
return !is_null($v) && !is_numeric($v);
}
)
->thenInvalid('host weight must be numeric')
->end()
->end()
->scalarNode('timeout')
->info("For Redis and Memcache: Timeout for the given host.")
->defaultNull()
->validate()
->ifTrue(
function ($v) {
return !is_null($v) && !is_numeric($v);
}
)
->thenInvalid('host timeout must be numeric')
->end()
->end()
->end()
->end()
->end()
->end()
->end()
;
return $node;
}
/**
* @return ArrayNodeDefinition
*/
private function getMemcachedOptions()
{
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('memcached');
if (class_exists('\Memcached')) {
$node
->children()
->booleanNode('compression')
->end()
->enumNode('serializer')
->values(array('php', 'igbinary', 'json'))
->end()
->scalarNode('prefix_key')
->end()
->enumNode('hash')
->values(array('default', 'md5', 'crc', 'fnv1_64', 'fnv1a_64', 'fnv1_32', 'fnv1a_32', 'hsieh', 'murmur'))
->end()
->enumNode('distribution')
->values(array('modula', 'consistent'))
->end()
->booleanNode('libketama_compatible')
->end()
->booleanNode('uffer_writes')
->end()
->booleanNode('binary_protocol')
->end()
->booleanNode('no_block')
->end()
->booleanNode('tcp_nodelay')
->end()
->integerNode('socket_send_size')
->end()
->integerNode('socket_recv_size')
->end()
->integerNode('connect_timeout')
->end()
->integerNode('retry_timeout')
->end()
->integerNode('send_timeout')
->end()
->integerNode('recv_timeout')
->end()
->integerNode('poll_timeout')
->end()
->booleanNode('cache_lookups')
->end()
->integerNode('server_failure_limit')
->end()
->end();
}
return $node;
}
/**
* Configure the "aequasi_cache.session" section
*
* @return ArrayNodeDefinition
*/
private function addSessionSupportSection()
{
$tree = new TreeBuilder();
$node = $tree->root('session');
$node
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')
->defaultFalse()
->end()
->scalarNode('instance')->end()
->scalarNode('prefix')
->defaultValue("session_")
->end()
->scalarNode('ttl')->end()
->end()
;
return $node;
}
/**
* Configure the "aequasi_cache.doctrine" section
*
* @return ArrayNodeDefinition
*/
private function addDoctrineSection()
{
$tree = new TreeBuilder();
$node = $tree->root('doctrine');
$node
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')
->defaultFalse()
->isRequired()
->end()
->end()
;
$types = array('metadata', 'result', 'query');
foreach ($types as $type) {
$node->children()
->arrayNode($type)
->canBeUnset()
->children()
->scalarNode('instance')
->end()
->arrayNode('entity_managers')
->defaultValue(array())
->beforeNormalization()
->ifString()
->then(
function ($v) {
return (array) $v;
}
)
->end()
->prototype('scalar')
->end()
->end()
->arrayNode('document_managers')
->defaultValue(array())
->beforeNormalization()
->ifString()
->then(
function ($v) {
return (array) $v;
}
)
->end()
->prototype('scalar')
->end()
->end()
->end()
->end();
}
return $node;
}
/**
* Configure the "aequasi_cache.router" section
*
* @return ArrayNodeDefinition
*/
private function addRouterSection()
{
$tree = new TreeBuilder();
$node = $tree->root('router');
$node->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')
->defaultFalse()
->end()
->scalarNode('instance')
->defaultNull()
->end()
->end();
return $node;
}
}