1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RunOpenCode\Bundle\ExchangeRate\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use RunOpenCode\ExchangeRate\Configuration as RateConfiguration; |
6
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
10
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension; |
11
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
12
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
13
|
|
|
|
14
|
|
|
class Extension extends BaseExtension |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
2 |
|
public function load(array $config, ContainerBuilder $container) |
20
|
|
|
{ |
21
|
2 |
|
$configuration = new Configuration(); |
22
|
2 |
|
$config = $this->processConfiguration($configuration, $config); |
23
|
|
|
|
24
|
2 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
25
|
2 |
|
$loader->load('services.xml'); |
26
|
|
|
|
27
|
2 |
|
$this->configureExchangeRateService($config, $container); |
28
|
2 |
|
$this->configureSourcesRegistry($config, $container); |
29
|
2 |
|
$this->configureProcessorsRegistry($config, $container); |
30
|
2 |
|
$this->configureRatesRegistry($config, $container); |
31
|
2 |
|
$this->configureFileRepository($config, $container); |
32
|
2 |
|
} |
33
|
|
|
|
34
|
2 |
|
protected function configureExchangeRateService(array $config, ContainerBuilder $container) |
35
|
|
|
{ |
36
|
2 |
|
if ($container->hasDefinition('run_open_code.exchange_rate')) { |
37
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate'); |
38
|
|
|
|
39
|
2 |
|
$definition->setArguments(array( |
40
|
2 |
|
$config['base_currency'], |
41
|
2 |
|
new Reference($config['repository']), |
42
|
2 |
|
new Reference('run_open_code.exchange_rate.registry.sources'), |
43
|
2 |
|
new Reference('run_open_code.exchange_rate.registry.processors'), |
44
|
2 |
|
new Reference('run_open_code.exchange_rate.registry.rates') |
45
|
2 |
|
)); |
46
|
2 |
|
} |
47
|
2 |
|
} |
48
|
|
|
|
49
|
2 |
|
protected function configureSourcesRegistry(array $config, ContainerBuilder $container) |
50
|
|
|
{ |
51
|
2 |
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.sources')) { |
52
|
|
|
|
53
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.sources'); |
54
|
|
|
|
55
|
2 |
|
$requiredSources = array(); |
56
|
|
|
|
57
|
2 |
|
foreach ($config['rates'] as $rate) { |
58
|
2 |
|
$requiredSources[$rate['source']] = $rate['source']; |
59
|
2 |
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
2 |
|
foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.source') as $id => $tags) { |
63
|
|
|
|
64
|
2 |
|
foreach ($tags as $attributes) { |
65
|
|
|
|
66
|
2 |
|
if (array_key_exists($attributes['alias'], $requiredSources)) { |
67
|
|
|
|
68
|
2 |
|
$definition->addMethodCall('add', array( |
69
|
2 |
|
new Reference($id) |
70
|
2 |
|
)); |
71
|
|
|
|
72
|
2 |
|
unset($requiredSources[$attributes['alias']]); |
73
|
2 |
|
} |
74
|
2 |
|
} |
75
|
2 |
|
} |
76
|
|
|
|
77
|
2 |
|
if (count($requiredSources) > 0) { |
78
|
|
|
throw new InvalidConfigurationException(sprintf('Required source(s) "%s" does not exists.', implode(', ', $requiredSources))); |
79
|
|
|
} |
80
|
2 |
|
} |
81
|
2 |
|
} |
82
|
|
|
|
83
|
2 |
|
protected function configureProcessorsRegistry(array $config, ContainerBuilder $container) |
84
|
|
|
{ |
85
|
2 |
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.processors')) { |
86
|
|
|
|
87
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.processors'); |
88
|
|
|
|
89
|
2 |
|
$processors = array(); |
90
|
|
|
|
91
|
2 |
|
foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.processor') as $id => $tags) { |
92
|
|
|
|
93
|
2 |
|
if (!in_array($id, $config['processors'])) { |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
foreach ($tags as $attributes) { |
98
|
2 |
|
$processors[$id] = (isset($attributes['priority'])) ? intval($attributes['priority']) : 0; |
99
|
2 |
|
} |
100
|
2 |
|
} |
101
|
|
|
|
102
|
2 |
|
asort($processors); |
103
|
|
|
|
104
|
2 |
|
foreach (array_keys($processors) as $id) { |
105
|
2 |
|
$definition->addMethodCall('add', array( |
106
|
2 |
|
new Reference($id) |
107
|
2 |
|
)); |
108
|
2 |
|
} |
109
|
2 |
|
} |
110
|
2 |
|
} |
111
|
|
|
|
112
|
2 |
|
protected function configureRatesRegistry(array $config, ContainerBuilder $container) |
113
|
|
|
{ |
114
|
2 |
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.rates')) { |
115
|
|
|
|
116
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.rates'); |
117
|
|
|
|
118
|
2 |
|
$processed = array(); |
119
|
|
|
|
120
|
2 |
|
foreach ($config['rates'] as $rateConfiguration) { |
121
|
|
|
|
122
|
2 |
|
$key = sprintf('%s_%s', $rateConfiguration['currency_code'], $rateConfiguration['rate_type']); |
123
|
|
|
|
124
|
2 |
|
if (isset($processed[$key])) { |
125
|
|
|
throw new InvalidConfigurationException(sprintf('Currency code "%s" for rate type "%s" is configured twice.', $rateConfiguration['currency_code'], $rateConfiguration['rate_type'])); |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
$rateConfiguration['extra'] = (isset($ratesConfiguration['extra'])) ? $ratesConfiguration['extra'] : array(); |
129
|
|
|
|
130
|
2 |
|
$processed[$key] = $rateConfiguration; |
131
|
2 |
|
} |
132
|
|
|
|
133
|
2 |
|
$definition->setArguments(array( |
134
|
2 |
|
array_values($processed) |
135
|
2 |
|
)); |
136
|
2 |
|
} |
137
|
2 |
|
} |
138
|
|
|
|
139
|
2 |
|
protected function configureFileRepository(array $config, ContainerBuilder $container) |
140
|
|
|
{ |
141
|
|
|
if ( |
142
|
2 |
|
$config['repository'] == 'run_open_code.exchange_rate.repository.file_repository' |
143
|
2 |
|
&& |
144
|
2 |
|
$container->hasDefinition('run_open_code.exchange_rate.repository.file_repository') |
145
|
2 |
|
) { |
146
|
|
|
|
147
|
2 |
|
if (!empty($config['file_repository']) && !empty($config['file_repository']['path'])) { |
148
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.repository.file_repository'); |
149
|
2 |
|
$definition->setArguments(array( |
150
|
2 |
|
$config['file_repository']['path'] |
151
|
2 |
|
)); |
152
|
2 |
|
} else { |
153
|
|
|
throw new InvalidConfigurationException('You must configure location to the file where file repository will store exchange rates.'); |
154
|
|
|
} |
155
|
|
|
|
156
|
2 |
|
} elseif ($config['repository'] == 'run_open_code.exchange_rate.repository.file_repository') { |
157
|
|
|
throw new InvalidConfigurationException('File repository is used to store exchange rates, but it is not available in container.'); |
158
|
|
|
} else { |
159
|
|
|
$container->removeDefinition('run_open_code.exchange_rate.repository.file_repository'); |
160
|
|
|
} |
161
|
2 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
4 |
|
public function getAlias() |
167
|
|
|
{ |
168
|
4 |
|
return "run_open_code_exchange_rate"; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|