|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Exchange Rate Bundle, an RunOpenCode project. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2016 RunOpenCode |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace RunOpenCode\Bundle\ExchangeRate\DependencyInjection; |
|
11
|
|
|
|
|
12
|
|
|
use RunOpenCode\ExchangeRate\Configuration; |
|
|
|
|
|
|
13
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
|
14
|
|
|
use Symfony\Component\Config\FileLocator; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension; |
|
17
|
2 |
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
19
|
2 |
|
|
|
20
|
2 |
|
/** |
|
21
|
|
|
* Class Extension |
|
22
|
2 |
|
* |
|
23
|
2 |
|
* Bundle extension. |
|
24
|
|
|
* |
|
25
|
2 |
|
* @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection |
|
26
|
2 |
|
*/ |
|
27
|
2 |
|
class Extension extends BaseExtension |
|
28
|
2 |
|
{ |
|
29
|
2 |
|
/** |
|
30
|
2 |
|
* {@inheritdoc} |
|
31
|
2 |
|
*/ |
|
32
|
|
|
public function getAlias() |
|
33
|
2 |
|
{ |
|
34
|
|
|
return "run_open_code_exchange_rate"; |
|
35
|
2 |
|
} |
|
36
|
2 |
|
|
|
37
|
|
|
/** |
|
38
|
2 |
|
* {@inheritdoc} |
|
39
|
2 |
|
*/ |
|
40
|
2 |
|
public function load(array $config, ContainerBuilder $container) |
|
41
|
2 |
|
{ |
|
42
|
2 |
|
$configuration = new Configuration(); |
|
|
|
|
|
|
43
|
2 |
|
$config = $this->processConfiguration($configuration, $config); |
|
|
|
|
|
|
44
|
1 |
|
|
|
45
|
1 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
46
|
2 |
|
$loader->load('services.xml'); |
|
47
|
|
|
|
|
48
|
2 |
|
$this->configureExchangeRateService($config, $container); |
|
49
|
|
|
$this->configureSourcesRegistry($config, $container); |
|
50
|
2 |
|
$this->configureProcessorsRegistry($config, $container); |
|
51
|
|
|
$this->configureRatesRegistry($config, $container); |
|
52
|
2 |
|
$this->configureFileRepository($config, $container); |
|
53
|
|
|
$this->configureController($config, $container); |
|
54
|
2 |
|
$this->configureDebugCommand($config, $container); |
|
55
|
|
|
} |
|
56
|
2 |
|
|
|
57
|
2 |
|
/** |
|
58
|
1 |
|
* Configure exchange rate service. |
|
59
|
|
|
* |
|
60
|
|
|
* @param array $config |
|
61
|
2 |
|
* @param ContainerBuilder $container |
|
62
|
|
|
*/ |
|
63
|
2 |
|
protected function configureExchangeRateService(array $config, ContainerBuilder $container) |
|
64
|
|
|
{ |
|
65
|
2 |
|
if ($container->hasDefinition('run_open_code.exchange_rate')) { |
|
66
|
|
|
|
|
67
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate'); |
|
68
|
2 |
|
|
|
69
|
1 |
|
$definition->setArguments(array( |
|
70
|
|
|
$config['base_currency'], |
|
71
|
2 |
|
new Reference($config['repository']), |
|
72
|
1 |
|
new Reference('run_open_code.exchange_rate.registry.sources'), |
|
73
|
1 |
|
new Reference('run_open_code.exchange_rate.registry.processors'), |
|
74
|
1 |
|
new Reference('run_open_code.exchange_rate.registry.rates') |
|
75
|
|
|
)); |
|
76
|
2 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
/** |
|
80
|
2 |
|
* Configure sources registry. |
|
81
|
|
|
* |
|
82
|
2 |
|
* @param array $config |
|
83
|
|
|
* @param ContainerBuilder $container |
|
84
|
2 |
|
*/ |
|
85
|
|
|
protected function configureSourcesRegistry(array $config, ContainerBuilder $container) |
|
86
|
2 |
|
{ |
|
87
|
|
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.sources')) { |
|
88
|
2 |
|
|
|
89
|
|
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.sources'); |
|
90
|
2 |
|
$requiredSources = $this->getRequiredSources($config); |
|
91
|
|
|
|
|
92
|
2 |
|
|
|
93
|
|
|
foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.source') as $id => $tags) { |
|
94
|
|
|
|
|
95
|
|
|
foreach ($tags as $attributes) { |
|
96
|
2 |
|
|
|
97
|
2 |
|
if (array_key_exists($attributes['alias'], $requiredSources)) { |
|
98
|
1 |
|
|
|
99
|
1 |
|
$definition->addMethodCall('add', array( |
|
100
|
|
|
new Reference($id) |
|
101
|
2 |
|
)); |
|
102
|
|
|
|
|
103
|
2 |
|
unset($requiredSources[$attributes['alias']]); |
|
104
|
2 |
|
} |
|
105
|
2 |
|
} |
|
106
|
1 |
|
} |
|
107
|
1 |
|
|
|
108
|
1 |
|
if (count($requiredSources) > 0) { |
|
109
|
2 |
|
throw new InvalidConfigurationException(sprintf('Required source(s) "%s" does not exists.', implode(', ', $requiredSources))); |
|
110
|
|
|
} |
|
111
|
2 |
|
} |
|
112
|
|
|
} |
|
113
|
2 |
|
|
|
114
|
|
|
/** |
|
115
|
2 |
|
* Configure processors registry. |
|
116
|
|
|
* |
|
117
|
2 |
|
* @param array $config |
|
118
|
|
|
* @param ContainerBuilder $container |
|
119
|
2 |
|
*/ |
|
120
|
|
|
protected function configureProcessorsRegistry(array $config, ContainerBuilder $container) |
|
121
|
2 |
|
{ |
|
122
|
|
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.processors')) { |
|
123
|
2 |
|
|
|
124
|
|
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.processors'); |
|
125
|
|
|
|
|
126
|
|
|
$processors = array(); |
|
127
|
2 |
|
|
|
128
|
|
|
foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.processor') as $id => $tags) { |
|
129
|
2 |
|
|
|
130
|
1 |
|
if (!in_array($id, $config['processors'], true)) { |
|
131
|
|
|
continue; |
|
132
|
2 |
|
} |
|
133
|
2 |
|
|
|
134
|
1 |
|
foreach ($tags as $attributes) { |
|
135
|
1 |
|
$processors[$id] = (isset($attributes['priority'])) ? intval($attributes['priority']) : 0; |
|
136
|
2 |
|
} |
|
137
|
|
|
} |
|
138
|
2 |
|
|
|
139
|
|
|
asort($processors); |
|
140
|
|
|
|
|
141
|
2 |
|
foreach ($processors as $id => $processor) { |
|
142
|
1 |
|
$definition->addMethodCall('add', array( |
|
143
|
2 |
|
new Reference($id) |
|
144
|
1 |
|
)); |
|
145
|
|
|
} |
|
146
|
2 |
|
} |
|
147
|
2 |
|
} |
|
148
|
2 |
|
|
|
149
|
2 |
|
/** |
|
150
|
1 |
|
* Configure rates. |
|
151
|
1 |
|
* |
|
152
|
1 |
|
* @param array $config |
|
153
|
|
|
* @param ContainerBuilder $container |
|
154
|
|
|
*/ |
|
155
|
1 |
|
protected function configureRatesRegistry(array $config, ContainerBuilder $container) |
|
156
|
|
|
{ |
|
157
|
|
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.rates')) { |
|
158
|
|
|
|
|
159
|
|
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.rates'); |
|
160
|
2 |
|
|
|
161
|
|
|
$aliases = array(); |
|
162
|
2 |
|
|
|
163
|
|
|
/** |
|
164
|
2 |
|
* @var Configuration $rateConfiguration |
|
165
|
2 |
|
*/ |
|
166
|
2 |
|
foreach ($config['rates'] as $rateConfiguration) { |
|
167
|
2 |
|
|
|
168
|
2 |
|
if ($rateConfiguration->getAlias() === null) { |
|
169
|
2 |
|
continue; |
|
170
|
2 |
|
} |
|
171
|
2 |
|
|
|
172
|
1 |
|
if (array_key_exists($rateConfiguration->getAlias(), $aliases)) { |
|
173
|
1 |
|
throw new InvalidConfigurationException(sprintf('Rate with alias "%s" is already defined.', $rateConfiguration->getAlias())); |
|
174
|
2 |
|
} |
|
175
|
|
|
|
|
176
|
|
|
$aliases[$rateConfiguration->getAlias()] = $rateConfiguration; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
4 |
|
$definition->setArguments(array(array_values($config['rates']))); |
|
180
|
|
|
} |
|
181
|
4 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Configure file registry, if used. |
|
185
|
|
|
* |
|
186
|
|
|
* @param array $config |
|
187
|
|
|
* @param ContainerBuilder $container |
|
188
|
|
|
*/ |
|
189
|
|
|
protected function configureFileRepository(array $config, ContainerBuilder $container) |
|
190
|
|
|
{ |
|
191
|
|
|
if ( |
|
192
|
|
|
$config['repository'] === 'run_open_code.exchange_rate.repository.file_repository' |
|
193
|
|
|
&& |
|
194
|
|
|
$container->hasDefinition('run_open_code.exchange_rate.repository.file_repository') |
|
195
|
|
|
) { |
|
196
|
|
|
|
|
197
|
|
|
if (!empty($config['file_repository']) && !empty($config['file_repository']['path'])) { |
|
198
|
|
|
$definition = $container->getDefinition('run_open_code.exchange_rate.repository.file_repository'); |
|
199
|
|
|
$definition->setArguments(array( |
|
200
|
|
|
$config['file_repository']['path'] |
|
201
|
|
|
)); |
|
202
|
|
|
} else { |
|
203
|
|
|
throw new InvalidConfigurationException('You must configure location to the file where file repository will store exchange rates.'); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
} elseif ($config['repository'] === 'run_open_code.exchange_rate.repository.file_repository') { |
|
207
|
|
|
throw new InvalidConfigurationException('File repository is used to store exchange rates, but it is not available in container.'); |
|
208
|
|
|
} else { |
|
209
|
|
|
$container->removeDefinition('run_open_code.exchange_rate.repository.file_repository'); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Configure controller - view layer. |
|
215
|
|
|
* |
|
216
|
|
|
* @param array $config |
|
217
|
|
|
* @param ContainerBuilder $container |
|
218
|
|
|
*/ |
|
219
|
|
|
protected function configureController(array $config, ContainerBuilder $container) |
|
220
|
|
|
{ |
|
221
|
|
|
if ($container->has('run_open_code.exchange_rate.controller')) { |
|
222
|
|
|
$definition = $container->getDefinition('run_open_code.exchange_rate.controller'); |
|
223
|
|
|
$definition->setArguments(array( |
|
224
|
|
|
new Reference($config['repository']), |
|
225
|
|
|
$config['base_currency'], |
|
226
|
|
|
$config['view'] |
|
227
|
|
|
)); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Configure debug command. |
|
233
|
|
|
* |
|
234
|
|
|
* @param array $config |
|
235
|
|
|
* @param ContainerBuilder $container |
|
236
|
|
|
*/ |
|
237
|
|
|
protected function configureDebugCommand(array $config, ContainerBuilder $container) |
|
238
|
|
|
{ |
|
239
|
|
|
if ($container->has('run_open_code.exchange_rate.command.configuration_debug')) { |
|
240
|
|
|
$definition = $container->getDefinition('run_open_code.exchange_rate.command.configuration_debug'); |
|
241
|
|
|
|
|
242
|
|
|
$arguments = $definition->getArguments(); |
|
243
|
|
|
$arguments[3] = new Reference($config['repository']); |
|
244
|
|
|
|
|
245
|
|
|
$definition->setArguments($arguments); |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Extract name of only required sources from configuration. |
|
251
|
|
|
* |
|
252
|
|
|
* @param array $config |
|
253
|
|
|
* @return array |
|
254
|
|
|
*/ |
|
255
|
|
|
protected function getRequiredSources(array $config) |
|
256
|
|
|
{ |
|
257
|
|
|
$requiredSources = array(); |
|
258
|
|
|
|
|
259
|
|
|
foreach ($config['rates'] as $rate) { |
|
260
|
|
|
$requiredSources[$rate['source']] = $rate['source']; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
return $requiredSources; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: