1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the BazingaGeocoderBundle package. |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @license MIT License |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Bazinga\GeocoderBundle\DependencyInjection; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
14
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
15
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author William Durand <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Configuration implements ConfigurationInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Whether to use the debug mode. |
24
|
|
|
* |
25
|
|
|
* @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41 |
26
|
|
|
* |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $debug; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param bool $debug |
33
|
|
|
*/ |
34
|
|
|
public function __construct($debug) |
35
|
|
|
{ |
36
|
|
|
$this->debug = (bool) $debug; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Generates the configuration tree builder. |
41
|
|
|
* |
42
|
|
|
* @return TreeBuilder The tree builder |
43
|
|
|
*/ |
44
|
|
|
public function getConfigTreeBuilder() |
45
|
|
|
{ |
46
|
|
|
$treeBuilder = new TreeBuilder(); |
47
|
|
|
$rootNode = $treeBuilder->root('bazinga_geocoder'); |
48
|
|
|
|
49
|
|
|
$rootNode |
50
|
|
|
->children() |
51
|
|
|
->append($this->getProvidersNode()) |
52
|
|
|
->arrayNode('profiling') |
53
|
|
|
->addDefaultsIfNotSet() |
54
|
|
|
->treatFalseLike(['enabled' => false]) |
55
|
|
|
->treatTrueLike(['enabled' => true]) |
56
|
|
|
->treatNullLike(['enabled' => $this->debug]) |
57
|
|
|
->info('Extend the debug profiler with information about requests.') |
58
|
|
|
->children() |
59
|
|
|
->booleanNode('enabled') |
60
|
|
|
->info('Turn the toolbar on or off. Defaults to kernel debug mode.') |
61
|
|
|
->defaultValue($this->debug) |
62
|
|
|
->end() |
63
|
|
|
->end() |
64
|
|
|
->end() |
65
|
|
|
->arrayNode('fake_ip') |
66
|
|
|
->beforeNormalization() |
67
|
|
|
->ifString() |
68
|
|
|
->then(function ($value) { |
69
|
|
|
return ['ip' => $value]; |
70
|
|
|
}) |
71
|
|
|
->end() |
72
|
|
|
->canBeEnabled() |
73
|
|
|
->children() |
74
|
|
|
->scalarNode('ip')->defaultNull()->end() |
75
|
|
|
->end() |
76
|
|
|
->end(); |
77
|
|
|
|
78
|
|
|
return $treeBuilder; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return ArrayNodeDefinition |
83
|
|
|
*/ |
84
|
|
|
private function getProvidersNode() |
85
|
|
|
{ |
86
|
|
|
$treeBuilder = new TreeBuilder(); |
87
|
|
|
$node = $treeBuilder->root('providers'); |
88
|
|
|
|
89
|
|
|
$node |
|
|
|
|
90
|
|
|
->requiresAtLeastOneElement() |
91
|
|
|
->useAttributeAsKey('name') |
92
|
|
|
->prototype('array') |
93
|
|
|
->children() |
94
|
|
|
->scalarNode('factory')->isRequired()->cannotBeEmpty()->end() |
95
|
|
|
->variableNode('options')->defaultValue([])->end() |
96
|
|
|
->scalarNode('cache')->defaultNull()->end() |
97
|
|
|
->scalarNode('cache_lifetime')->defaultNull()->end() |
98
|
|
|
->arrayNode('aliases') |
99
|
|
|
->prototype('scalar')->end() |
100
|
|
|
->end() |
101
|
|
|
->end() |
102
|
|
|
->end(); |
103
|
|
|
|
104
|
|
|
return $node; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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: