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
|
|
|
->end(); |
26
|
|
|
|
27
|
|
|
return $builder; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function addExtractorNode(): NodeDefinition |
31
|
|
|
{ |
32
|
|
|
$builder = new TreeBuilder(); |
33
|
|
|
$root = $builder->root('extractor'); |
34
|
|
|
$root |
35
|
|
|
->addDefaultsIfNotSet() |
36
|
|
|
->beforeNormalization() |
37
|
|
|
->ifString() |
38
|
|
|
->then(function ($extractorName) { |
39
|
|
|
return ['service_id' => 'eps.req2cmd.extractor.' . $extractorName]; |
40
|
|
|
}) |
41
|
|
|
->end() |
42
|
|
|
->children() |
43
|
|
|
->scalarNode('service_id') |
44
|
|
|
->cannotBeEmpty() |
45
|
|
|
->defaultValue('eps.req2cmd.extractor.serializer') |
46
|
|
|
->end() |
47
|
|
|
->end(); |
48
|
|
|
|
49
|
|
|
return $root; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function addCommandBusNode(): NodeDefinition |
53
|
|
|
{ |
54
|
|
|
$builder = new TreeBuilder(); |
55
|
|
|
$node = $builder->root('command_bus'); |
56
|
|
|
$node |
|
|
|
|
57
|
|
|
->addDefaultsIfNotSet() |
58
|
|
|
->beforeNormalization() |
59
|
|
|
->ifString() |
60
|
|
|
->then(function (string $svcId) { |
61
|
|
|
return ['service_id' => 'eps.req2cmd.command_bus.' . $svcId]; |
62
|
|
|
}) |
63
|
|
|
->end() |
64
|
|
|
->children() |
65
|
|
|
->scalarNode('service_id') |
66
|
|
|
->cannotBeEmpty() |
67
|
|
|
->defaultValue('eps.req2cmd.command_bus.tactician') |
68
|
|
|
->end() |
69
|
|
|
->scalarNode('name') |
70
|
|
|
->cannotBeEmpty() |
71
|
|
|
->defaultValue('default') |
72
|
|
|
->end() |
73
|
|
|
->end() |
74
|
|
|
->validate() |
75
|
|
|
->ifTrue(function ($config) { |
76
|
|
|
return $config['service_id'] !== 'eps.req2cmd.command_bus.tactician'; |
77
|
|
|
}) |
78
|
|
|
->then(function ($config) { |
79
|
|
|
unset($config['name']); |
80
|
|
|
return $config; |
81
|
|
|
}) |
82
|
|
|
->end(); |
83
|
|
|
|
84
|
|
|
return $node; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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: