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\Bundle\ExchangeRate\DependencyInjection\Configuration as TreeConfiguration; |
13
|
|
|
use RunOpenCode\ExchangeRate\Utils\CurrencyCodeUtil; |
14
|
|
|
use Symfony\Component\Config\FileLocator; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension; |
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Extension |
21
|
|
|
* |
22
|
|
|
* Bundle extension. |
23
|
|
|
* |
24
|
|
|
* @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection |
25
|
|
|
*/ |
26
|
|
|
class Extension extends BaseExtension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
4 |
|
public function getAlias() |
32
|
|
|
{ |
33
|
4 |
|
return "run_open_code_exchange_rate"; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
2 |
|
public function load(array $config, ContainerBuilder $container) |
40
|
|
|
{ |
41
|
|
|
|
42
|
2 |
|
$configuration = new TreeConfiguration(); |
43
|
2 |
|
$config = $this->processConfiguration($configuration, $config); |
44
|
|
|
|
45
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services')); |
46
|
|
|
$loader->load('repository.xml'); |
47
|
|
|
$loader->load('command.xml'); |
48
|
|
|
$loader->load('controller.xml'); |
49
|
|
|
$loader->load('form_type.xml'); |
50
|
|
|
$loader->load('manager.xml'); |
51
|
|
|
$loader->load('processor.xml'); |
52
|
|
|
$loader->load('source.xml'); |
53
|
|
|
$loader->load('validator.xml'); |
54
|
|
|
|
55
|
|
|
$this |
56
|
|
|
->configureBaseCurrency($config, $container) |
57
|
|
|
->configureRepository($config, $container) |
58
|
|
|
->configureFileRepository($config, $container) |
59
|
|
|
->configureController($config, $container) |
60
|
|
|
->configureRates($config, $container) |
61
|
|
|
->configureSimpleSources($config, $container) |
62
|
|
|
; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Configure base currency. |
67
|
|
|
* |
68
|
|
|
* @param array $config Configuration parameters. |
69
|
|
|
* @param ContainerBuilder $container Service container. |
70
|
|
|
* @return Extension $this Fluent interface. |
71
|
|
|
*/ |
72
|
|
|
protected function configureBaseCurrency(array $config, ContainerBuilder $container) |
73
|
|
|
{ |
74
|
|
|
$baseCurrency = CurrencyCodeUtil::clean($config['base_currency']); |
75
|
|
|
$container->setParameter('run_open_code.exchange_rate.base_currency', $baseCurrency); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Configure rates. |
82
|
|
|
* |
83
|
|
|
* @param array $config Configuration parameters. |
84
|
|
|
* @param ContainerBuilder $container Service container. |
85
|
|
|
* @return Extension $this Fluent interface. |
86
|
|
|
*/ |
87
|
|
|
protected function configureRates(array $config, ContainerBuilder $container) |
88
|
|
|
{ |
89
|
|
|
if (count($config['rates']) === 0) { |
90
|
|
|
throw new \RuntimeException('You have to configure which rates you would like to use in your project.'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$container->setParameter('run_open_code.exchange_rate.registered_rates', $config['rates']); |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Configure required processors. |
99
|
|
|
* |
100
|
|
|
* @param array $config Configuration parameters. |
101
|
|
|
* @param ContainerBuilder $container Service container. |
102
|
|
|
* @return Extension $this Fluent interface. |
103
|
|
|
*/ |
104
|
|
|
protected function configureRepository(array $config, ContainerBuilder $container) |
105
|
|
|
{ |
106
|
|
|
$container->setParameter('run_open_code.exchange_rate.repository', $config['repository']); |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Configure file repository, if used. |
112
|
|
|
* |
113
|
|
|
* @param array $config Configuration parameters. |
114
|
|
|
* @param ContainerBuilder $container Service container. |
115
|
|
|
* @return Extension $this Fluent interface. |
116
|
|
|
*/ |
117
|
|
|
protected function configureFileRepository(array $config, ContainerBuilder $container) |
118
|
|
|
{ |
119
|
|
|
$container->setParameter('run_open_code.exchange_rate.repository.file_repository.path', $config['file_repository']['path']); |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Configure controller. |
125
|
|
|
* |
126
|
|
|
* @param array $config Configuration parameters. |
127
|
|
|
* @param ContainerBuilder $container Service container. |
128
|
|
|
* @return Extension $this Fluent interface. |
129
|
|
|
*/ |
130
|
|
|
protected function configureController(array $config, ContainerBuilder $container) |
131
|
|
|
{ |
132
|
|
|
$container->setParameter( |
133
|
|
|
'run_open_code.exchange_rate.controller.view_configuration', |
134
|
|
|
$config['view'] |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Configure simple sources which does not have to be explicitly added to service container. |
142
|
|
|
* |
143
|
|
|
* @param array $config Configuration parameters. |
144
|
|
|
* @param ContainerBuilder $container Service container. |
145
|
|
|
* @return Extension $this Fluent interface. |
146
|
|
|
*/ |
147
|
|
|
protected function configureSimpleSources(array $config, ContainerBuilder $container) |
148
|
|
|
{ |
149
|
|
|
if (!empty($config['sources']) && count($config['sources']) > 0) { |
150
|
|
|
$container->setParameter('run_open_code.exchange_rate.source.registered_simple_sources', $config['sources']); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|