1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the TraitorBundle, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2017 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\Bundle\Traitor\DependencyInjection; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
13
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
14
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
15
|
|
|
|
16
|
|
|
class Configuration implements ConfigurationInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
7 |
|
public function getConfigTreeBuilder() |
22
|
|
|
{ |
23
|
7 |
|
$treeBuilder = new TreeBuilder(); |
24
|
|
|
|
25
|
7 |
|
$rootNode = $treeBuilder->root('runopencode_traitor'); |
26
|
|
|
|
27
|
|
|
$rootNode |
28
|
7 |
|
->addDefaultsIfNotSet() |
29
|
7 |
|
->fixXmlConfig('inject') |
30
|
7 |
|
->children() |
31
|
7 |
|
->booleanNode('use_common_traits') |
32
|
7 |
|
->defaultFalse() |
33
|
7 |
|
->info('For sake of productivity, some of the common Symfony and vendor traits, as well as traits from this library, can be automatically added to "inject" definition.') |
34
|
7 |
|
->end() |
35
|
7 |
|
->append($this->getInjectionsDefinition()) |
36
|
7 |
|
->append($this->getFiltesDefinition()) |
37
|
7 |
|
->append($this->getExclusionsDefinition()) |
38
|
7 |
|
->end(); |
39
|
|
|
|
40
|
7 |
|
return $treeBuilder; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Configure injections |
45
|
|
|
* |
46
|
|
|
* @return ArrayNodeDefinition |
47
|
|
|
*/ |
48
|
7 |
|
protected function getInjectionsDefinition() |
49
|
|
|
{ |
50
|
7 |
|
$node = new ArrayNodeDefinition('injects'); |
51
|
|
|
|
52
|
|
|
$node |
|
|
|
|
53
|
7 |
|
->useAttributeAsKey('trait') |
54
|
7 |
|
->prototype('array') |
55
|
7 |
|
->fixXmlConfig('call', 'call') |
56
|
7 |
|
->children() |
57
|
7 |
|
->arrayNode('call') |
58
|
7 |
|
->prototype('array') |
59
|
7 |
|
->fixXmlConfig('argument') |
60
|
7 |
|
->children() |
61
|
7 |
|
->scalarNode('method')->isRequired()->end() |
62
|
7 |
|
->arrayNode('arguments') |
63
|
7 |
|
->prototype('array') |
64
|
7 |
|
->children() |
65
|
7 |
|
->scalarNode('id')->defaultNull()->end() |
66
|
7 |
|
->scalarNode('type')->defaultValue('string')->end() |
67
|
7 |
|
->variableNode('value')->defaultNull()->end() |
68
|
7 |
|
->end() |
69
|
7 |
|
->end() |
70
|
7 |
|
->end() |
71
|
7 |
|
->end() |
72
|
7 |
|
->end() |
73
|
7 |
|
->end() |
74
|
7 |
|
->end() |
75
|
7 |
|
->end(); |
76
|
|
|
|
77
|
7 |
|
return $node; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Configure filters |
82
|
|
|
* |
83
|
|
|
* @return ArrayNodeDefinition |
84
|
|
|
*/ |
85
|
7 |
|
protected function getFiltesDefinition() |
86
|
|
|
{ |
87
|
7 |
|
$node = new ArrayNodeDefinition('filter'); |
88
|
|
|
|
89
|
|
|
$node |
90
|
7 |
|
->info('Analyse services for injection which matches filter criteria.') |
91
|
7 |
|
->fixXmlConfig('tag') |
92
|
7 |
|
->fixXmlConfig('namespace') |
93
|
7 |
|
->children() |
94
|
7 |
|
->arrayNode('tags') |
95
|
7 |
|
->prototype('scalar')->end() |
96
|
7 |
|
->end() |
97
|
7 |
|
->arrayNode('namespaces') |
98
|
7 |
|
->prototype('scalar')->end() |
99
|
7 |
|
->end() |
100
|
7 |
|
->end(); |
101
|
|
|
|
102
|
7 |
|
return $node; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Configure exclusions |
107
|
|
|
* |
108
|
|
|
* @return ArrayNodeDefinition |
109
|
|
|
*/ |
110
|
7 |
|
protected function getExclusionsDefinition() |
111
|
|
|
{ |
112
|
7 |
|
$node = new ArrayNodeDefinition('exclude'); |
113
|
|
|
|
114
|
|
|
$node |
115
|
7 |
|
->info('Exclude services from injection which matches filter criteria.') |
116
|
7 |
|
->fixXmlConfig('service') |
117
|
7 |
|
->fixXmlConfig('namespace') |
118
|
7 |
|
->fixXmlConfig('class', 'classes') |
119
|
7 |
|
->fixXmlConfig('tag') |
120
|
7 |
|
->children() |
121
|
7 |
|
->arrayNode('services') |
122
|
7 |
|
->prototype('scalar')->end() |
123
|
7 |
|
->end() |
124
|
7 |
|
->arrayNode('namespaces') |
125
|
7 |
|
->prototype('scalar')->end() |
126
|
7 |
|
->end() |
127
|
7 |
|
->arrayNode('classes') |
128
|
7 |
|
->prototype('scalar')->end() |
129
|
7 |
|
->end() |
130
|
7 |
|
->arrayNode('tags') |
131
|
7 |
|
->prototype('scalar')->end() |
132
|
7 |
|
->end() |
133
|
7 |
|
->end(); |
134
|
|
|
|
135
|
7 |
|
return $node; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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: