1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the BazingaGeocoderBundle package. |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @license MIT License |
9
|
|
|
*/ |
10
|
|
|
namespace Bazinga\Bundle\GeocoderBundle\DependencyInjection; |
11
|
|
|
|
12
|
|
|
use Bazinga\Bundle\GeocoderBundle\ProviderFactory\ProviderFactoryInterface; |
13
|
|
|
use Geocoder\Provider\Provider; |
14
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
15
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
16
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
use Symfony\Component\Config\FileLocator; |
21
|
|
|
use Symfony\Component\Config\Definition\Processor; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* William Durand <[email protected]>. |
25
|
|
|
*/ |
26
|
|
|
class BazingaGeocoderExtension extends Extension |
27
|
|
|
{ |
28
|
|
|
public function load(array $configs, ContainerBuilder $container) |
29
|
|
|
{ |
30
|
|
|
$processor = new Processor(); |
31
|
|
|
$configuration = $this->getConfiguration($configs, $container); |
32
|
|
|
$config = $processor->processConfiguration($configuration, $configs); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
35
|
|
|
$loader->load('services.yml'); |
36
|
|
|
|
37
|
|
|
if ($config['profiling']) { |
38
|
|
|
$loader->load('profiling.yml'); |
39
|
|
|
} |
40
|
|
|
$this->loadProviders($container, $config); |
41
|
|
|
|
42
|
|
|
if (!empty($config['fake_ip']) && true === $config['fake_ip']['enabled']) { |
43
|
|
|
$definition = $container->getDefinition('bazinga_geocoder.event_listener.fake_request'); |
44
|
|
|
$definition->replaceArgument(0, $config['fake_ip']['ip']); |
45
|
|
|
|
46
|
|
|
$tag = current($definition->getTag('kernel.event_listener')); |
47
|
|
|
$tag['priority'] = $config['fake_ip']['priority']; |
48
|
|
|
$tags = array('kernel.event_listener' => array($tag)); |
49
|
|
|
$definition->setTags($tags); |
50
|
|
|
} else { |
51
|
|
|
$container->removeDefinition('bazinga_geocoder.event_listener.fake_request'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function loadProviders(ContainerBuilder $container, array $config) |
56
|
|
|
{ |
57
|
|
|
foreach ($config['providers'] as $providerName => $providerConfig) { |
58
|
|
|
$factoryService = $container->getDefinition($providerConfig['factory']); |
59
|
|
|
$factoryClass = $factoryService->getClass() ?: $providerConfig['factory']; |
60
|
|
|
if (!(is_a($factoryClass, ProviderFactoryInterface::class))) { |
|
|
|
|
61
|
|
|
//throw new \LogicException(sprintf('Provider factory "%s" must implement ProviderFactoryInterface', $providerConfig['factory'])); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
$factoryClass::validate($providerConfig['options'], $providerName); |
64
|
|
|
|
65
|
|
|
// See if any option has a service reference |
66
|
|
|
$providerConfig['options'] = $this->findReferences($providerConfig['options']); |
67
|
|
|
|
68
|
|
|
$serviceId = 'bazinga_geocoder.provider.'.$providerName; |
69
|
|
|
$def = $container->register($serviceId, Provider::class); |
70
|
|
|
$def->setFactory([new Reference($providerConfig['factory']), 'createProvider']) |
71
|
|
|
->addArgument($providerConfig['options']); |
72
|
|
|
|
73
|
|
|
$def->addTag('bazinga_geocoder.provider'); |
74
|
|
|
foreach ($providerConfig['aliases'] as $alias) { |
75
|
|
|
$container->setAlias($alias, $serviceId); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->configureCache($container, $serviceId, $providerConfig); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Add cache to a provider if needed. |
84
|
|
|
* |
85
|
|
|
* @param ContainerBuilder $ |
86
|
|
|
* @param string $serviceId |
87
|
|
|
* @param array $providerConfig |
88
|
|
|
*/ |
89
|
|
|
private function configureCache(ContainerBuilder $container, string $serviceId, array $providerConfig) |
90
|
|
|
{ |
91
|
|
|
if ( |
92
|
|
|
(null !== $providerConfig['cache'] || null !== $providerConfig['cache_lifetime']) && |
93
|
|
|
!class_exists(ProviderCache::class) |
94
|
|
|
) { |
95
|
|
|
throw new \LogicException('You must install "geocoder-php/chain-provider" to use cache.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (null !== $cacheServiceId = $providerConfig['cache']) { |
99
|
|
|
if (!$container->has('app.cache\'')) { |
100
|
|
|
throw new \LogicException('You need to specify a service for cache.'); |
101
|
|
|
} |
102
|
|
|
$cacheServiceId = 'app.cache'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$container->register($serviceId.'.cache', ProviderCache::class) |
106
|
|
|
->setDecoratedService($serviceId) |
107
|
|
|
->setArguments([ |
108
|
|
|
new Reference($serviceId.'.cache.inner'), |
109
|
|
|
new Reference($cacheServiceId), |
110
|
|
|
$providerConfig['cache_lifetime'] |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function getConfiguration(array $config, ContainerBuilder $container) |
118
|
|
|
{ |
119
|
|
|
return new Configuration($container->getParameter('kernel.debug')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $options |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
private function findReferences(array $options) |
128
|
|
|
{ |
129
|
|
|
foreach ($options as $key => $value) { |
130
|
|
|
if (is_array($value)) { |
131
|
|
|
$options[$key] = $this->findReferences($value); |
132
|
|
|
} elseif (substr($key, -8) === '_service' || strpos($value, '@') === 0 || $key === 'service') { |
133
|
|
|
$options[$key] = new Reference(ltrim($value, '@')); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $options; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
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: