forked from tedious/TedivmStashBundle
/
Configuration.php
298 lines (272 loc) · 10.8 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
<?php
/*
* This file is part of the StashBundle package.
*
* (c) Josh Hall-Bachner <jhallbachner@gmail.com>
* (c) Robert Hafner <tedivm@tedivm.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tedivm\StashBundle\DependencyInjection;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Stash\DriverList;
/**
* Class Configuration
* @package Tedivm\StashBundle\DependencyInjection
* @author Josh Hall-Bachner <jhallbachner@gmail.com>
* @author Robert Hafner <tedivm@tedivm.com>
*/
class Configuration implements ConfigurationInterface
{
/**
* Default settings for various drivers.
*
* @var array
*/
protected $driverSettings = array(
'FileSystem' => array(
'dirSplit' => 2,
'path' => '%kernel.cache_dir%/stash',
'filePermissions' => 0660,
'dirPermissions' => 0770,
'memKeyLimit' => 200,
'keyHashFunction' => 'md5',
'encoder' => 'Native'
),
'SQLite' => array(
'filePermissions' => 0660,
'dirPermissions' => 0770,
'busyTimeout' => 500,
'nesting' => 0,
'subhandler' => 'PDO',
'version' => null,
'path' => '%kernel.cache_dir%/stash',
),
'Apc' => array(
'ttl' => 300,
'namespace' => null,
),
);
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('stash');
$rootNode
->beforeNormalization()
->ifTrue(function ($v) { return is_array($v) && !array_key_exists('default_cache', $v) && array_key_exists('caches', $v); })
->then(function ($v) {
return Configuration::normalizeDefaultCacheConfig($v);
})
->end()
->beforeNormalization()
->ifTrue(function ($v) { return is_array($v) && !array_key_exists('caches', $v) && !array_key_exists('cache', $v); })
->then(function ($v) {
return Configuration::normalizeCacheConfig($v);
})
->end()
->children()
->scalarNode('default_cache')->end()
->booleanNode('tracking')->end()
->booleanNode('tracking_values')->defaultTrue()->end()
->end()
->fixXmlConfig('cache')
->append($this->getCachesNode())
;
return $treeBuilder;
}
/**
* Creates the driver configuration tree.
*
* @return \Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition
*/
public function getCachesNode()
{
$drivers = array_keys(DriverList::getAvailableDrivers());
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('caches');
$childNode = $node
->fixXmlConfig('handler')
->fixXmlConfig('driver')
->beforeNormalization()
->ifTrue(function ($v) { return is_array($v) && array_key_exists('handlers', $v); })
->then(function ($v) {
return Configuration::normalizeHandlerToDriverConfig($v);
})
->end()
->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->prototype('array')
->children()
->arrayNode('drivers')
->requiresAtLeastOneElement()
->defaultValue(array('FileSystem'))
->prototype('scalar')
->validate()
->ifNotInArray($drivers)
->thenInvalid('A driver of name %s is not registered.')
->end()
->end()
->end()
->booleanNode('registerDoctrineAdapter')->defaultFalse()->end()
->booleanNode('registerSessionHandler')->defaultFalse()->end()
->booleanNode('inMemory')->defaultTrue()->end()
->scalarNode('logger')->defaultNull()->end()
;
foreach ($drivers as $driver) {
if ($driver !== 'Composite') {
$this->addDriverSettings($driver, $childNode);
}
}
$childNode->end()
;
return $node;
}
/**
* @param string $driver
* @param NodeBuilder $rootNode
*/
public function addDriverSettings($driver, NodeBuilder $rootNode)
{
$driverNode = $rootNode
->arrayNode($driver)
->fixXmlConfig('server');
if ($driver == 'Memcache') {
$finalNode = $driverNode
->info('All options except "servers" are Memcached options. See http://www.php.net/manual/en/memcached.constants.php')
->addDefaultsIfNotSet()
->children()
->booleanNode('compression')->end()
->scalarNode('serializer')->end()
->scalarNode('prefix_key')->end()
->scalarNode('hash')->end()
->scalarNode('distribution')->end()
->booleanNode('libketama_compatible')->end()
->booleanNode('buffer_writes')->end()
->booleanNode('binary_protocol')->end()
->booleanNode('no_block')->end()
->booleanNode('tcp_nodelay')->end()
->booleanNode('auto_eject_hosts')->end()
->booleanNode('remove_failed_servers')->end()
->scalarNode('socket_send_size')->end()
->scalarNode('socket_recv_size')->end()
->scalarNode('connect_timeout')->end()
->scalarNode('retry_timeout')->end()
->scalarNode('send_timeout')->end()
->scalarNode('recv_timeout')->end()
->scalarNode('poll_timeout')->end()
->booleanNode('cache_lookups')->end()
->scalarNode('server_failure_limit')->end()
->arrayNode('servers')
->info('Your Memcached server(s) configuration.')
->requiresAtLeastOneElement()
->example(array(array('server' => '127.0.0.1', 'port' => '11211')))
->defaultValue(array(array('server' => '127.0.0.1', 'port' => '11211')))
->prototype('array')
->children()
->scalarNode('server')->defaultValue('127.0.0.1')->end()
->scalarNode('port')->defaultValue('11211')->end()
->scalarNode('weight')->end()
->end()
->end()
->end()
->end()
;
} elseif ($driver == 'Redis') {
$finalNode = $driverNode
->info("Accepts server info, password, and database.")
->addDefaultsIfNotSet()
->children()
->scalarNode('password')->end()
->scalarNode('database')->end()
->arrayNode('servers')
->info('Configuration of Redis server(s)')
->requiresAtLeastOneElement()
->example(array(array('server' => '127.0.0.1', 'port' => '6379')))
->defaultValue(array(array('server' => '127.0.0.1', 'port' => '6379')))
->prototype('array')
->children()
->scalarNode('server')->defaultValue('127.0.0.1')->end()
->scalarNode('port')->defaultValue('6379')->end()
->scalarNode('ttl')->end()
->booleanNode('socket')->end()
->end()
->end()
->end()
->end()
;
} else {
$defaults = isset($this->driverSettings[$driver]) ? $this->driverSettings[$driver] : array();
$node = $driverNode
->addDefaultsIfNotSet()
->children();
foreach ($defaults as $setting => $default) {
$node
->scalarNode($setting)
->defaultValue($default)
->end()
;
}
$finalNode = $node->end()
;
}
$finalNode->end()
;
}
/**
* Used to normalize configuration values.
*
* This function allows users to define a caching system on the first level, without a name, when there is only a
* single cache being used. This makes for less nesting and a prettier config, especially since most cases will
* only involve a single caching system anyways.
*
* @param array $v
* @return array
*/
public static function normalizeCacheConfig($v)
{
$cache = array();
foreach ($v as $key => $value) {
if (in_array($key, array('default_cache', 'tracking', 'tracking_values'))) {
continue;
}
$cache[$key] = $v[$key];
unset($v[$key]);
}
$v['default_cache'] = isset($v['default_cache']) ? (string) $v['default_cache'] : 'default';
$v['caches'] = array($v['default_cache'] => $cache);
return $v;
}
/**
* Used to normalize configuration values.
*
* Sets the first defined cache as the default when the user hasn't explicitly set one.
*
* @param array $v
* @return array
*/
public static function normalizeDefaultCacheConfig($v)
{
$names = array_keys($v['caches']);
$v['default_cache'] = reset($names);
return $v;
}
/**
* Used to make configurations from <0.4 compatibility with the new version.
*
* This converts the old "handlers" field into the new "drivers" field.
*
* @param array $v
* @return array
*/
public static function normalizeHandlerToDriverConfig($v)
{
$v['drivers'] = $v['handlers'];
return $v;
}
}