|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Ivory Google Map bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ivory\GoogleMapBundle\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
|
|
|
/** |
|
20
|
|
|
* @author GeLo <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class Configuration implements ConfigurationInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
75 |
|
public function getConfigTreeBuilder() |
|
28
|
|
|
{ |
|
29
|
75 |
|
$treeBuilder = $this->createTreeBuilder('ivory_google_map'); |
|
30
|
75 |
|
$children = $treeBuilder->getRootNode() |
|
31
|
75 |
|
->children() |
|
32
|
75 |
|
->append($this->createMapNode()) |
|
33
|
75 |
|
->append($this->createStaticMapNode()); |
|
34
|
|
|
|
|
35
|
|
|
$services = [ |
|
36
|
75 |
|
'direction' => true, |
|
37
|
|
|
'distance_matrix' => true, |
|
38
|
|
|
'elevation' => true, |
|
39
|
|
|
'geocoder' => true, |
|
40
|
|
|
'place_autocomplete' => true, |
|
41
|
|
|
'place_detail' => true, |
|
42
|
|
|
'place_photo' => false, |
|
43
|
|
|
'place_search' => true, |
|
44
|
|
|
'time_zone' => true, |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
75 |
|
foreach ($services as $service => $http) { |
|
48
|
75 |
|
$children->append($this->createServiceNode($service, $http)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
75 |
|
return $treeBuilder; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return ArrayNodeDefinition |
|
56
|
|
|
*/ |
|
57
|
75 |
|
private function createMapNode() |
|
58
|
|
|
{ |
|
59
|
75 |
|
return $this->createNode('map') |
|
|
|
|
|
|
60
|
75 |
|
->addDefaultsIfNotSet() |
|
61
|
75 |
|
->children() |
|
62
|
75 |
|
->booleanNode('debug')->defaultValue('%kernel.debug%')->end() |
|
63
|
75 |
|
->scalarNode('language')->defaultValue('%locale%')->end() |
|
64
|
75 |
|
->scalarNode('api_key')->end() |
|
65
|
75 |
|
->end(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return ArrayNodeDefinition |
|
70
|
|
|
*/ |
|
71
|
75 |
|
private function createStaticMapNode() |
|
72
|
|
|
{ |
|
73
|
75 |
|
return $this->createNode('static_map') |
|
|
|
|
|
|
74
|
75 |
|
->addDefaultsIfNotSet() |
|
75
|
75 |
|
->children() |
|
76
|
75 |
|
->scalarNode('api_key')->end() |
|
77
|
75 |
|
->append($this->createBusinessAccountNode(false)) |
|
78
|
75 |
|
->end(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $service |
|
83
|
|
|
* @param bool $http |
|
84
|
|
|
* |
|
85
|
|
|
* @return ArrayNodeDefinition |
|
86
|
|
|
*/ |
|
87
|
75 |
|
private function createServiceNode($service, $http) |
|
88
|
|
|
{ |
|
89
|
75 |
|
$node = $this->createNode($service); |
|
90
|
|
|
$children = $node |
|
91
|
75 |
|
->children() |
|
92
|
75 |
|
->scalarNode('api_key')->end() |
|
93
|
75 |
|
->append($this->createBusinessAccountNode(true)); |
|
94
|
|
|
|
|
95
|
75 |
|
if ($http) { |
|
96
|
|
|
$children |
|
97
|
75 |
|
->scalarNode('client') |
|
98
|
75 |
|
->isRequired() |
|
99
|
75 |
|
->cannotBeEmpty() |
|
100
|
75 |
|
->end() |
|
101
|
75 |
|
->scalarNode('message_factory') |
|
102
|
75 |
|
->isRequired() |
|
103
|
75 |
|
->cannotBeEmpty() |
|
104
|
75 |
|
->end() |
|
105
|
75 |
|
->scalarNode('format')->end(); |
|
106
|
|
|
} else { |
|
107
|
|
|
$node |
|
108
|
75 |
|
->beforeNormalization() |
|
109
|
75 |
|
->ifNull() |
|
110
|
75 |
|
->then(function () { |
|
111
|
1 |
|
return []; |
|
112
|
75 |
|
}) |
|
113
|
75 |
|
->end(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
75 |
|
return $node; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param bool $service |
|
121
|
|
|
* |
|
122
|
|
|
* @return ArrayNodeDefinition |
|
123
|
|
|
*/ |
|
124
|
75 |
|
private function createBusinessAccountNode($service) |
|
125
|
|
|
{ |
|
126
|
75 |
|
$node = $this->createNode('business_account'); |
|
127
|
75 |
|
$clientIdNode = $node->children() |
|
128
|
75 |
|
->scalarNode('secret') |
|
129
|
75 |
|
->isRequired() |
|
130
|
75 |
|
->cannotBeEmpty() |
|
131
|
75 |
|
->end() |
|
132
|
75 |
|
->scalarNode('channel')->end() |
|
133
|
75 |
|
->scalarNode('client_id'); |
|
134
|
|
|
|
|
135
|
75 |
|
if ($service) { |
|
136
|
|
|
$clientIdNode |
|
137
|
75 |
|
->isRequired() |
|
138
|
75 |
|
->cannotBeEmpty(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
75 |
|
return $node; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string $name |
|
146
|
|
|
* @param string $type |
|
147
|
|
|
* |
|
148
|
|
|
* @return ArrayNodeDefinition|NodeDefinition |
|
149
|
|
|
*/ |
|
150
|
75 |
|
private function createNode(string $name = null, string $type = 'array') |
|
151
|
|
|
{ |
|
152
|
75 |
|
return $this->createTreeBuilder($name, $type)->getRootNode(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $name |
|
157
|
|
|
* @param string $type |
|
158
|
|
|
* @return TreeBuilder |
|
159
|
|
|
*/ |
|
160
|
75 |
|
private function createTreeBuilder(string $name = null, string $type = 'array') |
|
161
|
|
|
{ |
|
162
|
75 |
|
return new TreeBuilder($name, $type); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
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: