1 | <?php |
||
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() |
||
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: