1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\ShariffBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
15
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
16
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
17
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
18
|
|
|
|
19
|
|
|
final class Configuration implements ConfigurationInterface |
20
|
|
|
{ |
21
|
|
|
public function getConfigTreeBuilder() |
22
|
|
|
{ |
23
|
|
|
$treeBuilder = new TreeBuilder('core23_shariff'); |
24
|
|
|
|
25
|
|
|
$rootNode = $treeBuilder->getRootNode(); |
26
|
|
|
|
27
|
|
|
\assert($rootNode instanceof ArrayNodeDefinition); |
28
|
|
|
|
29
|
|
|
$this->addAliases($rootNode); |
30
|
|
|
$this->addOptions($rootNode); |
31
|
|
|
$this->addServices($rootNode); |
32
|
|
|
|
33
|
|
|
return $treeBuilder; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function addAliases(NodeDefinition $node): void |
37
|
|
|
{ |
38
|
|
|
$node |
|
|
|
|
39
|
|
|
->children() |
40
|
|
|
->scalarNode('cache')->isRequired()->end() |
41
|
|
|
->scalarNode('http_client')->isRequired()->end() |
42
|
|
|
->scalarNode('request_factory')->isRequired()->end() |
43
|
|
|
->end() |
44
|
|
|
; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function addOptions(NodeDefinition $node): void |
48
|
|
|
{ |
49
|
|
|
$node |
|
|
|
|
50
|
|
|
->children() |
51
|
|
|
->arrayNode('options') |
52
|
|
|
->fixXmlConfig('domain') |
53
|
|
|
->fixXmlConfig('service') |
54
|
|
|
->addDefaultsIfNotSet() |
55
|
|
|
->children() |
56
|
|
|
->arrayNode('domains') |
57
|
|
|
->defaultValue([]) |
58
|
|
|
->prototype('scalar')->end() |
59
|
|
|
->end() |
60
|
|
|
->arrayNode('services') |
61
|
|
|
->defaultValue(['addthis', 'buffer', 'facebook', 'pinterest', 'reddit', 'stumbleupon', 'vk', 'xing']) |
62
|
|
|
->prototype('scalar')->end() |
63
|
|
|
->end() |
64
|
|
|
->end() |
65
|
|
|
->end() |
66
|
|
|
->end() |
67
|
|
|
; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function addServices(NodeDefinition $node): void |
71
|
|
|
{ |
72
|
|
|
$node |
|
|
|
|
73
|
|
|
->children() |
74
|
|
|
->arrayNode('services') |
75
|
|
|
->children() |
76
|
|
|
->arrayNode('facebook') |
77
|
|
|
->children() |
78
|
|
|
->scalarNode('app_id')->end() |
79
|
|
|
->scalarNode('secret')->end() |
80
|
|
|
->scalarNode('version')->end() |
81
|
|
|
->end() |
82
|
|
|
->end() |
83
|
|
|
->end() |
84
|
|
|
->end() |
85
|
|
|
; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
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: