1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the BazingaGeocoderBundle package. |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license MIT License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Bazinga\GeocoderBundle\DependencyInjection; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
16
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
17
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author William Durand <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Configuration implements ConfigurationInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Whether to use the debug mode. |
26
|
|
|
* |
27
|
|
|
* @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41 |
28
|
|
|
* |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private $debug; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param bool $debug |
35
|
|
|
*/ |
36
|
28 |
|
public function __construct($debug) |
37
|
|
|
{ |
38
|
28 |
|
$this->debug = (bool) $debug; |
39
|
28 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Generates the configuration tree builder. |
43
|
|
|
* |
44
|
|
|
* @return TreeBuilder The tree builder |
45
|
|
|
*/ |
46
|
28 |
|
public function getConfigTreeBuilder() |
47
|
|
|
{ |
48
|
28 |
|
$treeBuilder = new TreeBuilder(); |
49
|
28 |
|
$rootNode = $treeBuilder->root('bazinga_geocoder'); |
50
|
|
|
|
51
|
|
|
$rootNode |
52
|
28 |
|
->children() |
53
|
28 |
|
->append($this->getProvidersNode()) |
54
|
28 |
|
->arrayNode('profiling') |
55
|
28 |
|
->addDefaultsIfNotSet() |
56
|
28 |
|
->treatFalseLike(['enabled' => false]) |
57
|
28 |
|
->treatTrueLike(['enabled' => true]) |
58
|
28 |
|
->treatNullLike(['enabled' => $this->debug]) |
59
|
28 |
|
->info('Extend the debug profiler with information about requests.') |
60
|
28 |
|
->children() |
61
|
28 |
|
->booleanNode('enabled') |
62
|
28 |
|
->info('Turn the toolbar on or off. Defaults to kernel debug mode.') |
63
|
28 |
|
->defaultValue($this->debug) |
64
|
28 |
|
->end() |
65
|
28 |
|
->end() |
66
|
28 |
|
->end() |
67
|
28 |
|
->arrayNode('fake_ip') |
68
|
28 |
|
->beforeNormalization() |
69
|
28 |
|
->ifString() |
70
|
28 |
|
->then(function ($value) { |
71
|
|
|
return ['ip' => $value]; |
72
|
28 |
|
}) |
73
|
28 |
|
->end() |
74
|
28 |
|
->canBeEnabled() |
75
|
28 |
|
->children() |
76
|
28 |
|
->scalarNode('ip')->defaultNull()->end() |
77
|
28 |
|
->end() |
78
|
28 |
|
->end(); |
79
|
|
|
|
80
|
28 |
|
return $treeBuilder; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return ArrayNodeDefinition |
85
|
|
|
*/ |
86
|
28 |
|
private function getProvidersNode() |
87
|
|
|
{ |
88
|
28 |
|
$treeBuilder = new TreeBuilder(); |
89
|
28 |
|
$node = $treeBuilder->root('providers'); |
90
|
|
|
|
91
|
|
|
$node |
|
|
|
|
92
|
28 |
|
->requiresAtLeastOneElement() |
93
|
28 |
|
->useAttributeAsKey('name') |
94
|
28 |
|
->prototype('array') |
95
|
28 |
|
->fixXmlConfig('plugin') |
96
|
28 |
|
->children() |
97
|
28 |
|
->scalarNode('factory')->isRequired()->cannotBeEmpty()->end() |
98
|
28 |
|
->variableNode('options')->defaultValue([])->end() |
99
|
28 |
|
->scalarNode('cache')->defaultNull()->end() |
100
|
28 |
|
->scalarNode('cache_lifetime')->defaultNull()->end() |
101
|
28 |
|
->scalarNode('limit')->defaultNull()->end() |
102
|
28 |
|
->scalarNode('locale')->defaultNull()->end() |
103
|
28 |
|
->scalarNode('logger')->defaultNull()->end() |
104
|
28 |
|
->arrayNode('aliases') |
105
|
28 |
|
->prototype('scalar')->end() |
106
|
28 |
|
->end() |
107
|
28 |
|
->append($this->createClientPluginNode()) |
108
|
28 |
|
->end() |
109
|
28 |
|
->end(); |
110
|
|
|
|
111
|
28 |
|
return $node; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Create plugin node of a client. |
116
|
|
|
* |
117
|
|
|
* @return ArrayNodeDefinition The plugin node |
118
|
|
|
*/ |
119
|
28 |
|
private function createClientPluginNode() |
120
|
|
|
{ |
121
|
28 |
|
$builder = new TreeBuilder(); |
122
|
28 |
|
$node = $builder->root('plugins'); |
123
|
|
|
|
124
|
|
|
/** @var ArrayNodeDefinition $pluginList */ |
125
|
|
|
$pluginList = $node |
126
|
28 |
|
->info('A list of plugin service ids. The order is important.') |
127
|
28 |
|
->prototype('array') |
128
|
|
|
; |
129
|
|
|
$pluginList |
130
|
|
|
// support having just a service id in the list |
131
|
28 |
|
->beforeNormalization() |
132
|
28 |
|
->always(function ($plugin) { |
133
|
2 |
|
if (is_string($plugin)) { |
134
|
|
|
return [ |
135
|
2 |
|
'reference' => [ |
136
|
|
|
'enabled' => true, |
137
|
2 |
|
'id' => $plugin, |
138
|
|
|
], |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
return $plugin; |
143
|
28 |
|
}) |
144
|
28 |
|
->end() |
145
|
|
|
; |
146
|
|
|
|
147
|
|
|
$pluginList |
148
|
28 |
|
->children() |
149
|
28 |
|
->arrayNode('reference') |
150
|
28 |
|
->canBeEnabled() |
151
|
28 |
|
->info('Reference to a plugin service') |
152
|
28 |
|
->children() |
153
|
28 |
|
->scalarNode('id') |
154
|
28 |
|
->info('Service id of a plugin') |
155
|
28 |
|
->isRequired() |
156
|
28 |
|
->cannotBeEmpty() |
157
|
28 |
|
->end() |
158
|
28 |
|
->end() |
159
|
28 |
|
->end() |
160
|
28 |
|
->end() |
161
|
28 |
|
->end(); |
162
|
|
|
|
163
|
28 |
|
return $node; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
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: