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