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\MenuBundle\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
|
|
|
use Symfony\Component\Config\Definition\NodeInterface; |
19
|
|
|
|
20
|
|
|
final class Configuration implements ConfigurationInterface |
21
|
|
|
{ |
22
|
|
|
public function getConfigTreeBuilder() |
23
|
|
|
{ |
24
|
|
|
$treeBuilder = new TreeBuilder('core23_menu'); |
25
|
|
|
|
26
|
|
|
$rootNode = $treeBuilder->getRootNode(); |
27
|
|
|
|
28
|
|
|
\assert($rootNode instanceof ArrayNodeDefinition); |
29
|
|
|
|
30
|
|
|
$this->addMenuSection($rootNode); |
31
|
|
|
|
32
|
|
|
return $treeBuilder; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function addMenuSection(ArrayNodeDefinition $node): void |
36
|
|
|
{ |
37
|
|
|
$menuNode = $node |
38
|
|
|
->children() |
39
|
|
|
->arrayNode('groups') |
40
|
|
|
->useAttributeAsKey('id') |
41
|
|
|
->prototype('array') |
42
|
|
|
->fixXmlConfig('attribute') |
43
|
|
|
->fixXmlConfig('item') |
44
|
|
|
->addDefaultsIfNotSet() |
45
|
|
|
->children() |
46
|
|
|
->scalarNode('name')->defaultNull()->end() |
47
|
|
|
->arrayNode('attributes') |
48
|
|
|
->useAttributeAsKey('id') |
49
|
|
|
->defaultValue([]) |
50
|
|
|
->prototype('scalar')->end() |
51
|
|
|
->end() |
52
|
|
|
->arrayNode('items') |
53
|
|
|
->useAttributeAsKey('id') |
54
|
|
|
->defaultValue([]) |
55
|
|
|
->prototype('array') |
56
|
|
|
; |
57
|
|
|
|
58
|
|
|
$this->buildPathNode($menuNode); |
59
|
|
|
|
60
|
|
|
$menuNode |
61
|
|
|
->end() |
62
|
|
|
->end() |
63
|
|
|
->end() |
64
|
|
|
->end() |
65
|
|
|
; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function buildPathNode(NodeDefinition $node): void |
69
|
|
|
{ |
70
|
|
|
$node |
|
|
|
|
71
|
|
|
->addDefaultsIfNotSet() |
72
|
|
|
->children() |
73
|
|
|
->scalarNode('label')->defaultNull()->end() |
74
|
|
|
->scalarNode('label_catalogue')->defaultFalse()->end() |
75
|
|
|
->scalarNode('icon')->defaultNull()->end() |
76
|
|
|
->scalarNode('class')->defaultNull()->end() |
77
|
|
|
->scalarNode('route')->defaultNull()->end() |
78
|
|
|
->arrayNode('routeParams') |
79
|
|
|
->defaultValue([]) |
80
|
|
|
->useAttributeAsKey('param') |
81
|
|
|
->prototype('scalar')->end() |
82
|
|
|
->validate()->ifTrue(static function ($element) { |
83
|
|
|
return !\is_array($element); |
84
|
|
|
})->thenInvalid('The routeParams element must be an array.')->end() |
85
|
|
|
->end() |
86
|
|
|
->variableNode('children') |
87
|
|
|
->defaultValue([]) |
88
|
|
|
->validate()->ifTrue(static function ($element) { |
89
|
|
|
return !\is_array($element); |
90
|
|
|
})->thenInvalid('The children element must be an array.')->end() |
91
|
|
|
->validate()->always(function ($children) { |
92
|
|
|
array_walk($children, [$this, 'evaluateChildren']); |
93
|
|
|
|
94
|
|
|
return $children; |
95
|
|
|
})->end() |
96
|
|
|
->end() |
97
|
|
|
->end() |
98
|
|
|
; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function evaluateChildren(array &$child, string $name): void |
102
|
|
|
{ |
103
|
|
|
$child = $this->getPathNode($name)->finalize($child); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private function getPathNode(string $name = ''): NodeInterface |
107
|
|
|
{ |
108
|
|
|
$treeBuilder = new TreeBuilder($name); |
109
|
|
|
|
110
|
|
|
$definition = $treeBuilder->getRootNode(); |
111
|
|
|
|
112
|
|
|
\assert($definition instanceof ArrayNodeDefinition); |
113
|
|
|
|
114
|
|
|
$this->buildPathNode($definition); |
115
|
|
|
|
116
|
|
|
return $definition->getNode(true); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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: