1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\TranslationBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
6
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* This is the class that validates and merges configuration from your app/config files. |
10
|
|
|
*/ |
11
|
|
|
class Configuration implements ConfigurationInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
1 |
|
public function getConfigTreeBuilder() |
17
|
|
|
{ |
18
|
1 |
|
$treeBuilder = new TreeBuilder(); |
19
|
1 |
|
$root = $treeBuilder->root('happyr_translation'); |
20
|
|
|
|
21
|
1 |
|
$root->children() |
22
|
1 |
|
->enumNode('translation_service')->values(array('blackhole', 'filesystem', 'loco'))->defaultValue('loco')->end() |
23
|
1 |
|
->scalarNode('target_dir')->defaultValue('%kernel.root_dir%/Resources/translations')->end() |
24
|
1 |
|
->scalarNode('httplug_client')->defaultValue('httplug.client')->cannotBeEmpty()->end() |
25
|
1 |
|
->scalarNode('httplug_message_factory')->defaultValue('httplug.message_factory')->cannotBeEmpty()->end() |
26
|
1 |
|
->booleanNode('sync_empty_translations')->defaultTrue()->end() |
27
|
1 |
|
->booleanNode('auto_add_assets')->defaultFalse()->end() |
28
|
1 |
|
->booleanNode('allow_edit')->defaultTrue()->end() |
29
|
1 |
|
->enumNode('file_extension')->values(array('csv', 'ini', 'json', 'mo', 'php', 'po', 'qt', 'yml', 'xlf'))->defaultValue('xlf')->end() |
30
|
1 |
|
->arrayNode('locales') |
31
|
1 |
|
->requiresAtLeastOneElement() |
32
|
1 |
|
->prototype('scalar')->end() |
33
|
1 |
|
->end() |
34
|
1 |
|
->arrayNode('domains') |
35
|
1 |
|
->requiresAtLeastOneElement() |
36
|
1 |
|
->prototype('scalar')->end() |
37
|
1 |
|
->end() |
38
|
1 |
|
->append($this->getProjectNode()) |
39
|
1 |
|
->end(); |
40
|
|
|
|
41
|
1 |
|
return $treeBuilder; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return \Symfony\Component\Config\Definition\Builder\NodeDefinition |
46
|
|
|
*/ |
47
|
1 |
|
private function getProjectNode() |
48
|
|
|
{ |
49
|
1 |
|
$treeBuilder = new TreeBuilder(); |
50
|
1 |
|
$node = $treeBuilder->root('projects'); |
51
|
|
|
$node |
|
|
|
|
52
|
1 |
|
->useAttributeAsKey('name') |
53
|
1 |
|
->prototype('array') |
54
|
1 |
|
->children() |
55
|
1 |
|
->scalarNode('api_key')->isRequired()->end() |
56
|
1 |
|
->arrayNode('locales') |
57
|
1 |
|
->requiresAtLeastOneElement() |
58
|
1 |
|
->prototype('scalar')->end() |
59
|
1 |
|
->end() |
60
|
1 |
|
->arrayNode('domains') |
61
|
1 |
|
->requiresAtLeastOneElement() |
62
|
1 |
|
->prototype('scalar')->end() |
63
|
1 |
|
->end() |
64
|
1 |
|
->end() |
65
|
1 |
|
->end(); |
66
|
|
|
|
67
|
1 |
|
return $node; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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: