|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Eps\Req2CmdBundle\DependencyInjection; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
|
7
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
|
8
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
|
9
|
|
|
|
|
10
|
|
|
final class Req2CmdConfiguration implements ConfigurationInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritdoc} |
|
14
|
|
|
* @throws \RuntimeException |
|
15
|
|
|
*/ |
|
16
|
|
|
public function getConfigTreeBuilder(): TreeBuilder |
|
17
|
|
|
{ |
|
18
|
|
|
$builder = new TreeBuilder(); |
|
19
|
|
|
|
|
20
|
|
|
$root = $builder->root('req2cmd'); |
|
21
|
|
|
$root |
|
22
|
|
|
->children() |
|
23
|
|
|
->append($this->addExtractorNode()) |
|
24
|
|
|
->append($this->addCommandBusNode()) |
|
25
|
|
|
->append($this->addEventListenersNode()) |
|
26
|
|
|
->end(); |
|
27
|
|
|
|
|
28
|
|
|
return $builder; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private function addExtractorNode(): NodeDefinition |
|
32
|
|
|
{ |
|
33
|
|
|
$builder = new TreeBuilder(); |
|
34
|
|
|
$root = $builder->root('extractor'); |
|
35
|
|
|
$root |
|
36
|
|
|
->addDefaultsIfNotSet() |
|
37
|
|
|
->beforeNormalization() |
|
38
|
|
|
->ifString() |
|
39
|
|
|
->then(function ($extractorName) { |
|
40
|
|
|
return ['service_id' => 'eps.req2cmd.extractor.' . $extractorName]; |
|
41
|
|
|
}) |
|
42
|
|
|
->end() |
|
43
|
|
|
->children() |
|
44
|
|
|
->scalarNode('service_id') |
|
45
|
|
|
->cannotBeEmpty() |
|
46
|
|
|
->defaultValue('eps.req2cmd.extractor.serializer') |
|
47
|
|
|
->end() |
|
48
|
|
|
->end(); |
|
49
|
|
|
|
|
50
|
|
|
return $root; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function addCommandBusNode(): NodeDefinition |
|
54
|
|
|
{ |
|
55
|
|
|
$builder = new TreeBuilder(); |
|
56
|
|
|
$node = $builder->root('command_bus'); |
|
57
|
|
|
$node |
|
|
|
|
|
|
58
|
|
|
->addDefaultsIfNotSet() |
|
59
|
|
|
->beforeNormalization() |
|
60
|
|
|
->ifString() |
|
61
|
|
|
->then(function (string $svcId) { |
|
62
|
|
|
return ['service_id' => 'eps.req2cmd.command_bus.' . $svcId]; |
|
63
|
|
|
}) |
|
64
|
|
|
->end() |
|
65
|
|
|
->children() |
|
66
|
|
|
->scalarNode('service_id') |
|
67
|
|
|
->cannotBeEmpty() |
|
68
|
|
|
->defaultValue('eps.req2cmd.command_bus.tactician') |
|
69
|
|
|
->end() |
|
70
|
|
|
->scalarNode('name') |
|
71
|
|
|
->cannotBeEmpty() |
|
72
|
|
|
->defaultValue('default') |
|
73
|
|
|
->end() |
|
74
|
|
|
->end() |
|
75
|
|
|
->validate() |
|
76
|
|
|
->ifTrue(function ($config) { |
|
77
|
|
|
return $config['service_id'] !== 'eps.req2cmd.command_bus.tactician'; |
|
78
|
|
|
}) |
|
79
|
|
|
->then(function ($config) { |
|
80
|
|
|
unset($config['name']); |
|
81
|
|
|
return $config; |
|
82
|
|
|
}) |
|
83
|
|
|
->end(); |
|
84
|
|
|
|
|
85
|
|
|
return $node; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private function addEventListenersNode(): NodeDefinition |
|
89
|
|
|
{ |
|
90
|
|
|
$builder = new TreeBuilder(); |
|
91
|
|
|
$node = $builder->root('listeners'); |
|
92
|
|
|
|
|
93
|
|
|
$node |
|
94
|
|
|
->addDefaultsIfNotSet() |
|
95
|
|
|
->children() |
|
96
|
|
|
->arrayNode('extractor') |
|
97
|
|
|
->addDefaultsIfNotSet() |
|
98
|
|
|
->beforeNormalization() |
|
99
|
|
|
->ifTrue(function ($enabled) { return is_bool($enabled); }) |
|
100
|
|
|
->then(function ($isEnabled) { |
|
101
|
|
|
return ['enabled' => $isEnabled]; |
|
102
|
|
|
}) |
|
103
|
|
|
->end() |
|
104
|
|
|
->children() |
|
105
|
|
|
->booleanNode('enabled') |
|
106
|
|
|
->defaultValue(true) |
|
107
|
|
|
->end() |
|
108
|
|
|
->integerNode('priority') |
|
109
|
|
|
->defaultValue(0) |
|
110
|
|
|
->end() |
|
111
|
|
|
->end() |
|
112
|
|
|
->end() |
|
113
|
|
|
->end(); |
|
114
|
|
|
|
|
115
|
|
|
return $node; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
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: