1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the BazingaGeocoderBundle package. |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license MIT License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Bazinga\GeocoderBundle\DependencyInjection; |
14
|
|
|
|
15
|
|
|
use Bazinga\GeocoderBundle\DataCollector\GeocoderDataCollector; |
16
|
|
|
use Bazinga\GeocoderBundle\Plugin\FakeIpPlugin; |
17
|
|
|
use Bazinga\GeocoderBundle\Plugin\ProfilingPlugin; |
18
|
|
|
use Bazinga\GeocoderBundle\ProviderFactory\PluginProviderFactory; |
19
|
|
|
use Bazinga\GeocoderBundle\ProviderFactory\ProviderFactoryInterface; |
20
|
|
|
use Geocoder\Plugin\Plugin\CachePlugin; |
21
|
|
|
use Geocoder\Plugin\Plugin\LimitPlugin; |
22
|
|
|
use Geocoder\Plugin\Plugin\LocalePlugin; |
23
|
|
|
use Geocoder\Plugin\Plugin\LoggerPlugin; |
24
|
|
|
use Geocoder\Plugin\PluginProvider; |
25
|
|
|
use Symfony\Component\Config\Definition\Processor; |
26
|
|
|
use Symfony\Component\Config\FileLocator; |
27
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
28
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
29
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
30
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* William Durand <[email protected]>. |
34
|
|
|
*/ |
35
|
|
|
class BazingaGeocoderExtension extends Extension |
36
|
|
|
{ |
37
|
23 |
|
public function load(array $configs, ContainerBuilder $container) |
38
|
|
|
{ |
39
|
23 |
|
$processor = new Processor(); |
40
|
23 |
|
$configuration = $this->getConfiguration($configs, $container); |
41
|
23 |
|
$config = $processor->processConfiguration($configuration, $configs); |
|
|
|
|
42
|
|
|
|
43
|
23 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
44
|
23 |
|
$loader->load('services.yml'); |
45
|
|
|
|
46
|
23 |
|
if (true === $config['profiling']['enabled']) { |
47
|
1 |
|
$loader->load('profiling.yml'); |
48
|
|
|
} |
49
|
|
|
|
50
|
23 |
|
if ($config['fake_ip']['enabled']) { |
51
|
|
|
$definition = $container->getDefinition(FakeIpPlugin::class); |
52
|
|
|
$definition->replaceArgument(1, $config['fake_ip']['ip']); |
53
|
|
|
} else { |
54
|
23 |
|
$container->removeDefinition(FakeIpPlugin::class); |
55
|
|
|
} |
56
|
|
|
|
57
|
23 |
|
$this->loadProviders($container, $config); |
58
|
23 |
|
} |
59
|
|
|
|
60
|
23 |
|
private function loadProviders(ContainerBuilder $container, array $config) |
61
|
|
|
{ |
62
|
23 |
|
foreach ($config['providers'] as $providerName => $providerConfig) { |
63
|
22 |
|
$factoryService = $container->getDefinition($providerConfig['factory']); |
64
|
22 |
|
$factoryClass = $factoryService->getClass() ?: $providerConfig['factory']; |
65
|
22 |
|
if (!$this->implementsPoviderFactory($factoryClass)) { |
66
|
|
|
throw new \LogicException(sprintf('Provider factory "%s" must implement ProviderFactoryInterface', $providerConfig['factory'])); |
67
|
|
|
} |
68
|
22 |
|
$factoryClass::validate($providerConfig['options'], $providerName); |
69
|
|
|
|
70
|
|
|
// See if any option has a service reference |
71
|
22 |
|
$providerConfig['options'] = $this->findReferences($providerConfig['options']); |
72
|
|
|
|
73
|
22 |
|
$serviceId = 'bazinga_geocoder.provider.'.$providerName; |
74
|
22 |
|
$plugins = $this->configureProviderPlugins($container, $providerConfig, $serviceId); |
75
|
|
|
|
76
|
22 |
|
$def = $container->register($serviceId, PluginProvider::class) |
77
|
22 |
|
->setFactory([PluginProviderFactory::class, 'createPluginProvider']) |
78
|
22 |
|
->addArgument($plugins) |
79
|
22 |
|
->addArgument(new Reference($providerConfig['factory'])) |
80
|
22 |
|
->addArgument($providerConfig['options']); |
81
|
|
|
|
82
|
22 |
|
$def->addTag('bazinga_geocoder.provider'); |
83
|
22 |
|
foreach ($providerConfig['aliases'] as $alias) { |
84
|
22 |
|
$container->setAlias($alias, $serviceId); |
85
|
|
|
} |
86
|
|
|
} |
87
|
23 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Configure plugins for a client. |
91
|
|
|
* |
92
|
|
|
* @param ContainerBuilder $container |
93
|
|
|
* @param array $config |
94
|
|
|
* @param string $providerServiceId |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
22 |
|
public function configureProviderPlugins(ContainerBuilder $container, array $config, string $providerServiceId): array |
99
|
|
|
{ |
100
|
22 |
|
$plugins = []; |
101
|
22 |
|
foreach ($config['plugins'] as $plugin) { |
102
|
|
|
$plugins[] = $plugin['id']; |
103
|
|
|
} |
104
|
|
|
|
105
|
22 |
|
if (isset($config['cache']) || isset($config['cache_lifetime'])) { |
106
|
1 |
|
if (null === $cacheServiceId = $config['cache']) { |
107
|
|
|
if (!$container->has('app.cache')) { |
108
|
|
|
throw new \LogicException('You need to specify a service for cache.'); |
109
|
|
|
} |
110
|
|
|
$cacheServiceId = 'app.cache'; |
111
|
|
|
} |
112
|
1 |
|
$plugins[] = $providerServiceId.'.cache'; |
113
|
1 |
|
$container->register($providerServiceId.'.cache', CachePlugin::class) |
114
|
1 |
|
->setPublic(false) |
115
|
1 |
|
->setArguments([new Reference($cacheServiceId), (int) $config['cache_lifetime']]); |
116
|
|
|
} |
117
|
|
|
|
118
|
22 |
View Code Duplication |
if (isset($config['limit'])) { |
|
|
|
|
119
|
|
|
$plugins[] = $providerServiceId.'.limit'; |
120
|
|
|
$container->register($providerServiceId.'.limit', LimitPlugin::class) |
121
|
|
|
->setPublic(false) |
122
|
|
|
->setArguments([(int) $config['limit']]); |
123
|
|
|
} |
124
|
|
|
|
125
|
22 |
View Code Duplication |
if (isset($config['locale'])) { |
|
|
|
|
126
|
|
|
$plugins[] = $providerServiceId.'.locale'; |
127
|
|
|
$container->register($providerServiceId.'.locale', LocalePlugin::class) |
128
|
|
|
->setPublic(false) |
129
|
|
|
->setArguments([$config['locale']]); |
130
|
|
|
} |
131
|
|
|
|
132
|
22 |
View Code Duplication |
if (isset($config['logger'])) { |
|
|
|
|
133
|
|
|
$plugins[] = $providerServiceId.'.logger'; |
134
|
|
|
$container->register($providerServiceId.'.logger', LoggerPlugin::class) |
135
|
|
|
->setPublic(false) |
136
|
|
|
->setArguments([new Reference($config['logger'])]); |
137
|
|
|
} |
138
|
|
|
|
139
|
22 |
|
if ($container->has(FakeIpPlugin::class)) { |
140
|
|
|
$plugins[] = FakeIpPlugin::class; |
141
|
|
|
} |
142
|
|
|
|
143
|
22 |
|
if ($container->has(GeocoderDataCollector::class)) { |
144
|
|
|
$plugins[] = $providerServiceId.'.profiler'; |
145
|
|
|
$container->register($providerServiceId.'.profiler', ProfilingPlugin::class) |
146
|
|
|
->setPublic(false) |
147
|
|
|
->setArguments([substr($providerServiceId, strlen('bazinga_geocoder.provider.'))]) |
148
|
|
|
->addTag('bazinga_geocoder.profiling_plugin'); |
149
|
|
|
} |
150
|
|
|
|
151
|
22 |
|
return array_map(function (string $id) { |
152
|
1 |
|
return new Reference($id); |
153
|
22 |
|
}, $plugins); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
23 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
160
|
|
|
{ |
161
|
23 |
|
return new Configuration($container->getParameter('kernel.debug')); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param array $options |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
22 |
|
private function findReferences(array $options): array |
170
|
|
|
{ |
171
|
22 |
|
foreach ($options as $key => $value) { |
172
|
18 |
|
if (is_array($value)) { |
173
|
1 |
|
$options[$key] = $this->findReferences($value); |
174
|
18 |
|
} elseif (substr((string) $key, -8) === '_service' || strpos((string) $value, '@') === 0 || $key === 'service') { |
175
|
18 |
|
$options[$key] = new Reference(ltrim($value, '@')); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
22 |
|
return $options; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param mixed $factoryClass |
184
|
|
|
* |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
22 |
|
private function implementsPoviderFactory($factoryClass): bool |
188
|
|
|
{ |
189
|
22 |
|
if (false === $interfaces = class_implements($factoryClass)) { |
190
|
|
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
22 |
|
return in_array(ProviderFactoryInterface::class, $interfaces); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: