-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathBazingaGeocoderExtension.php
223 lines (192 loc) · 8.69 KB
/
BazingaGeocoderExtension.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?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 Bazinga\GeocoderBundle\DataCollector\GeocoderDataCollector;
use Bazinga\GeocoderBundle\DependencyInjection\Compiler\FactoryValidatorPass;
use Bazinga\GeocoderBundle\Plugin\FakeIpPlugin;
use Bazinga\GeocoderBundle\Plugin\ProfilingPlugin;
use Bazinga\GeocoderBundle\ProviderFactory\PluginProviderFactory;
use Bazinga\GeocoderBundle\ProviderFactory\ProviderFactoryInterface;
use Faker\Generator;
use Geocoder\Dumper\Dumper;
use Geocoder\Plugin\Plugin\CachePlugin;
use Geocoder\Plugin\Plugin\LimitPlugin;
use Geocoder\Plugin\Plugin\LocalePlugin;
use Geocoder\Plugin\Plugin\LoggerPlugin;
use Geocoder\Plugin\PluginProvider;
use Geocoder\Provider\Provider;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
/**
* @author William Durand <[email protected]>.
*/
class BazingaGeocoderExtension extends Extension
{
/**
* @param array<mixed, mixed> $configs
*/
public function load(array $configs, ContainerBuilder $container): void
{
$processor = new Processor();
$configuration = $this->getConfiguration($configs, $container);
$config = $processor->processConfiguration($configuration, $configs);
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
$loader->load('services.yml');
if (true === $config['profiling']['enabled']) {
$loader->load('profiling.yml');
}
if ($config['fake_ip']['enabled']) {
$definition = $container->getDefinition(FakeIpPlugin::class);
$definition->replaceArgument(0, $config['fake_ip']['local_ip']);
$definition->replaceArgument(1, $config['fake_ip']['ip']);
$definition->replaceArgument(2, $config['fake_ip']['use_faker']);
if ($config['fake_ip']['use_faker'] && !class_exists(Generator::class)) {
throw new \LogicException('To enable this option, you must install fakerphp/faker package.');
}
} else {
$container->removeDefinition(FakeIpPlugin::class);
}
$this->loadProviders($container, $config);
$container->registerForAutoconfiguration(Dumper::class)
->addTag('bazinga_geocoder.dumper');
}
/**
* @param array<mixed, mixed> $config
*/
private function loadProviders(ContainerBuilder $container, array $config): void
{
foreach ($config['providers'] as $providerName => $providerConfig) {
try {
$factoryService = $container->getDefinition($providerConfig['factory']);
$factoryClass = $factoryService->getClass() ?: $providerConfig['factory'];
if (!$this->implementsProviderFactory($factoryClass)) {
throw new \LogicException(sprintf('Provider factory "%s" must implement ProviderFactoryInterface', $providerConfig['factory']));
}
// See if any option has a service reference
$providerConfig['options'] = $this->findReferences($providerConfig['options']);
$factoryClass::validate($providerConfig['options'], $providerName);
} catch (ServiceNotFoundException $e) {
// Assert: We are using a custom factory. If invalid config, it will be caught in FactoryValidatorPass
$providerConfig['options'] = $this->findReferences($providerConfig['options']);
FactoryValidatorPass::addFactoryServiceId($providerConfig['factory']);
}
$serviceId = 'bazinga_geocoder.provider.'.$providerName;
$plugins = $this->configureProviderPlugins($container, $providerConfig, $serviceId);
$def = $container->register($serviceId, PluginProvider::class)
->setFactory([PluginProviderFactory::class, 'createPluginProvider'])
->addArgument($plugins)
->addArgument(new Reference($providerConfig['factory']))
->addArgument($providerConfig['options']);
$def->addTag('bazinga_geocoder.provider');
foreach ($providerConfig['aliases'] as $alias) {
$container->setAlias($alias, $serviceId);
}
$container->registerAliasForArgument($serviceId, Provider::class, "{$providerName}Geocoder");
}
}
/**
* Configure plugins for a client.
*
* @param array<mixed, mixed> $config
*
* @return Reference[]
*/
public function configureProviderPlugins(ContainerBuilder $container, array $config, string $providerServiceId): array
{
$plugins = [];
foreach ($config['plugins'] as $plugin) {
if ($plugin['reference']['enabled']) {
$plugins[] = $plugin['reference']['id'];
}
}
if ($container->has(FakeIpPlugin::class)) {
$plugins[] = FakeIpPlugin::class;
}
if (isset($config['cache']) || isset($config['cache_lifetime']) || isset($config['cache_precision'])) {
$cacheLifetime = isset($config['cache_lifetime']) ? (int) $config['cache_lifetime'] : null;
if (null === $cacheServiceId = $config['cache']) {
if (!$container->has('app.cache')) {
throw new \LogicException('You need to specify a service for cache.');
}
$cacheServiceId = 'app.cache';
}
$plugins[] = $providerServiceId.'.cache';
$container->register($providerServiceId.'.cache', CachePlugin::class)
->setPublic(false)
->setArguments([new Reference($cacheServiceId), $cacheLifetime, $config['cache_precision']]);
}
if (isset($config['limit'])) {
$plugins[] = $providerServiceId.'.limit';
$container->register($providerServiceId.'.limit', LimitPlugin::class)
->setPublic(false)
->setArguments([(int) $config['limit']]);
}
if (isset($config['locale'])) {
$plugins[] = $providerServiceId.'.locale';
$container->register($providerServiceId.'.locale', LocalePlugin::class)
->setPublic(false)
->setArguments([$config['locale']]);
}
if (isset($config['logger'])) {
$plugins[] = $providerServiceId.'.logger';
$container->register($providerServiceId.'.logger', LoggerPlugin::class)
->setPublic(false)
->setArguments([new Reference($config['logger'])]);
}
if ($container->has(GeocoderDataCollector::class)) {
$plugins[] = $providerServiceId.'.profiler';
$container->register($providerServiceId.'.profiler', ProfilingPlugin::class)
->setPublic(false)
->setArguments([substr($providerServiceId, strlen('bazinga_geocoder.provider.'))])
->addTag('bazinga_geocoder.profiling_plugin');
}
return array_map(static fn (string $id): Reference => new Reference($id), $plugins);
}
/**
* @param array<mixed, mixed> $config
*/
public function getConfiguration(array $config, ContainerBuilder $container): Configuration
{
/** @var bool $debug */
$debug = $container->getParameter('kernel.debug');
return new Configuration($debug);
}
/**
* @param array<mixed, mixed> $options
*
* @return array<mixed, mixed>
*/
private function findReferences(array $options): array
{
foreach ($options as $key => $value) {
if (is_array($value)) {
$options[$key] = $this->findReferences($value);
} elseif ('_service' === substr((string) $key, -8) || 0 === strpos((string) $value, '@') || 'service' === $key) {
$options[$key] = new Reference(ltrim($value, '@'));
}
}
return $options;
}
/**
* @param mixed $factoryClass
*/
private function implementsProviderFactory($factoryClass): bool
{
if (false === $interfaces = class_implements($factoryClass)) {
return false;
}
return in_array(ProviderFactoryInterface::class, $interfaces, true);
}
}