1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facile\MongoDbBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeBuilder; |
6
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
7
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Configuration. |
11
|
|
|
* |
12
|
|
|
* @internal |
13
|
|
|
*/ |
14
|
|
|
final class Configuration implements ConfigurationInterface |
15
|
|
|
{ |
16
|
|
|
const READ_PREFERENCE_VALID_OPTIONS = ['primary', 'primaryPreferred', 'secondary', 'secondaryPreferred', 'nearest']; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
13 |
|
public function getConfigTreeBuilder() |
22
|
|
|
{ |
23
|
13 |
|
$treeBuilder = new TreeBuilder('mongo_db_bundle'); |
24
|
13 |
|
$rootBuilder = \method_exists(TreeBuilder::class, 'getRootNode') |
25
|
|
|
? $treeBuilder->getRootNode() |
26
|
13 |
|
: $treeBuilder->root('mongo_db_bundle'); |
|
|
|
|
27
|
13 |
|
|
28
|
13 |
|
$this->addDataCollection($rootBuilder->children()); |
29
|
|
|
$this->addClients($rootBuilder->children()); |
30
|
13 |
|
$this->addConnections($rootBuilder->children()); |
31
|
|
|
$this->addDriversOptionFactory($rootBuilder->children()); |
32
|
|
|
|
33
|
13 |
|
return $treeBuilder; |
34
|
|
|
} |
35
|
|
|
|
36
|
13 |
|
private function addDataCollection(NodeBuilder $builder) |
37
|
13 |
|
{ |
38
|
13 |
|
$builder |
39
|
13 |
|
->booleanNode('data_collection') |
40
|
|
|
->defaultTrue() |
41
|
13 |
|
->info('Disables Data Collection if needed'); |
42
|
|
|
} |
43
|
|
|
|
44
|
13 |
|
private function addClients(NodeBuilder $builder) |
45
|
13 |
|
{ |
46
|
13 |
|
$clientsBuilder = $builder |
47
|
13 |
|
->arrayNode('clients') |
48
|
13 |
|
->isRequired() |
49
|
13 |
|
->requiresAtLeastOneElement() |
50
|
|
|
->useAttributeAsKey('name') |
51
|
13 |
|
->prototype('array') |
52
|
|
|
->children(); |
53
|
|
|
|
54
|
13 |
|
$this->addClientsHosts($clientsBuilder); |
55
|
13 |
|
|
56
|
13 |
|
$clientsBuilder |
57
|
|
|
->scalarNode('uri') |
58
|
|
|
->defaultNull() |
59
|
13 |
|
->info('Overrides hosts configuration'); |
60
|
13 |
|
|
61
|
|
|
$clientsBuilder |
62
|
|
|
->scalarNode('username') |
63
|
13 |
|
->defaultValue(''); |
64
|
13 |
|
|
65
|
|
|
$clientsBuilder |
66
|
|
|
->scalarNode('password') |
67
|
13 |
|
->defaultValue(''); |
68
|
13 |
|
|
69
|
13 |
|
$clientsBuilder |
70
|
|
|
->scalarNode('authSource') |
71
|
|
|
->defaultNull() |
72
|
13 |
|
->info('Database name associated with the user’s credentials'); |
73
|
13 |
|
|
74
|
13 |
|
$clientsBuilder |
75
|
13 |
|
->scalarNode('readPreference') |
76
|
13 |
|
->defaultValue('primaryPreferred') |
77
|
|
|
->validate() |
78
|
|
|
->ifNotInArray(self::READ_PREFERENCE_VALID_OPTIONS) |
79
|
13 |
|
->thenInvalid('Invalid readPreference option %s, must be one of [' . implode(', ', self::READ_PREFERENCE_VALID_OPTIONS) . ']'); |
80
|
13 |
|
|
81
|
|
|
$clientsBuilder |
82
|
|
|
->scalarNode('replicaSet') |
83
|
13 |
|
->defaultNull(); |
84
|
13 |
|
|
85
|
|
|
$clientsBuilder |
86
|
|
|
->booleanNode('ssl') |
87
|
13 |
|
->defaultFalse(); |
88
|
13 |
|
|
89
|
13 |
|
$clientsBuilder |
90
|
|
|
->integerNode('connectTimeoutMS') |
91
|
13 |
|
->defaultNull(); |
92
|
|
|
} |
93
|
|
|
|
94
|
13 |
|
private function addClientsHosts(NodeBuilder $builder) |
95
|
13 |
|
{ |
96
|
13 |
|
$hostsBuilder = $builder |
97
|
13 |
|
->arrayNode('hosts') |
98
|
|
|
->info('Hosts addresses and ports') |
99
|
|
|
->prototype('array') |
100
|
13 |
|
->children(); |
101
|
13 |
|
|
102
|
|
|
$hostsBuilder |
103
|
|
|
->scalarNode('host') |
104
|
13 |
|
->isRequired(); |
105
|
13 |
|
|
106
|
13 |
|
$hostsBuilder |
107
|
|
|
->integerNode('port') |
108
|
13 |
|
->defaultValue(27017); |
109
|
|
|
} |
110
|
|
|
|
111
|
13 |
|
private function addDriverOptions(NodeBuilder $builder) |
|
|
|
|
112
|
13 |
|
{ |
113
|
13 |
|
$driverOptionsBuilder = $builder |
114
|
13 |
|
->arrayNode('driverOptions') |
115
|
13 |
|
->info('Available options for driver') |
116
|
13 |
|
->children(); |
117
|
|
|
|
118
|
|
|
$driverOptionsBuilder |
119
|
13 |
|
->variableNode('context') |
120
|
13 |
|
->defaultValue([]) |
121
|
13 |
|
->info('Service id name'); |
122
|
|
|
} |
123
|
|
|
|
124
|
13 |
|
private function addDriversOptionFactory(NodeBuilder $builder) |
125
|
13 |
|
{ |
126
|
13 |
|
$connectionBuilder = $builder |
|
|
|
|
127
|
13 |
|
->scalarNode('driverOptions'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function addConnections(NodeBuilder $builder) |
131
|
|
|
{ |
132
|
|
|
$connectionBuilder = $builder |
133
|
|
|
->arrayNode('connections') |
134
|
|
|
->isRequired() |
135
|
|
|
->requiresAtLeastOneElement() |
136
|
|
|
->useAttributeAsKey('name') |
137
|
|
|
->prototype('array') |
138
|
|
|
->children(); |
139
|
|
|
|
140
|
|
|
$connectionBuilder |
141
|
|
|
->scalarNode('client_name') |
142
|
|
|
->isRequired() |
143
|
|
|
->info('Desired client name'); |
144
|
|
|
|
145
|
|
|
$connectionBuilder |
146
|
|
|
->scalarNode('database_name') |
147
|
|
|
->isRequired() |
148
|
|
|
->info('Database name'); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.