1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Psr\Log\LogLevel; |
15
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
16
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
17
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This is the class that validates and merges configuration from app/config files. |
21
|
|
|
*/ |
22
|
|
|
class Configuration implements ConfigurationInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function getConfigTreeBuilder() |
28
|
|
|
{ |
29
|
|
|
$treeBuilder = new TreeBuilder(); |
30
|
|
|
$rootNode = $treeBuilder->root('ongr_elasticsearch'); |
31
|
|
|
|
32
|
|
|
$rootNode |
33
|
|
|
->children() |
34
|
|
|
->booleanNode('cache') |
35
|
|
|
->info( |
36
|
|
|
'Enables cache handler to store metadata and other data to the cache. '. |
37
|
|
|
'Default is false, we recommend to enable it in the production.' |
38
|
|
|
) |
39
|
|
|
->defaultFalse() |
40
|
|
|
->end() |
41
|
|
|
->append($this->getAnalysisNode()) |
42
|
|
|
->append($this->getConnectionsNode()) |
43
|
|
|
->append($this->getManagersNode()) |
44
|
|
|
->end(); |
45
|
|
|
|
46
|
|
|
return $treeBuilder; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Analysis configuration node. |
51
|
|
|
* |
52
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\NodeDefinition |
53
|
|
|
*/ |
54
|
|
|
private function getAnalysisNode() |
55
|
|
|
{ |
56
|
|
|
$builder = new TreeBuilder(); |
57
|
|
|
$node = $builder->root('analysis'); |
58
|
|
|
|
59
|
|
|
$node |
|
|
|
|
60
|
|
|
->info('Defines analyzers, tokenizers and filters') |
61
|
|
|
->addDefaultsIfNotSet() |
62
|
|
|
->children() |
63
|
|
|
->arrayNode('tokenizer') |
64
|
|
|
->defaultValue([]) |
65
|
|
|
->prototype('variable')->end() |
66
|
|
|
->end() |
67
|
|
|
->arrayNode('filter') |
68
|
|
|
->defaultValue([]) |
69
|
|
|
->prototype('variable')->end() |
70
|
|
|
->end() |
71
|
|
|
->arrayNode('analyzer') |
72
|
|
|
->defaultValue([]) |
73
|
|
|
->prototype('variable')->end() |
74
|
|
|
->end() |
75
|
|
|
->end(); |
76
|
|
|
|
77
|
|
|
return $node; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Connections configuration node. |
82
|
|
|
* |
83
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\NodeDefinition |
84
|
|
|
* |
85
|
|
|
* @throws InvalidConfigurationException |
86
|
|
|
*/ |
87
|
|
|
private function getConnectionsNode() |
88
|
|
|
{ |
89
|
|
|
$builder = new TreeBuilder(); |
90
|
|
|
$node = $builder->root('connections'); |
91
|
|
|
|
92
|
|
|
$node |
|
|
|
|
93
|
|
|
->isRequired() |
94
|
|
|
->requiresAtLeastOneElement() |
95
|
|
|
->info('Defines connections to indexes and its settings.') |
96
|
|
|
->prototype('array') |
97
|
|
|
->children() |
98
|
|
|
->arrayNode('hosts') |
99
|
|
|
->info('Defines hosts to connect to.') |
100
|
|
|
->defaultValue(['127.0.0.1:9200']) |
101
|
|
|
->prototype('scalar') |
102
|
|
|
->end() |
103
|
|
|
->end() |
104
|
|
|
->arrayNode('auth') |
105
|
|
|
->info('holds information for http authentication.') |
106
|
|
|
->children() |
107
|
|
|
->scalarNode('username') |
108
|
|
|
->isRequired() |
109
|
|
|
->example('john') |
110
|
|
|
->end() |
111
|
|
|
->scalarNode('password') |
112
|
|
|
->isRequired() |
113
|
|
|
->example('mytopsecretpassword') |
114
|
|
|
->end() |
115
|
|
|
->scalarNode('option') |
116
|
|
|
->defaultValue('Basic') |
117
|
|
|
->info('authentication type') |
118
|
|
|
->end() |
119
|
|
|
->end() |
120
|
|
|
->end() |
121
|
|
|
->scalarNode('index_name') |
122
|
|
|
->isRequired() |
123
|
|
|
->info('Sets index name for connection.') |
124
|
|
|
->end() |
125
|
|
|
->arrayNode('settings') |
126
|
|
|
->defaultValue([]) |
127
|
|
|
->info('Sets index settings for connection.') |
128
|
|
|
->prototype('variable')->end() |
129
|
|
|
->end() |
130
|
|
|
->arrayNode('analysis') |
131
|
|
|
->addDefaultsIfNotSet() |
132
|
|
|
->info('Sets index analysis settings for connection.') |
133
|
|
|
->children() |
134
|
|
|
->arrayNode('tokenizer')->prototype('scalar')->defaultValue([])->end()->end() |
135
|
|
|
->arrayNode('filter')->prototype('scalar')->defaultValue([])->end()->end() |
136
|
|
|
->arrayNode('analyzer')->prototype('scalar')->defaultValue([])->end()->end() |
137
|
|
|
->end() |
138
|
|
|
->end() |
139
|
|
|
->end() |
140
|
|
|
->end(); |
141
|
|
|
|
142
|
|
|
return $node; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Managers configuration node. |
147
|
|
|
* |
148
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\NodeDefinition |
149
|
|
|
*/ |
150
|
|
|
private function getManagersNode() |
151
|
|
|
{ |
152
|
|
|
$builder = new TreeBuilder(); |
153
|
|
|
$node = $builder->root('managers'); |
154
|
|
|
|
155
|
|
|
$node |
|
|
|
|
156
|
|
|
->isRequired() |
157
|
|
|
->requiresAtLeastOneElement() |
158
|
|
|
->info('Maps managers to connections and bundles') |
159
|
|
|
->prototype('array') |
160
|
|
|
->children() |
161
|
|
|
->scalarNode('connection') |
162
|
|
|
->isRequired() |
163
|
|
|
->info('Sets connection for manager.') |
164
|
|
|
->end() |
165
|
|
|
->integerNode('bulk_size') |
166
|
|
|
->min(0) |
167
|
|
|
->defaultValue(100) |
168
|
|
|
->info( |
169
|
|
|
'Maximum documents size in the bulk container. ' . |
170
|
|
|
'When the limit is reached it will auto-commit.' |
171
|
|
|
) |
172
|
|
|
->end() |
173
|
|
|
->enumNode('commit_mode') |
174
|
|
|
->values(['refresh', 'flush', 'none']) |
175
|
|
|
->defaultValue('refresh') |
176
|
|
|
->info( |
177
|
|
|
'The type of commit to the elasticsearch' |
178
|
|
|
) |
179
|
|
|
->end() |
180
|
|
|
->arrayNode('logger') |
181
|
|
|
->info('Enables elasticsearch queries logging') |
182
|
|
|
->addDefaultsIfNotSet() |
183
|
|
|
->beforeNormalization() |
184
|
|
|
->ifTrue( |
185
|
|
|
function ($v) { |
186
|
|
|
return is_bool($v); |
187
|
|
|
} |
188
|
|
|
) |
189
|
|
|
->then( |
190
|
|
|
function ($v) { |
191
|
|
|
return ['enabled' => $v]; |
192
|
|
|
} |
193
|
|
|
) |
194
|
|
|
->end() |
195
|
|
|
->children() |
196
|
|
|
->booleanNode('enabled') |
197
|
|
|
->info('enables logging') |
198
|
|
|
->defaultFalse() |
199
|
|
|
->end() |
200
|
|
|
->scalarNode('level') |
201
|
|
|
->info('Sets PSR logging level') |
202
|
|
|
->defaultValue(LogLevel::WARNING) |
203
|
|
|
->validate() |
204
|
|
|
->ifNotInArray((new \ReflectionClass('Psr\Log\LogLevel'))->getConstants()) |
205
|
|
|
->thenInvalid('Invalid PSR log level.') |
206
|
|
|
->end() |
207
|
|
|
->end() |
208
|
|
|
->scalarNode('log_file_name') |
209
|
|
|
->info('Log filename, by default it is a manager name') |
210
|
|
|
->defaultValue(null) |
211
|
|
|
->end() |
212
|
|
|
->end() |
213
|
|
|
->end() |
214
|
|
|
->arrayNode('mappings') |
215
|
|
|
->info('Maps manager to bundles. f.e. AcmeDemoBundle') |
216
|
|
|
->prototype('scalar')->end() |
217
|
|
|
->end() |
218
|
|
|
->end() |
219
|
|
|
->end(); |
220
|
|
|
|
221
|
|
|
return $node; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: