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
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function getConfigTreeBuilder() |
25
|
|
|
{ |
26
|
|
|
$treeBuilder = new TreeBuilder('core23_shariff'); |
27
|
|
|
|
28
|
|
|
// Keep compatibility with symfony/config < 4.2 |
29
|
|
|
if (!method_exists($treeBuilder, 'getRootNode')) { |
30
|
|
|
$rootNode = $treeBuilder->root('core23_shariff'); |
31
|
|
|
} else { |
32
|
|
|
$rootNode = $treeBuilder->getRootNode(); |
33
|
|
|
} |
34
|
|
|
\assert($rootNode instanceof ArrayNodeDefinition); |
35
|
|
|
|
36
|
|
|
$this->addOptions($rootNode); |
37
|
|
|
$this->addServices($rootNode); |
38
|
|
|
|
39
|
|
|
return $treeBuilder; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param NodeDefinition $node |
44
|
|
|
*/ |
45
|
|
|
private function addOptions(NodeDefinition $node): void |
46
|
|
|
{ |
47
|
|
|
$node |
|
|
|
|
48
|
|
|
->children() |
49
|
|
|
->arrayNode('options') |
50
|
|
|
->fixXmlConfig('domain') |
51
|
|
|
->fixXmlConfig('service') |
52
|
|
|
->addDefaultsIfNotSet() |
53
|
|
|
->children() |
54
|
|
|
->arrayNode('domains') |
55
|
|
|
->defaultValue([]) |
56
|
|
|
->prototype('scalar')->end() |
57
|
|
|
->end() |
58
|
|
|
->arrayNode('services') |
59
|
|
|
->defaultValue(['GooglePlus', 'Facebook', 'LinkedIn', 'Reddit', 'StumbleUpon', 'Flattr', 'Pinterest', 'Xing', 'AddThis']) |
60
|
|
|
->prototype('scalar')->end() |
61
|
|
|
->end() |
62
|
|
|
->end() |
63
|
|
|
->end() |
64
|
|
|
->end() |
65
|
|
|
; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param NodeDefinition $node |
70
|
|
|
*/ |
71
|
|
|
private function addServices(NodeDefinition $node): void |
72
|
|
|
{ |
73
|
|
|
$node |
|
|
|
|
74
|
|
|
->children() |
75
|
|
|
->arrayNode('services') |
76
|
|
|
->children() |
77
|
|
|
->arrayNode('facebook') |
78
|
|
|
->children() |
79
|
|
|
->scalarNode('app_id')->end() |
80
|
|
|
->scalarNode('secret')->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: