forked from geocoder-php/BazingaGeocoderBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBazingaGeocoderExtension.php
207 lines (177 loc) · 8.32 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
<?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\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* @author William Durand <william.durand1@gmail.com>.
*/
class BazingaGeocoderExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$processor = new Processor();
$configuration = $this->getConfiguration($configs, $container);
$config = $processor->processConfiguration($configuration, $configs);
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/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 fzaninotto/faker package.');
}
} else {
$container->removeDefinition(FakeIpPlugin::class);
}
$this->loadProviders($container, $config);
$container->registerForAutoconfiguration(Dumper::class)
->addTag('bazinga_geocoder.dumper');
}
private function loadProviders(ContainerBuilder $container, array $config)
{
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.
*/
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 (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(FakeIpPlugin::class)) {
$plugins[] = FakeIpPlugin::class;
}
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(function (string $id) {
return new Reference($id);
}, $plugins);
}
/**
* {@inheritdoc}
*/
public function getConfiguration(array $config, ContainerBuilder $container)
{
return new Configuration($container->getParameter('kernel.debug'));
}
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);
}
}